如何存根的过程。在node . js env吗?

时间:2021-05-19 23:08:03

I want to stub process.env.FOO with bar.

我想结束这个过程。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不行。

3 个解决方案

#1


42  

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.env must be a string. So, if you need a particular value in your test:

从我对过程的理解。env,在设置它的属性时,你可以像对待其他变量一样对待它。但是要记住,每一个过程中的价值。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;
   });

#2


14  

I was able to get process.env to be stubed properly in my unit tests by cloning it and in a teardown method restoring it.

我能得到过程。在我的单元测试中,通过克隆env并使用拆解方法来恢复它,来正确地进行测试。

Example using 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.

记住,这只会在过程中起作用。env仅在您正在测试的函数中被读取。例如,如果您正在测试的代码读取变量并在闭包中使用它,那么它将不起作用。您可能会使缓存的要求无效,以便对其进行适当的测试。

For example the following won't have the env stubbed:

例如,以下内容不会出现env存根化:

const nodeEnv = process.env.NODE_ENV;

const fnToTest = () => {
   nodeEnv ...
}

#3


3  

In a spec-helper.coffee or something similar where you set up your sinon sandbox, keep track of the original process.env and restore it after each test, so you don't leak between tests and don't have to remember to reset every time.

spec-helper。咖啡或类似的东西,你设置你的sinon沙盒,跟踪原始进程。env并在每次测试之后恢复它,这样您就不会在测试之间泄漏,也不必记住每次都要重置。

_ = require 'lodash'
sinon = require 'sinon'

beforeEach ->
    @originalProcessEnv = _.cloneDeep process.env

afterEach ->
    process.env = _.cloneDeep @originalProcessEnv

In your test, use process.env as normal.

在您的测试中,使用过程。env正常。

it 'does something based on an env var', ->
    process.env.FOO = 'bar'

#1


42  

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.env must be a string. So, if you need a particular value in your test:

从我对过程的理解。env,在设置它的属性时,你可以像对待其他变量一样对待它。但是要记住,每一个过程中的价值。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;
   });

#2


14  

I was able to get process.env to be stubed properly in my unit tests by cloning it and in a teardown method restoring it.

我能得到过程。在我的单元测试中,通过克隆env并使用拆解方法来恢复它,来正确地进行测试。

Example using 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.

记住,这只会在过程中起作用。env仅在您正在测试的函数中被读取。例如,如果您正在测试的代码读取变量并在闭包中使用它,那么它将不起作用。您可能会使缓存的要求无效,以便对其进行适当的测试。

For example the following won't have the env stubbed:

例如,以下内容不会出现env存根化:

const nodeEnv = process.env.NODE_ENV;

const fnToTest = () => {
   nodeEnv ...
}

#3


3  

In a spec-helper.coffee or something similar where you set up your sinon sandbox, keep track of the original process.env and restore it after each test, so you don't leak between tests and don't have to remember to reset every time.

spec-helper。咖啡或类似的东西,你设置你的sinon沙盒,跟踪原始进程。env并在每次测试之后恢复它,这样您就不会在测试之间泄漏,也不必记住每次都要重置。

_ = require 'lodash'
sinon = require 'sinon'

beforeEach ->
    @originalProcessEnv = _.cloneDeep process.env

afterEach ->
    process.env = _.cloneDeep @originalProcessEnv

In your test, use process.env as normal.

在您的测试中,使用过程。env正常。

it 'does something based on an env var', ->
    process.env.FOO = 'bar'