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

Incorrect returnValue when using withArgs() and firstCall or lastCall #1487

Closed
jvanoostveen opened this issue Jul 14, 2017 · 1 comment
Closed

Comments

@jvanoostveen
Copy link

  • Sinon version : 2.3.8 (likely since 2.3.6)
  • Environment : Windows 10, Node 8.1.4
  • Other libraries you are using: sinon-chai

Related to: #1478 and #1476

What did you expect to happen?
It should not matter how the stub is queried, i.e., getCall(0), firstCall or lastCall should all return the same value if there is only one call using withArgs().
In version 2.3.5, this test succeeds, but when updating to any version above it (tested it with 2.3.8), the test will fail.

What actually happens
The test fails when using withArgs() together with firstCall.returnValue or lastCall.returnValue. But somehow, when using getCalls(0).returnValue or returnValues[0], it does work.

How to reproduce
Given the following test case (sorry, it's in TypeScript).
The inject stub will return a different fake component function based on the arguments that are passed. The return value of the inject stub is then used for example to check the arguments passed etc.

import { SinonStub } from 'sinon';
import * as chai from 'chai';
import * as sinon from 'sinon';
import * as sinonChai from 'sinon-chai';

chai.use(sinonChai);
const { expect } = chai;

let testComponentA = (config: object) => ({ render: () => 'test a' });
let testComponentB = (config: object) => ({ render: () => 'test b' });

describe('Sinon firstCall, lastCall test', () => {
  let inject: SinonStub;

  beforeEach(() => {
    let fakeComponent = (variant: string) => {
      return sinon.stub().returns({
        render: sinon.stub().returns(`fake component ${variant}`)
      });
    };
    inject = sinon.stub().throws('Nothing set');
    inject.withArgs(testComponentA).returns(fakeComponent('a'));
    inject.withArgs(testComponentB).returns(fakeComponent('b'));
  });

  it('returnValues', () => {
    let config = { option: 'a' };
    let component = inject(testComponentA)(config);

    expect(inject).to.have.been.calledWith(testComponentA);
    expect(inject).to.not.have.been.calledWith(testComponentB);

    expect(component).to.respondTo('render');
    expect(component.render()).to.equal('fake component a');

    expect(inject.withArgs(testComponentA).returnValues[0]).to.have.been.calledWith(config); // Ok
    expect(inject.withArgs(testComponentA).getCall(0).returnValue).to.have.been.calledWith(config); // Ok

    expect(inject.withArgs(testComponentA).firstCall.returnValue).to.have.been.calledWith(config); // Fails
    expect(inject.withArgs(testComponentA).lastCall.returnValue).to.have.been.calledWith(config); // Fails
    // TypeError: undefined is not a spy or a call to a spy!
    // at assertCanWorkWith (node_modules\sinon-chai\lib\sinon-chai.js:53:19)
    // at Proxy.<anonymous> (node_modules\sinon-chai\lib\sinon-chai.js:98:13)
    // at Proxy.methodWrapper (node_modules\chai\lib\chai\utils\addMethod.js:57:25)
    // at Context.it (sinon-test.ts:39:79)
  });
});
@fatso83
Copy link
Contributor

fatso83 commented Jul 14, 2017

This is a great issue description! Thank you for taking the time to fill it out. The testcase makes an excellent candidate for git bisect to automatically find the regression too!

Will run the test case when given some time.

kbtknr pushed a commit to kbtknr/sinon that referenced this issue Jul 18, 2017
brian-mann added a commit to cypress-io/sinon that referenced this issue Jul 26, 2017
* commit '4a40f80b79f3f74558e2c7c5890cc299ea276984':
  Use property of this, not variable
  Rename variable name
  Fix sinonjs#1487: incorrect withArgs().returnValue
chrisbreiding added a commit to cypress-io/sinon that referenced this issue Jul 27, 2017
chrisbreiding added a commit to cypress-io/sinon that referenced this issue Jul 28, 2017
fatso83 added a commit that referenced this issue Jul 28, 2017
franck-romano pushed a commit to franck-romano/sinon that referenced this issue Oct 1, 2019
franck-romano pushed a commit to franck-romano/sinon that referenced this issue Oct 1, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants