Sinon: Property stubbing on process.env in node throws

Created on 18 Oct 2012  Â·  9Comments  Â·  Source: sinonjs/sinon

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] }

Most helpful comment

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

All 9 comments

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

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

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

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 :unamused:

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.

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

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

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

@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.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

stevenmusumeche picture stevenmusumeche  Â·  3Comments

fearphage picture fearphage  Â·  3Comments

stephanwlee picture stephanwlee  Â·  3Comments

sudhirbits picture sudhirbits  Â·  4Comments

OscarF picture OscarF  Â·  4Comments