Why does Node fs.watchFile keep test from completing?

Viewed 172

I've recently integrated config-js into a Jest test to get a configurable website url and credentials, but now my test does not fully complete. Generally, I run npm test from the command line, the test executes, and the command line prompt is returned. With config-js in the test, the testing part finishes, but the command line just hangs, and I have to ctrl+c to get back to a prompt. I did some experimentation, and I'm pretty sure this is due to config-js's use of fs.watchFile:

fs.watchFile(this.pathToConfigFile, function () {
    self.loadConfig(self.pathToDefaults, self.pathToConfigFile, self.region);
});

When I comment these lines out, everything works fine. I've tried using fs.watch instead, but that doesn't fix it. Just so that it's said, I'm not actually doing any changing or hot-reloading of the config file in my tests.

In case it's helpful, here's the test:

const Config = require('config-js');
let myConfig = new Config('./my.config.js');
describe('config test', function () {
  it('gets the baseUrl', function() {
    expect(myConfig.get('testing.baseUrl')).toEqual('http://foo.com');
  });
});

And here's what the config file looks like:

module.exports = {
  testing: {
    baseUrl: "http://foo.com"
  }
}
1 Answers
Related