admin管理员组文章数量:1428551
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;
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;
Share
Improve this question
asked Apr 9, 2014 at 23:30
doremidoremi
15.3k31 gold badges97 silver badges152 bronze badges
1 Answer
Reset to default 8I would delete the module from Node's module cache before running the 2nd test:
var resolved = require.resolve(__dirname + '/../../config');
delete require.cache[resolved];
So when requiring it again, Node will load from scratch. Note that the code above will only delete the config
module from the cache. If you need to delete the modules loaded by the require
calls inside your config
module, then you'll have to do the same as above for each of them too.
By the way, if your tests are going to bee asynchronous, the you need the done
callback like you currently have. If your tests are meant to remain synchronous as they are now, the you could remove done
from the argument list of the callbacks you give to it
and omit calling it.
本文标签: javascriptHow to require same file in Mocha testStack Overflow
版权声明:本文标题:javascript - How to require same file in Mocha test - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745524876a2661789.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论