I would like to replace process.env
for testing purposes.
Currently, we are replacing it as follows.Is there a smarter way?
describe('test', function(){
beforeEach(function(){
this.env = process.env;
process.env = {hoge:123};
});
afterEach(function() {process.env=this.env;});
it('works', function() {/*...*/});
});
In the case of function
, I think sinon.stub(obj, 'method', func)
would be good in Sinon.JS, but in the case of property
, I don't know what to do.
How about using the Sandbox API from Sinon.JS?
Below is the sample code.
With this, I could only stub the PATH environment variable.
varsandbox=require('sinon').sandbox.create();
describe('test', function(){
beforeEach(function(){
sandbox.stub(process.env, "PATH", "hoge");
});
afterEach(function(){
sandbox.restore();
});
it('works', function() {/*...*/});
});
© 2024 OneMinuteCode. All rights reserved.