Andrés
Andrés

Reputation: 783

NODE_ENV with Jest

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?

Upvotes: 16

Views: 47536

Answers (4)

Mohammad Fallah
Mohammad Fallah

Reputation: 1110

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

Upvotes: 0

Shahrad Elahi
Shahrad Elahi

Reputation: 1362

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"
}

Upvotes: 4

alisidaniel
alisidaniel

Reputation: 11

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

Upvotes: 1

Yuci
Yuci

Reputation: 30219

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.

Upvotes: 30

Related Questions