简体   繁体   English

node.js 在测试中设置 process.env 变量

[英]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?当我使用 mocha 在 node.js 中运行测试时,如何设置时间环境变量?

in a module, I have a variable depending of a environment variable在一个模块中,我有一个取决于环境变量的变量

var myVariable = proccess.env.ENV_VAR;

now I use the rewire module,现在我使用rewire模块,

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:myModule.js文件中,导出一个将变量作为参数的函数,例如:

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.我的第一直觉是在任何 require 语句之前简单地在 test.js 顶部设置 env var。 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.但是,如果您有一个依赖 env var 的模块,并且在同一测试运行中多次需要它,这可能对您不起作用。 say you have an env dependent module called mode.js:假设您有一个名为 mode.js 的依赖于 env 的模块:

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

If you add a single test file called bTest.js with如果您添加一个名为bTest.js测试文件

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.并将其命名为aTest.js ,新文件将首先在您的套件中运行,并且 mode.MODE 对于所有后续测试 js 文件将是未定义的。 The require command won't actually reload the same module multiple times. require命令实际上不会多次重新加载同一个模块。

Let's assume you aren't able to use the dotenv package in your tests.假设您无法在测试中使用 dotenv 包。 If so, you can set values on the process.env programmatically in the mocha config file.如果是这样,您可以在 mocha 配置文件中以编程方式在 process.env 上设置值。 By default, this is found in .mocharc.json or .mocha.yml, but this can easily be translated to .mocharc.js .默认情况下,它可以在 .mocharc.json 或 .mocha.yml 中找到,但这可以很容易地转换为 .mocharc.js 。 Referring to the sample js file here: https://github.com/mochajs/mocha/blob/master/example/config/.mocharc.js参考这里的示例 js 文件: https : //github.com/mochajs/mocha/blob/master/example/config/.mocharc.js

So your .mocharc.js could be所以你的 .mocharc.js 可能是

"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.并且 ENV_VAR 将在 mocha 需要或运行您的任何模块之前设置。 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.即使您正在使用 dotenv ,您也可以选择从您可能不想在本地开发服务器的 .env 文件中设置的 mochajs 配置中设置其他 dotenv 选项。 That way, your .env.mocha vars will be available to individual modules that don't require dotenv.这样,您的 .env.mocha 变量将可用于不需要 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尽管在第二种情况下,您最好将 dotenv env 路径设置为 package.json 中测试命令的一部分: node -r dotenv/config /node_modules/mocha/bin/_mocha dotenv_config_path=/full/custom/path/to/.env.mocha

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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