13

I am migrating from Mocha to Jest. My test imports the config package, which selects a configuration file or another depending on the NODE_ENV environment variable. However, it looks like NODE_ENV is found 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?

x80486
  • 6,627
  • 5
  • 52
  • 111
Andrés
  • 719
  • 1
  • 4
  • 21

4 Answers4

27

Jest automatically defines environment variable NODE_ENV as test(see https://jestjs.io/docs/environment-variables), 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.

Shockline
  • 25
  • 1
  • 5
Yuci
  • 27,235
  • 10
  • 114
  • 113
  • How to override the NODE_ENV defined by jest? Can i assign that NODE_ENV with my own environment? – dennbagas Nov 11 '19 at 07:14
  • @dennbagas, yes, you can. For example, on Mac/Linux, you can try `$ NODE_ENV=dev npm run test`, and `NODE_ENV` will be set to `dev`; on Windows, check this question: https://stackoverflow.com/questions/9249830/how-can-i-set-node-env-production-on-windows – Yuci Nov 19 '19 at 11:04
3

The easiest way is using cross-env package:

npm install --save-dev cross-env

Then, you can use it in your package.json:

"scripts": {
    "test": "cross-env NODE_ENV=development jest --config jest.config.json"
}
Shahrad Elahi
  • 774
  • 12
  • 22
1

The warning is from the Strict-Mode. So what you have to do here is..

  1. Create a file called test.json inside config/
  2. Add NODE_ENV value as test

That should work

alisidaniel
  • 11
  • 1
  • 5
0

Please create test.js in the config folder:

module.exports = {};

and then add dotenv-cli as devDependencies

dotenv -e .env -- jest --maxWorkers=50% --watch

Note: jest automatically set NODE_ENV=test

Mohammad Fallah
  • 976
  • 11
  • 14