Skip to content

fix(ext/node): implement mock.getter, mock.setter, mockImplementation in node:test - #33755

Merged
bartlomieju merged 3 commits into
mainfrom
fix/node-test-mock-improvements
May 1, 2026
Merged

bartlomieju merged 3 commits into
mainfrom
fix/node-test-mock-improvements

Conversation

@bartlomieju

Copy link
Copy Markdown
Member

Summary

Implements several missing mock APIs in the node:test polyfill:

  • MockFunctionContext.mockImplementation() — change the mock implementation after creation
  • MockFunctionContext.mockImplementationOnce(impl, onCall?) — set a one-time implementation for a specific call index
  • mock.getter(object, name, impl?, options?) — mock a getter on an object (was notImplemented)
  • mock.setter(object, name, impl?, options?) — mock a setter on an object (was notImplemented)
  • findPropertyDescriptor() — walk the prototype chain to find property descriptors, enabling mocking of inherited methods
  • mock.method() restore now uses ObjectDefineProperty instead of simple assignment, correctly restoring getters/setters/non-writable properties
  • Handle overloaded mock.fn(options) and mock.fn(original, options) call signatures matching Node.js API

Test plan

  • node_test_module spec tests pass (3/3)
  • node_test_mock spec test passes
  • tools/format.js clean
  • tools/lint.js --js clean

… in node:test

Implements several missing mock APIs in the node:test polyfill:

- `MockFunctionContext.mockImplementation()` to change the mock impl
- `MockFunctionContext.mockImplementationOnce()` for per-call overrides
- `mock.getter()` and `mock.setter()` (were `notImplemented`)
- `findPropertyDescriptor()` for prototype-chain-aware property lookup
- `mock.method()` restore via `ObjectDefineProperty` instead of simple
  assignment, correctly restoring getters/setters/non-writable properties
- Overloaded `mock.fn(options)` and `mock.fn(original, options)` signatures

@fibibot fibibot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Substantive — fills in mock.getter/mock.setter (previously notImplemented), adds mockImplementation/mockImplementationOnce, and refactors mock.method to walk the prototype chain via findPropertyDescriptor plus restore via ObjectDefineProperty (preserving the original descriptor's enumerable/configurable/writable — actually slightly more faithful than Node's ReflectSet restore, which loses descriptor flags). The overload normalization for mock.fn(options) / mock.fn(original, options) matches Node's signature.

Three correctness items inline: one real edge-case bug (mockImplementationOnce + times), two validation-shape divergences from Node. CI mostly pending (45 pass / 21 pending / 0 fail) — holding COMMENT per the CI rule. Substance is otherwise tracking Node's lib/internal/test_runner/mock/mock.js well.

Comment thread ext/node/polyfills/testing.ts Resolved
Comment thread ext/node/polyfills/testing.ts Resolved
Comment thread ext/node/polyfills/testing.ts Resolved
- Fix _nextImpl() being consumed before _shouldMock() gate, which
  silently lost one-time impls when times limit was reached
- Validate onCall parameter in mockImplementationOnce as non-negative integer
- Use validateFunction/validateInteger from internal/validators for
  proper ERR_INVALID_ARG_TYPE errors matching Node's error shape

@fibibot fibibot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All three of my v1 inline concerns addressed cleanly in 84a06af. Substance walks:

  • _nextImpl() ordering (the real edge-case bug): now called inside the _shouldMock() true branch. The once-impl stays in the map until it would actually be used; no more silent consumption when the times limit is reached. ✓
  • onCall validation: validateInteger(onCall, "onCall", 0) from internal/validators.mjs — Node's exact validator. Bogus values now throw the right error code. ✓
  • implementation validation: bare TypeError replaced with validateFunction(implementation, "implementation") in both mockImplementation and mockImplementationOnce. Now produces ERR_INVALID_ARG_TYPE matching Node. ✓

The earlier method-mock validation (Cannot mock property '...' etc.) wasn't touched in this commit — that was a non-blocking note about other locations using bare TypeError. Fine to leave for follow-up.

CI: 63 pass, 24 pending, 0 fail. Holding COMMENT per the CI rule. Substance is otherwise ready; would flip to APPROVE on green CI.

Resolve conflict in testing.ts primordial imports: keep both
MapPrototype*/ObjectGetOwnPropertyDescriptor/ObjectGetPrototypeOf
from PR and ErrorPrototype/ObjectPrototypeIsPrototypeOf from main.
@bartlomieju
bartlomieju enabled auto-merge (squash) May 1, 2026 07:27
@bartlomieju
bartlomieju merged commit 1d36a12 into main May 1, 2026
136 checks passed
@bartlomieju
bartlomieju deleted the fix/node-test-mock-improvements branch May 1, 2026 08:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants