sinonjs / sinonjs/sinon

Error when stubbing a property that does not exist

Open
#2,629 2 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

I'd like a more informative error message for this situation:

        const x = {
            getFoo: () => {
                return "bar"
            }
        }

        sinon.stub(x, 'getFoo').value("baz");

        console.log(x.getFoo()); // TypeError: x.getFoo is not a function
        console.log(typeof x.getFoo); // string

The intent here was to stub x's getFoo method, but the user mistakenly used value to do so. In this situation, it would be more helpful for sinon to error on the stub line with an error message similar to: "x.getFoo is a function, not a getter. Use returns instead of value."

I'm aware this is a breaking change, so I think the user would need to configure some sort of "strict mode" to enable an error message like this.

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 by examining the stub(x, 'getFoo').value('baz') behavior described in the issue and how Sinon distinguishes method stubs from property values. Define how an opt-in strict mode should report this misuse without changing default behavior, and verify that the error points to the stub call with guidance to use returns instead.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript
Domain
testing-qa
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.