Skip to content

Fix crash when constructing a jest.fn() mock#30641

Closed
robobun wants to merge 1 commit into
mainfrom
farm/f4798f68/mock-fn-construct
Closed

Fix crash when constructing a jest.fn() mock#30641
robobun wants to merge 1 commit into
mainfrom
farm/f4798f68/mock-fn-construct

Fix crash when constructing a jest.fn() mock

281711e
Select commit
Loading
Failed to load commit list.
Claude / Claude Code Review completed May 13, 2026 in 13m 1s

Code review found 1 important issue

Found 5 candidates, confirmed 2. See review comments for details.

Details

Severity Count
🔴 Important 1
🟡 Nit 1
🟣 Pre-existing 0
Severity File:Line Issue
🔴 Important src/jsc/bindings/JSMockFunction.cpp:850 new fn() ignores user-assigned fn.prototype when newTarget === fn
🟡 Nit src/jsc/bindings/JSMockFunction.cpp:854-866 mock.instances not populated on non-construct calls (Jest divergence)

Annotations

Check failure on line 850 in src/jsc/bindings/JSMockFunction.cpp

See this annotation in the file changed.

@claude claude / Claude Code Review

new fn() ignores user-assigned fn.prototype when newTarget === fn

The `newTargetObject == fn` fast-path skips `createSubclassStructure` and hard-codes `objectStructureForObjectConstructor()` (i.e. `Object.prototype`), so a user-assigned `fn.prototype` is ignored when constructing directly via `new fn()` — yet honored via `Reflect.construct(fn, [], Other)`. Since `createSubclassStructure` already falls back to `baseStructure` when `newTarget.prototype` isn't an object, you can drop the ternary and call it unconditionally.

Check warning on line 866 in src/jsc/bindings/JSMockFunction.cpp

See this annotation in the file changed.

@claude claude / Claude Code Review

mock.instances not populated on non-construct calls (Jest divergence)

In Jest, `mock.instances` is pushed on **every** invocation (in lockstep with `mock.calls`/`mock.contexts`), not only on `[[Construct]]` — see `jest-mock`'s wrapper, which runs `mockState.instances.push(this)` unconditionally. With this change, mixing `new fn()` and `fn()` produces arrays where `instances[i]` no longer corresponds to `calls[i]`, and the new test "calling as a function does not populate instances" will fail when this file is run under Jest (the file header says it's meant to be c