How to require same file in Mocha test

前端 未结 1 782
难免孤独
难免孤独 2020-12-21 10:55

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

相关标签:
1条回答
  • 2020-12-21 11:36

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

    0 讨论(0)
提交回复
热议问题