fix(spy): keep prototype methods on instances created from mocked classes#10584
Closed
naveentehrpariya wants to merge 1 commit into
Closed
fix(spy): keep prototype methods on instances created from mocked classes#10584naveentehrpariya wants to merge 1 commit into
naveentehrpariya wants to merge 1 commit into
Conversation
Member
|
there is already open PR #10561 |
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.
Description
Resolves #10553
When a mock is constructed, the instance is created with
Reflect.construct(implementation, args, new.target), wherenew.targetis the mock itself — so the instance's prototype ismock.prototype, and any methods defined on the implementation's prototype are lost. This breaks the documentedmockImplementation(class { ... })pattern:The same root cause also affects:
vi.spyOn(obj, 'Class')with no mock implementation — constructed instances lose the original class's methods,vi.fn(class { ... })— instances lose the class's prototype methods.(Class fields work because the constructor installs them as own properties; only prototype members are lost.)
Fix
Before constructing, link the resolved implementation's prototype into the chain:
Object.setPrototypeOf(mock.prototype, implementation.prototype). The instance's chain becomesinstance → mock.prototype → implementation.prototype → ..., which preserves the existing pinned semantics:instance instanceof mockstaystrue(docs promise,mocks constructorstest),mock.prototype(e.g.Dog.prototype.speak = vi.fn()) keep priority,mock.prototype,mockImplementationOncewith different classes resolves each class's prototype per construction.spies on classestest assertedexpect(new Spy()).not.toBeInstanceOf(MockExample). Prototype methods can only resolve through the prototype chain, so makinginstance.methodwork necessarily putsMockExample.prototypein the chain, andinstanceof MockExamplebecomestrue. I updated that assertion — it also matches how a realclass MockClass extends Originalinstance would behave. If hiding the implementation class is intentional, an alternative is copying prototype descriptors instead of linking, but that breaksmockImplementationOncesequencing and user-assigned method priority in subtle ways.Tests
extends+ override), plainvi.spyOnon a class,vi.fn(class),mock.prototype-assigned method priority, andmockImplementationOncewith different classes.test/unitfull suite: 2425 passed, type checks clean. (Browser-dependent e2e specs were skipped locally — no Playwright browsers installed.)Please don't delete this checklist! Before submitting the PR, please make sure you do the following:
pnpm-lock.yamlunless you introduce a new test example.Tests
pnpm test:ci. (test/unitprojects run locally; browser e2e requires Playwright binaries not available in my environment)Documentation
Changesets