1

How would it be possible to set environment variables for Mocha tests under windows OS? I'm only able to add only 1 variable but not more, example:

"name": "node-app",
"version": "1.0.0",
"description": "some app",
"main": "index.js",
"scripts": {
    "integration-test": "SET TEST_MODE=handler&mocha tests/test_cases/*.js --reporter spec"
},
"author": "",

This can be done under windows using cross-env without changing the source code, we only need to install it as a dev dependency and then add it to the script line. But still under other linux we can simply do this :

"scripts": {
    "integration-test": "env KEY1=YOUR_KEY1 KEY2=YOUR_KEY2 mocha test"
},

I wonder if it is possible to make it happen for windows without additional libraries?

user2517028
  • 784
  • 1
  • 11
  • 25
  • You can set as many Windows environment variables as you want externally (for example, with a .bat file). Mocha can read any/all of them at runtime: [Mocha tests with extra options or parameters](https://stackoverflow.com/questions/16144455/mocha-tests-with-extra-options-or-parameters) – paulsm4 Jul 07 '18 at 17:58
  • Great ref, but those seem to be general solutions that require some changes to the actual code, would it be possible to get an example that doesn't alter the actual code as an answer (as I'm already using env variables)? – user2517028 Jul 07 '18 at 18:18
  • I'd love to be able to use something similar to this: env KEY1=YOUR_KEY1 KEY2=YOUR_KEY2 mocha test – user2517028 Jul 07 '18 at 18:20
  • Possible duplicate of [How to set Environment variables from within package.json \[Node.js\]](https://stackoverflow.com/questions/25112510/how-to-set-environment-variables-from-within-package-json-node-js) – Estus Flask Jul 07 '18 at 18:42

2 Answers2

1

There's a package on npm solving this, called cross-env.

From the documentation:

{
  "scripts": {
    "build": "cross-env NODE_ENV=production webpack --config build/webpack.config.js"
  }
}

You can also set multiple variables easily.

Karamell
  • 1,023
  • 9
  • 23
0

No extra library:

    before(function (): void {
        process.env.YOUR_VAR = 'yourVarValue';
    });
Barosanu240
  • 725
  • 8
  • 15