10

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
  • 5,713
  • 5
  • 38
  • 91
Andrés
  • 679
  • 1
  • 4
  • 19

3 Answers3

21

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
  • 22,858
  • 8
  • 99
  • 108
  • 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
0

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
  • 1
  • 1
  • 4
-6

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

ReyHaynes
  • 2,568
  • 1
  • 12
  • 18
  • 1
    Thanks for your reply @ReyHaynes. Just a question, is babel mandatory? My application is at backend side and my intention is to run the tests directly with `npm test` – Andrés Jan 26 '18 at 14:31
  • as far as I can see, the Babel preset "env" has nothing at all to do with the env that Jest uses. `process.env.NODE_ENV` will be `test`, regardless of your Babel config – Patrick Hund Dec 28 '18 at 15:46