Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Property stubbing on process.env in node throws #188

Closed
mantoni opened this issue Oct 17, 2012 · 9 comments
Closed

Property stubbing on process.env in node throws #188

mantoni opened this issue Oct 17, 2012 · 9 comments

Comments

@mantoni
Copy link
Member

mantoni commented Oct 17, 2012

process.env does not implement hasOwnProperty.

Reproduce on the node repl:

sinon.andbox.create().stub(process.env, 'HELL', 'froze over')
TypeError: Property 'hasOwnProperty' of object #<error> is not a function
    at Object.stub (/Users/max/projects/hub.js/node_modules/sinon/lib/sinon/collection.js:96:33)

Interestingly, stubbing the entire environment works.

sinon.andbox.create().stub(process, 'env', { 'HELL', 'froze over' })
{ restore: [Function] }
@cjohansen
Copy link
Contributor

/tmp  npm install [email protected]
.../tmp  node
> require("sinon").sandbox.create().stub(process.env, "PATH", "meh");
{ restore: [Function] }

@mantoni
Copy link
Member Author

mantoni commented Oct 19, 2012

So I can start measuring your response time with high resolution timers?

@cjohansen
Copy link
Contributor

Hehe. I was stuck and tired and figured I might as well fix this issue instead :)

@davetapley
Copy link

Hmm, I guess this doesn't work anymore?
I tried sinon.stub process.env, 'CORE_DATABASE_URL', 'db://my_db', but I get:

TypeError: Custom stub should be function

I've spent a while trying to find an alternative way to stub process.env, but I'm not having much luck 😒

@mantoni
Copy link
Member Author

mantoni commented Apr 20, 2014

Use a sandbox. That's the only supported way to stub a property.

-Max

Am 18.04.2014 um 22:14 schrieb Dave Tapley [email protected]:

Hmm, I guess this doesn't work anymore?
I tried sinon.stub process.env, 'CORE_DATABASE_URL', 'db://my_db', but I get:

TypeError: Custom stub should be function
I've spent a while trying to find an alternative way to stub process.env, but I'm not having much luck


Reply to this email directly or view it on GitHub.

@LukasBombach
Copy link

I use a sandbox and also run into issues. To explain this: I require my AWS key and secret to be in my ENV vars. I have a function that tells me when a any of them are missing. So I want to set the AWS_SECRET_ACCESS_KEY to test if my function tells me the AWS_ACCESS_KEY_ID is still missing:

  describe('when I need the missing keys in the ENV vars', () => {
    let sandbox;
    beforeEach(() => { sandbox = sinon.sandbox.create(); });
    afterEach(() => { sandbox.restore(); });
    it('should return the key/value pair if AWS_ACCESS_KEY_ID is missing', () => {
      sandbox.stub(process.env, 'AWS_SECRET_ACCESS_KEY', 'some access key');
      S3Client.getMissingKeysInEnv().length.should.equal(1);
      // other assertions
    });
  });

what I get is

 TypeError: Cannot stub non-existent own property AWS_SECRET_ACCESS_KEY

@Siilwyn
Copy link

Siilwyn commented Feb 9, 2017

Workaround is stubbing the whole environment, like:

describe('when I need the missing keys in the ENV vars', () => {
  const sandbox = sinon.sandbox.create();

  afterEach(() => sandbox.restore());

  it('should have mocked environment', () => {
    sandbox.stub(process, 'env', {SOME_KEY: 'some value'});
  });
});

@Siilwyn
Copy link

Siilwyn commented Aug 3, 2017

Since version 3 I can't get it to work, could you shed some light on this @cjohansen?

@fatso83
Copy link
Contributor

fatso83 commented Aug 4, 2017

@Siilwyn Please reopen as a new issue. You can link to this discussion in the description field, but it is highly probable this is related to something entirely else, like #1512.

Remember to provide a minimal snippet showing what worked before 3.0, but not after.

@sinonjs sinonjs locked and limited conversation to collaborators Aug 4, 2017
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants