How to stub process.env in node.js?

后端 未结 6 1608
梦毁少年i
梦毁少年i 2021-02-02 04:56

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

var sinon = require(\'sinon\');
var stub = sinon.stub(process.env, \'FOO\', \'bar\');
<         


        
6条回答
  •  天涯浪人
    2021-02-02 05:39

    With sinon you can stub any variable like this.

     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/


    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');
    

提交回复
热议问题