NODE_ENV with Jest

亡梦爱人 提交于 2020-06-17 02:04:42

问题


I am migrating from Mocha to Jest. My test import the 'config' package, which selects a configuration file or another depending on the NODE_ENV variable. However, it looks like NODE_ENV variable is found by config while running the test from Jest

Next line does not work (that is, NODE_ENV is ignored):

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

As a consequence the 'config' package reports:

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

Do you know how to include NODE_ENV?


回答1:


Jest automatically defines environment variable NODE_ENV as test(see https://jestjs.io/docs/en/getting-started.html), as you can confirm from your error message:

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

What you can do is simply create config/test.json and include the contents {}, which is an empty valid JSON object.

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

Note: the aforementioned error occurs when you use the config package, and meanwhile you don't have the test.json file in the config directory.




回答2:


You can add this to your babel config:

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

This should also automatically set NODE_ENV=test when running jest.

More info here: Getting Started - Jest



来源:https://stackoverflow.com/questions/48461394/node-env-with-jest

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!