Sinon: Stub doesn't return the correct value when used with new

Created on 26 Jan 2018  路  7Comments  路  Source: sinonjs/sinon

  • Sinon version : 4.2.1
  • Environment : Node 6.10.3

What did you expect to happen?
Stub to return the correct value when used with new.

What actually happens
Returns a function

How to reproduce

const stub = sinon.stub().returns('a value');
console.log(stub()); // 'a value'
console.log(new stub()); // 'functionStub {}'
Bug Medium Help wanted hacktoberfest pinned patch

Most helpful comment

I can reproduce this. I was using a Symbol in returns but after looking at the code only objects are supported. So my workaround is to use an empty object instead of a symbol to indicate the the correct value is passed through some calls.

All 7 comments

In case you want to scratch your own itch, here is the piece of code where I would start looking into it:

https://github.com/sinonjs/sinon/blob/a8171c3441869002bb61e4ef22eb4b72d67da72e/lib/sinon/spy.js#L197-L204

I can reproduce this. I was using a Symbol in returns but after looking at the code only objects are supported. So my workaround is to use an empty object instead of a symbol to indicate the the correct value is passed through some calls.

Is there a way around this since "new" keyword is like default with ES6 can it support stubs themselves with objects..
EG:
const returnStub = sinon.stub(); const stub = sinon.stub().returns(returnStub);

since "new" keyword is like default with ES6 can it support stubs themselves with objects..

I am sorry, but I do not understand what this means. Can you elaborate on what you mean? I especially do not understand what you mean by saying "new is like default with ES6".

Sorry i wrote in jiffy, I was trying to do following

const instanceStub = sinon.stub(); 
const type = sinon.stub();
type.returns(instanceStub);
const instance = new type();
console.log(instance); //prints proxy function, expecting instanceStub

The above failed if instanceStub was a Stub but if instanceStub is normal object then it passed.

I am guessing returnStub is supposed to be instanceStub? In any case, seeing that this issue is over 2 years old, I think it is quite likely that this will only happen when someone that is affected takes a stab at doing it ... Max gave some good hints at where to start a change. We are very friendly and accept pull requests 馃樅

Here's how to start

Corrected code, ohh i saw the date now 馃檪

Was this page helpful?
0 / 5 - 0 ratings

Related issues

JakobJingleheimer picture JakobJingleheimer  路  3Comments

NathanHazout picture NathanHazout  路  3Comments

OscarF picture OscarF  路  4Comments

kbirger picture kbirger  路  3Comments

ljian3377 picture ljian3377  路  3Comments