Skip to content

stub.onCall(#).yields(x).returns(y) returns undefined instead of y #1736

@nfriedly

Description

@nfriedly

I just updated some code from sinon 1.x to 4.x to take advantage of the newer promise features, and tests that had been passing started failing. These tests were stubbing http.request, which both returns a value and calls a callback. The return req value changed to undefined, and so code that called req.on(...) started failing.

  • Sinon version : 4.4.5
  • Environment : Node.js 8.9.4, macOS
  • Example: see below
  • Other libraries you are using: just sinon + mocha in the example below

What did you expect to happen?
I expect the stub to return whatever I pass into .returns()

What actually happens
When onCall() is used, it returns undefined

How to reproduce

var assert = require('assert')
var sinon = require('sinon');

describe('sinon yield + returns', function() {
    // this passes
    it('works without without onCall', () => {
        const stub = sinon.stub().yields('yield val').returns('return val');
        const cb = sinon.spy();
        const ret = stub(cb);
        assert.equal(ret, 'return val');
        assert.deepEqual(cb.args, [['yield val']]);
    });
    // this fails
    it('should return the correct value with onCall', () => {
        const stub = sinon.stub().onCall(0).yields('yield val').returns('return val');
        const cb = sinon.spy();
        const ret = stub(cb);
        assert.equal(ret, 'return val'); // throws because ret is undefined
        assert.deepEqual(cb.args, [['yield val']]);
    });
});

save the above example to a test.js file, install sinon, and run npx mocha test.js

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions