带有 Jest 的 NODE_ENV

And*_*rés 7 testing mocha.js node.js jestjs

我正在从 Mocha 迁移到 Jest。我的测试导入了 'config' 包,它根据 NODE_ENV 变量选择一个或另一个配置文件。但是,从 Jest 运行测试时,配置似乎找到了 NODE_ENV 变量

下一行不起作用(即忽略 NODE_ENV):

 NODE_ENV=test jest test/*.js --notify --config jest.config.json
Run Code Online (Sandbox Code Playgroud)

因此,“配置”包报告:

console.error node_modules/config/lib/config.js:1727
WARNING: NODE_ENV value of 'test' did not match any deployment config file names. 
Run Code Online (Sandbox Code Playgroud)

你知道如何包含 NODE_ENV 吗?

Yuc*_*uci 18

Jest 自动将环境变量定义NODE_ENVtest(请参阅https://jestjs.io/docs/en/getting-started.html),您可以从错误消息中确认:

console.error node_modules/config/lib/config.js:1727
WARNING: NODE_ENV value of 'test' did not match any deployment config file names. 
Run Code Online (Sandbox Code Playgroud)

您可以做的只是创建 config/test.json 并包含内容{},这是一个空的有效 JSON 对象。

https://github.com/lorenwest/node-config/wiki/Strict-Mode

注意:使用config包时会出现上述错误,同时config目录下没有test.json文件。