简体   繁体   中英

node.js set process.env variable in test

when I run a test in node.js with mocha, how I can set temporal environment variables?

in a module, I have a variable depending of a environment variable

var myVariable = proccess.env.ENV_VAR;

now I use the rewire module,

var rewire = require('rewire');
var myModule = rewire('../myModule');

myModule.__set__('myVariable', 'someValue');

exist a more simple way? without the rewire module?

In your myModule.js file, export a function that takes the variable as an argument eg:

module.exports = function (var) {
    // return what you were exporting before
};

Then when you require it, require it like so:

var myModule = require('../myModule')(process.env.ENV_VAR);

My first instinct was to simply set the env var at the top of the test.js before any require statements. However, this may not work for you if you have a module that depends on a env var, and it is required multiple times in the same test run. say you have an env dependent module called mode.js:

module.exports = {
    MODE : process.env.ENV_VAR
};

If you add a single test file called bTest.js with

process.env.ENV_VAR= "UNIT_TEST_MODE"
const mode = require('./mode.js')

// describe some tests scenarios that use mode.MODE
...

you will be OK. but if you add a second test file

const mode = require('./mode.js')

// describe some more tests scenarios that use mode.MODE
...

and name it aTest.js , the new file will run first in your suite and mode.MODE will be undefined for all subsequent test js files. The require command won't actually reload the same module multiple times.

Let's assume you aren't able to use the dotenv package in your tests. If so, you can set values on the process.env programmatically in the mocha config file. By default, this is found in .mocharc.json or .mocha.yml, but this can easily be translated to .mocharc.js . Referring to the sample js file here: https://github.com/mochajs/mocha/blob/master/example/config/.mocharc.js

So your .mocharc.js could be

"use strict";

process.env.ENV_VAR = "UNIT_TEST_MODE";
// end of .mocharc.js

and ENV_VAR will be set before mocha requires or runs any of your modules. Even if you are using dotenv , you can choose to flip set other dotenv option from inside your mochajs config that you might not want to set on your local dev server's .env file. That way, your .env.mocha vars will be available to individual modules that don't require dotenv.

"use strict";

require('dotenv').config({ debug: process.env.DEBUG, { path: '/full/custom/path/to/.env.mocha' } })`.
// end of .mocharc.js

Although in the second case, you may be better off just setting the dotenv env path as part of the test command in your package.json: node -r dotenv/config /node_modules/mocha/bin/_mocha dotenv_config_path=/full/custom/path/to/.env.mocha

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