NODE_ENV 是的

NODE_ENV with Jest

我正在从 Mocha 迁移到 Jest。我的测试导入了 config 包,它根据 NODE_ENV 环境变量选择一个或另一个配置文件。但是,看起来 NODE_ENV 被发现而 运行 来自 Jest

的测试

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

 NODE_ENV=test jest test/*.js --notify --config jest.config.json

因此 config 包报告:

console.error node_modules/config/lib/config.js:1727
WARNING: NODE_ENV value of 'test' did not match any deployment config file names. 

你知道如何包含 NODE_ENV 吗?

您可以将其添加到您的 babel 配置中:

"env": {
  "test": {
    "presets": [["env"], ...<OTHER PRESETS>]
  }
}

当运行 jest时,这也应该自动设置NODE_ENV=test

更多信息在这里:Getting Started - Jest

Jest 自动将环境变量 NODE_ENV 定义为 test(参见 https://jestjs.io/docs/environment-variables),您可以从错误消息中确认:

console.error node_modules/config/lib/config.js:1727
WARNING: NODE_ENV value of 'test' did not match any deployment config file names. 

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

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

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

警告来自 Strict-Mode。所以你在这里要做的是..

  1. config/
  2. 中创建一个名为 test.json 的文件
  3. NODE_ENV 值添加为 test

应该可行