如何在 node.js 中存根 process.env?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24589021/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to stub process.env in node.js?
提问by Matt - sanemat
I want to stub process.env.FOOwith bar.
我想存根process.env.FOO使用bar。
var sinon = require('sinon');
var stub = sinon.stub(process.env, 'FOO', 'bar');
I'm confused. I read document, but still I don't understand yet.sinonjs docs
我糊涂了。我阅读了文档,但我仍然不明白。sinonjs 文档
sinonjs is one example, not sinonjs is okay.
sinonjs 就是一个例子,不是 sinonjs 是可以的。
回答by Joshua Dutton
From my understanding of process.env, you can simply treat it like any other variable when setting its properties. Keep in mind, though, that every value in process.envmust be a string. So, if you need a particular value in your test:
根据我对 的理解process.env,在设置其属性时,您可以像对待任何其他变量一样简单地对待它。但是请记住,中的每个值都process.env必须是字符串。因此,如果您在测试中需要特定值:
it('does something interesting', () => {
process.env.NODE_ENV = 'test';
// ...
});
To avoid leaking state into other tests, be sure to reset the variable to its original value or delete it altogether:
为避免将状态泄漏到其他测试中,请确保将变量重置为其原始值或将其完全删除:
afterEach(() => {
delete process.env.NODE_ENV;
});
回答by pllee
I was able to get process.envto be stubed properly in my unit tests by cloning it and in a teardown method restoring it.
process.env通过克隆它并在拆卸方法中恢复它,我能够在我的单元测试中正确地存根。
Example using Mocha
使用 Mocha 的示例
const env = Object.assign({}, process.env);
after(() => {
process.env = env;
});
...
it('my test', ()=> {
process.env.NODE_ENV = 'blah'
})
Keep in mind this will only work if the process.env is only being read in the function you are testing. For example if the code that you are testing reads the variable and uses it in a closure it will not work. You probably invalidate the cached require to test that properly.
请记住,这仅在 process.env 仅在您正在测试的函数中被读取时才有效。例如,如果您正在测试的代码读取变量并在闭包中使用它,它将不起作用。您可能会使缓存的要求无效以正确测试。
For example the following won't have the env stubbed:
例如,以下不会对 env 进行存根:
const nodeEnv = process.env.NODE_ENV;
const fnToTest = () => {
nodeEnv ...
}
回答by Andrew Homeyer
In a spec-helper.coffeeor something similar where you set up your sinon sandbox, keep track of the original process.envand restore it after each test, so you don't leak between tests and don't have to remember to reset every time.
在spec-helper.coffee您设置 sinon 沙箱的地方或类似的地方,跟踪原始process.env并在每次测试后恢复它,这样您就不会在测试之间泄漏,也不必记住每次都重置。
_ = require 'lodash'
sinon = require 'sinon'
beforeEach ->
@originalProcessEnv = _.cloneDeep process.env
afterEach ->
process.env = _.cloneDeep @originalProcessEnv
In your test, use process.envas normal.
在您的测试中,process.env照常使用。
it 'does something based on an env var', ->
process.env.FOO = 'bar'
回答by Getriax
With sinon you can stub any variable like this.
使用 sinon,您可以像这样存根任何变量。
const myObj = {
example: 'oldValue',
};
sinon.stub(myObj, 'example').value('newValue');
myObj.example; // 'newValue'
This example is form sinon documentation. https://sinonjs.org/releases/v6.1.5/stubs/
这个例子是表单 sinon 文档。https://sinonjs.org/releases/v6.1.5/stubs/
With that knowledge, you can stub any environment variable. In your case it would look like this:
有了这些知识,您就可以存根任何环境变量。在您的情况下,它看起来像这样:
let stub = sinon.stub(process.env, 'FOO').value('bar');
回答by Hossein Rabizadeh
How to quickly mock process.env during unit testing.
如何在单元测试期间快速模拟 process.env。
https://glebbahmutov.com/blog/mocking-process-env/
https://glebbahmutov.com/blog/mocking-process-env/
const sinon = require('sinon')
let sandbox = sinon.createSandbox()
beforeEach(() => {
sandbox.stub(process.env, 'USER').value('test-user')
})
it('has expected user', () => {
assert(process.env.USER === 'test-user', 'wrong user')
})
afterEach(() => {
sandbox.restore()
})
But what about properties that might not exist in process.env before the test? You can use the following package and then you will be able to test the not exist env variables.
但是在测试之前 process.env 中可能不存在的属性呢?您可以使用以下包,然后您将能够测试不存在的环境变量。

