简体   繁体   中英

Bypass NODE_ENV with node-config

I am trying to test my config files by validating them, nothing fancy, a schema, a list of envs, iterate over it, load the config and validate the variable against the schema.

Problem is, to do that, I currently have to set process.env.NODE_ENV . Since the tests have their own reserved config file, this mean if the tests were run in parrallel, it may happen that the test change the NODE_ENV variable when the other tests are loading the config, which while it doesn't seems likely to happens, still bother me.

A simple solution would be to be able to tell node-config to ignore the environment variable, and use a given value as if it was, something like require('config')('myNodeEnv') , but I couldn't find anything similar in the wiki nor the documentation. The closest is custom env variable, but this would just move the problem to another variable.

Instead of tests having their own config files, tests should load a same "base" test-config.

When a specific test needs to override a specific value it should override that specific value and not the entire config (needless to say, we need to make sure to fix the overridden value when the test finishes).

When we work as described above, it reduces the chances of any collision because the chances that different tests will override the exact same value at the exact same moment is very low.

Another option is to run tests separately, for example:

npm run test:suite1
npm run test:suite2

where test:suite1 could be declared in package.json under scripts section, for example:

"test:suite1": "NODE_ENV=test-conf-1 tests/suite1.js",
...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM