Repository navigation
fix(spy): record the actual instance in thisValues when spying on a constructor - #2786
Open
saadkhantareen wants to merge 1 commit into
Open
saadkhantareen wants to merge 1 commit into
saadkhantareen wants to merge 1 commit into
Conversation
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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 throwawaythisobject created for the proxy invocation, which the constructor never ran on. Properties and methods assigned by the constructor were missing fromthisValues[0], andspy.calledOn(instance)did not match the instance the caller received.Changes
src/sinon/proxy-invoke.js: in thecalledWithNewpath, the constructor is invoked through a bound construction, which runs it on a new instance rather than on thethishanded to the proxy. When that construction produces an instance of the spied constructor, record it as the call'sthisValuefor the proxy and its matching fakes, replacing the discarded proxythis. A constructor that returns an unrelated object keeps the originalthisValue, since that object was never the constructor'sthis(this preserves the existingcalledWithNewbehaviour for such constructors).test/src/spy-test.js: two regression tests under.thisValuescovering 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-sensitiveSandbox > warns of potential leak when many fakes are createdtimeout; unmodifiedmainshows 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