Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 845
Andrés Avatar asked Jan 26 '18 12:01

Andrés


1 Answers

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.

like image 176
Yuci Avatar answered Nov 17 '22 22:11

Yuci