-
Notifications
You must be signed in to change notification settings - Fork 4.7k
Fix crash when constructing a mock function whose implementation returns a primitive #31375
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
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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.
🟣 Not a regression, but worth noting while we're here:
mock.instancesis still never populated —fn->instancesis declared, cleared, GC-visited and exposed viagetInstances(), but nothing ever pushes to it. In Jest,new mockFn()pushes the constructedthisontomock.instances(it's the canonical API for tracking instances), and the new construct path now hasthisObjectin hand, so this would be the natural place to wire it up. The new test asserts onnoImpl.mock.contexts[0]rather thannoImpl.mock.instances[0], which sidesteps the gap.Extended reasoning...
What the gap is. In Jest,
mockFn.mock.instancesis the documented API for tracking objects created by calling a mock withnew— eachnew mockFn()pushes the constructedthisonto that array. Bun declares the backing storage for this (mutable JSC::WriteBarrier<JSC::JSArray> instances), clears it inclear(), visits it invisitAdditionalChildrenInGCThread, lazily creates it ingetInstances(), and wires it into the mock object structure at offset 2 — but nothing in the file ever pushes a value into it. Grepping the whole ofJSMockFunction.cppforinstancesshows only declarations, clears, visits, and reads; there is nopush/initializeIndex/putDirectIndextargetingfn->instances.Code path.
jsMockFunctionCallrecords each invocation by pushing tofn->calls,fn->contexts,fn->invocationCallOrder, andfn->returnValues, but notfn->instances. The newjsMockFunctionConstructcreatesthisObject(exactly the value Jest pushes ontomock.instances), then delegates tojsMockFunctionCallviacallframe->setThisValue(thisObject). So after construction,mock.contextscontains the new instance butmock.instancesremains empty.Why nothing prevents it. The only thing that could populate
mock.instancesis an explicit push, and there isn't one anywhere in the file. The new test inmock-fn.test.jsassertsexpect(noImpl.mock.contexts[0]).toBe(instance)rather thanexpect(noImpl.mock.instances[0]).toBe(instance), so the test suite doesn't catch the omission. Under real Jest (which this test file is documented to be runnable against),mock.instances[0]would equalinstance— the cross-runner contract is satisfied only because the test avoids checking it.Step-by-step proof.
const Fn = jest.fn(); const inst = new Fn();jsMockFunctionConstructruns, allocatesthisObject, sets it as the call frame'sthis, and callsjsMockFunctionCall.jsMockFunctionCallpushesthisObjectontofn->contexts(soFn.mock.contexts === [inst]), pushes[]ontofn->calls, pushes a result ontofn->returnValues, pushes an id ontofn->invocationCallOrder— and returns.fn->instances.Fn.mock.instanceslazily resolves viagetInstances()to a fresh empty array.Fn.mock.instances→[]in Bun,[inst]in Jest.Impact. This is a pre-existing Jest-compat gap, not a regression —
mock.instanceswas never populated before this PR either, and the PR's stated purpose (fixing theisCell()crash) is achieved correctly without it. But the PR explicitly adds the construct path "to match Jest, where mocks are plain JS functions", andmock.instancesis the Jest API specifically for construct calls, so it's directly adjacent. User code that follows the Jest docs (expect(MockCtor.mock.instances[0]).toBe(...)) will still see an empty array under Bun.Fix. In
jsMockFunctionConstruct, after creatingthisObject(and before or after delegating tojsMockFunctionCall), pushthisObjectontofn->instancesusing the same pattern used forcontextsinjsMockFunctionCall(push if the array exists, otherwise create a 1-element array andfn->instances.set(...)). Then the new test could assertnoImpl.mock.instances[0]instead of (or in addition to)noImpl.mock.contexts[0]. Non-blocking — fine as a follow-up.