I have config/index.js
which returns a different config file based on the NODE_ENV environment variable that is set.
I'm trying to write a simple test to ensure that the right config is returned for each environment, but I'm running into an issue where only the first require is actually be called and subsequent requires of the same file are using the value from the first require.
How should I change my test to resolve this issue?
describe('config', function () {
it('should return dev config', function (done) {
process.env.NODE_ENV = 'development';
var config = require(__dirname + '/../../config'); // development config
console.log(config.plugins.ipFilter);
done();
});
it('should return prod config', function (done) {
process.env.NODE_ENV = 'production';
// development config from above.
// the require here doesn't actually get invoked
var config = require(__dirname + '/../../config');
console.log(config.plugins.ipFilter);
done();
});
});
And here is a simplified version of config/index.js
(which is working fine), that I'm trying to test:
var Hoek = require('hoek');
var settings = {
'defaults': require('./settings/defaults'),
'production': require('./settings/production')
};
var env;
switch (process.env.NODE_ENV) {
case 'production': env = 'production'; break;
case 'development': env = 'development'; break;
default: env = 'defaults'; break;
}
var config = Hoek.applyToDefaults(settings['defaults'], settings[env]);
module.exports = config;
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…