Spy's thisValue not pointing to instance when spying constructor
Open
Nobody has claimed this yet.
Bug
Difficulty: Medium
hacktoberfest
Help wanted
pinned
Regression
- Dominant language
- JavaScript
- Stars
- 9.8k
- Forks
- 809
- PR merge metrics
- No merged PRs in 30d
Description
This snippet works fine with Sinon 2.0.0 but fails with Sinon 4.2.2:
it('returns correct thisValue', function() {
window.SomeClass = function() {
this.counter = 0;
this.inc = function() {
this.counter++;
};
this.getCounter = function() {
return this.counter;
};
};
var spy = sinon.spy(window, 'SomeClass');
var some = new window.SomeClass();
some.inc();
expect(spy.thisValues[0].counter).toEqual(1);
expect(spy.thisValues[0].getCounter()).toEqual(1);
some.inc();
expect(spy.thisValues[0].counter).toEqual(2);
expect(spy.thisValues[0].getCounter()).toEqual(2);
});
It looks like the thisValues[0] object is a proxy and does not refer to the actual instance of some.
This is the error with Sinon 4.2.2:
Expected undefined to equal 1
TypeError: undefined is not a function (evaluating 'spy.thisValues[0].getCounter()')
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Reproduce the provided constructor-spy snippet with Sinon 4.2.2 and inspect how constructor calls populate spy.thisValues. Verify that the recorded value is the actual constructed instance, then rerun the snippet and confirm all counter and getCounter assertions pass.
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
- Mostly clear
- Newbie friendliness
- 42/100