-
Notifications
You must be signed in to change notification settings - Fork 4.7k
bun:test: return an object when constructing a mock function #31373
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🟣 Pre-existing, not introduced here:
mock.instancesis exposed on the mock object but is never populated —jsMockFunctionCallImplpushes tocalls/contexts/invocationCallOrder/returnValuesbut never tofn->instances. Now that mocks have a real[[Construct]]path, users will reach forfn.mock.instances[0](the canonical Jest API for constructor calls) and find it empty. Might be worth pushingthisValueintoinstancesalongsidecontextswhile you're in here, but it shouldn't block this crash fix.Extended reasoning...
What the bug is
JSMockFunctiondeclares aWriteBarrier<JSArray> instancesfield, lazily creates it viagetInstances(), exposes it at offset 2 of the mock-object structure (themock.instancesproperty), clears it inclear(), and visits it in GC. But nothing injsMockFunctionCallImpl(or anywhere else in the file) ever pushes to it. Sofn.mock.instancesis always an empty array regardless of how many times the mock is called or constructed.In Jest,
mock.instancesrecords thethisvalue for every invocation — fornew fn()that's the newly created instance, and for plain calls it's whateverthiswas bound to. It is the documented way to assert on constructed instances (e.g.expect(Foo.mock.instances[0]).toBe(a)).Code path
Walk through
const fn = jest.fn(); const inst = new fn();after this PR:jsMockFunctionConstructruns, buildsthisObjectfromnew.target.prototype.jsMockFunctionCallImpl(globalObject, callframe, thisObject).fn->callsgets[ [] ],fn->contextsgets[thisObject],fn->invocationCallOrdergets[n], andfn->returnValuesgets[{type:'return', value:undefined}].fn->instances...->push(...)orinstances->initializeIndex(...)— grep forinstancesin the file and you'll find only the declaration,clear(),getInstances(), the GC visit, and the structure offset.jsMockFunctionConstructreturnsthisObject.fn.mock.instancesis still[];fn.mock.contextsis[inst].The PR's own test asserts
expect(fn.mock.contexts[0]).toBe(instance)rather thanfn.mock.instances[0], which is consistent withinstancesbeing empty.Why existing code doesn't prevent it
There simply is no write site. The array is wired up end-to-end on the read side (lazy creation, structure slot, GC) but the population step was never implemented. Before this PR it mattered less because constructing a mock didn't have proper semantics anyway (it returned primitives / asserted in debug). Now that
[[Construct]]is real, the gap becomes user-visible in the exact scenario this PR enables.Impact
Tests ported from Jest that do things like:
will fail under
bun:testeven thoughnew Ctor()now behaves correctly. The workaround ismock.contexts, but that's not what Jest users typically reach for when testing constructors.How to fix
In
jsMockFunctionCallImpl, mirror thecontextspush forinstances: after pushingthisValuetofn->contexts, also push it tofn->instances(lazily creating the array the same way). Jest recordsthisininstancesfor every call, not just constructor calls, so doing it unconditionally in the shared impl matches upstream semantics and keepsinstances.length === calls.length.Severity
Pre-existing.
instanceswas never populated before this PR either; this change just makes the omission relevant. It shouldn't block landing the crash fix, but it's a natural follow-up (or a one-line addition here if the author prefers).