sinonjs / sinonjs/sinon

stub#returnsArg supercedes stub#returns even if returns is called later

Open
#440 10 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

pinned
Dominant language
JavaScript
Stars
9.8k
Forks
808
PR merge metrics
No merged PRs in 30d

Description

I'm unfortunately stuck with sinon 1.3.x (yes, I know, way old), so please forgive me if this is in fact addressed in a later release. I found nothing when searching the issues archive, at least.

It looks like calls to returns does not supercede calls to returnsArg. Since (outside of the new onCall(n) interface) calls to returns or returnsArg overwrite earlier calls to the same functions, it seems to me that the returns call should clobber the returnsArg information if it occurs later. This keeps the behavior consistent-- in all cases, whether you're calling returns or returnsArg, you're changing what value you expect to be returned from the stub, so it makes sense to me simply to clobber all return-related state on the stub when calling any of those functions.

Here's a test case (written in QUnit):

test("returns should clobber returnsArg if called later", function (assert) {
    QUnit.expect(1);

    // arrange
    var stub = sinon.stub.returnsArg(0);
    stub.returns("new value");

    // act
    var result = stub("first argument");

    // assert
    assert.strictEqual(result, "new value", "New return value is returned since returns was called last");
});

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the supplied QUnit test and trace the stub#returnsArg and stub#returns entry points in the Sinon source. Confirm the existing return-related behavior and add or run a regression test showing that a later returns call determines the result; done means the test returns "new value" as asserted.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript
Domain
testing-qa
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.