Skip to content

fix(spy): record the actual instance in thisValues when spying on a constructor - #2786

Open
saadkhantareen wants to merge 1 commit into
sinonjs:mainfrom
saadkhantareen:fix/spy-constructor-this-value
Open

saadkhantareen wants to merge 1 commit into
sinonjs:mainfrom
saadkhantareen:fix/spy-constructor-this-value

Conversation

@saadkhantareen

Copy link
Copy Markdown

Summary

Fixes #1683. When a function or class constructor is spied on and called with new, spy.thisValues[0] did not point to the created instance: it held the throwaway this object created for the proxy invocation, which the constructor never ran on. Properties and methods assigned by the constructor were missing from thisValues[0], and spy.calledOn(instance) did not match the instance the caller received.

Changes

  • src/sinon/proxy-invoke.js: in the calledWithNew path, the constructor is invoked through a bound construction, which runs it on a new instance rather than on the this handed to the proxy. When that construction produces an instance of the spied constructor, record it as the call's thisValue for the proxy and its matching fakes, replacing the discarded proxy this. A constructor that returns an unrelated object keeps the original thisValue, since that object was never the constructor's this (this preserves the existing calledWithNew behaviour for such constructors).
  • test/src/spy-test.js: two regression tests under .thisValues covering a spied function constructor (the case from Spy's thisValue not pointing to instance when spying constructor #1683, including later mutation of the instance) and a spied ES class constructor.

Tests

  • npx mocha test/src/spy-test.js --grep "created instance": 2 passing. Both tests fail on the unmodified code.
  • npx mocha test/src/spy-test.js test/src/stub-test.js test/src/mock-test.js: 749 passing, 2 pending, 0 failing.
  • npm run test-node: 1561 passing, 12 pending, 1 failing. The single failure is the timing-sensitive Sandbox > warns of potential leak when many fakes are created timeout; unmodified main shows the same single failure (1559 passing), and the test passes in isolation with this change (3 passing).
  • npx eslint --max-warnings 0 src/sinon/proxy-invoke.js test/src/spy-test.js: no errors.
  • npx prettier --check src/sinon/proxy-invoke.js test/src/spy-test.js: all files use Prettier code style.

Fixes #1683

Copilot AI balanced review requested due to automatic review settings October 16, 2026 10:10

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

This branch has not been deployed

No deployments
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 this pull request may close these issues.

Spy's thisValue not pointing to instance when spying constructor

2 participants