我正在尝试为快速节点应用程序创建单元测试。
我希望用于测试的配置与生产中使用的配置不同,因此我实现了以下内容。
In my index.js
,我将配置加载到全局变量中,如下所示:
global.env = {};
global.env.config = require('./config/config');
// Create the server ...
server.listen(3000);
module.exports = server;
在其他一些控制器中myController.js
,我像这样访问全局变量
var Config = global.env.config
当我使用启动它时node index.js
它工作得很好。
但是当我使用 mocha 和 proxyquire 来覆盖配置时:
describe('myController', function () {
describe("#myMethod", () => {
it("must work", (done) => {
const config = {
INPUT_FILE_DIR: path.resolve('../ressources/input/')
}
const server = proxyquire('../index.js', { './config/config': config })// error in this line
})
})
})
我有一个错误告诉myController
无法读取属性配置
Cannot read property 'config' of undefined
感谢您的帮助