-
Notifications
You must be signed in to change notification settings - Fork 4.7k
fix(bun:test): return an object when constructing a mock function #31365
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.
🟡 Now that
[[Construct]]actually works, it might be worth pushing the constructed instance ontofn->instanceshere sofn.mock.instancesis populated like it is in Jest. Theinstancesarray is already declared, exposed on the mock object (offset 2), cleared inclear(), and GC-visited — it's just never written to, sofn.mock.instancesstays empty afternew fn(). This is pre-existing scaffolding that was never wired up rather than a regression, so feel free to leave it for a follow-up.Extended reasoning...
What's missing
JSMockFunctiondeclares aWriteBarrier<JSArray> instancesfield, lazily creates it ingetInstances(), exposes it as theinstancesproperty at offset 2 of the mock-object structure, clears it inclear(), and visits it invisitAdditionalChildrenInGCThread. But nothing in the file ever pushes to it.jsMockFunctionCallrecordscalls,contexts,invocationCallOrder, andreturnValues, and the newjsMockFunctionConstructsimply delegates tojsMockFunctionCallviaJSC::callwithout separately recording the constructed instance. The result is thatfn.mock.instancesis always an empty array, even afternew fn().Why it matters now
In Jest,
mock.instancesis the canonical API for tracking objects created by constructing a mock —new fn()pushes the resultingthis(or the returned object) ontofn.mock.instances, while plain calls pushundefined. Before this PR the[[Construct]]slot pointed atjsMockFunctionCall, sonew (mock())()either returnedundefined(release) or hit a debug assertion; the emptyinstancesarray was effectively unobservable because construction itself was broken. With this PR construction works correctly and the PR description says it "matches Jest", so users porting Jest tests that readfn.mock.instances[0]will now hit a visible compat gap.Step-by-step
const Fn = jest.fn(function () { this.x = 1 });const a = new Fn();— entersjsMockFunctionConstruct, which buildsthisObject, callsJSC::call(globalObject, fn, callData, thisObject, args)(recordingcalls[0] = [],contexts[0] = thisObject, etc.), and returnsthisObject.fn->instances.Fn.mock.instances→getInstances()lazily allocates an empty array →[].Fn.mock.instances[0] === a.Suggested fix
In
jsMockFunctionConstruct, after computing the final return value (returnValue.isObject() ? returnValue : thisObject), push it ontofn->instancesthe same wayjsMockFunctionCallpushes tocontexts(lazily creating the array on first use). For full Jest parityjsMockFunctionCallwould also pushundefinedfor non-construct calls, but the construct case is the important one and is squarely in scope for this PR.Severity
This is pre-existing unwired scaffolding — the
instancesfield has always been exposed and always been empty — so it is not a regression and shouldn't block the crash fix. Filing as a nit since this PR is the natural place to wire it up, but a follow-up is fine too.