Repository navigation
fix(ext/node): implement mock.getter, mock.setter, mockImplementation in node:test - #33755
Conversation
… 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
left a comment
There was a problem hiding this comment.
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.
- 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
left a comment
There was a problem hiding this comment.
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 thetimeslimit is reached. ✓onCallvalidation:validateInteger(onCall, "onCall", 0)frominternal/validators.mjs— Node's exact validator. Bogus values now throw the right error code. ✓implementationvalidation: bareTypeErrorreplaced withvalidateFunction(implementation, "implementation")in bothmockImplementationandmockImplementationOnce. Now producesERR_INVALID_ARG_TYPEmatching 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.
Summary
Implements several missing mock APIs in the
node:testpolyfill:MockFunctionContext.mockImplementation()— change the mock implementation after creationMockFunctionContext.mockImplementationOnce(impl, onCall?)— set a one-time implementation for a specific call indexmock.getter(object, name, impl?, options?)— mock a getter on an object (wasnotImplemented)mock.setter(object, name, impl?, options?)— mock a setter on an object (wasnotImplemented)findPropertyDescriptor()— walk the prototype chain to find property descriptors, enabling mocking of inherited methodsmock.method()restore now usesObjectDefinePropertyinstead of simple assignment, correctly restoring getters/setters/non-writable propertiesmock.fn(options)andmock.fn(original, options)call signatures matching Node.js APITest plan
node_test_modulespec tests pass (3/3)node_test_mockspec test passestools/format.jscleantools/lint.js --jsclean