javascript - How to require same file in Mocha test - Stack Overflow

I have configindex.js which returns a different config file based on the NODE_ENV environment variable

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
Add a ment  | 

1 Answer 1

Reset to default 8

I 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.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745524876a4631423.html

相关推荐

  • javascript - How to require same file in Mocha test - Stack Overflow

    I have configindex.js which returns a different config file based on the NODE_ENV environment variable

    3小时前
    10

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信