Skip to content

Add mock.property to node:test - #8383

Merged
jasnell merged 1 commit into
mainfrom
littledivy/2026-11-12-mock-property
Nov 12, 2026
Merged

jasnell merged 1 commit into
mainfrom
littledivy/2026-11-12-mock-property

Conversation

@littledivy

Copy link
Copy Markdown
Member

Node v24.3.0 added mock.property(object, propertyName[, value]) to the node:test MockTracker, but workerd's port only has fn, method, getter and setter, so code that mocks a plain data property fails with mock.property is not a function.

In this patch:

  • MockPropertyContext and MockTracker.property are ported from Node's lib/internal/test_runner/mock/mock.js, with accesses, accessCount(), mockImplementation(), mockImplementationOnce(), resetAccesses() and restore().
  • Each tracked mock now stores its own restore function, as in Node, so reset() and restoreAll() restore property mocks too.
  • Node's mock.property tests from test-runner-mocking.js are ported. The one that relies on per-test t.mock auto-restore is replaced with a restoreAll()/reset() check, since workerd's node:test has no per-test context.
Before After
mock.property(obj, 'foo', 42) TypeError: mock.property is not a function proxy where obj.foo === 42
prop.mock.accessCount() n/a 1 after one read
mock.restoreAll() n/a obj.foo back to its original value

This adds a method that is only reachable through node:test under nodejs_compat, so it ships without a compatibility flag.

Node v24.3.0 added mock.property(object, propertyName[, value]) to the
node:test MockTracker, but workerd's port only has fn, method, getter
and setter. Code that spies on plain data properties fails with
"mock.property is not a function".

In this patch we:
- port MockPropertyContext and MockTracker.property from Node, with
  accesses, accessCount, mockImplementation, mockImplementationOnce,
  resetAccesses and restore
- store each tracked mock with its own restore function, as Node does,
  so reset() and restoreAll() restore property mocks too
- port Node's mock.property tests from test-runner-mocking.js

This adds a method to an existing object that is only reachable
through node:test, which requires nodejs_compat. It ships without a
compatibility flag: new node: exports have been accepted without one,
and a Worker that does not call mock.property sees no change.
@littledivy
littledivy requested review from a team as code owners November 12, 2026 04:38

@clanky-the-clanker clanky-the-clanker Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

✅ Approved — A faithful port of Node's mock.property with matching tests; no correctness, security, performance or documentation issues found.

The decision is advisory: this review is a comment and never approves, blocks or requests changes on the pull request.


✅ All checks passed

🔒 Security: ✅ LGTM

No security concerns. This is test-runner instrumentation that can only be reached through node:test.


📖 AGENTS.md: ✅ LGTM

No issues found.


📋 Code Quality: ✅ LGTM

No issues found. The per-mock restore function is wired up the same way for function mocks and property mocks.


⚡ Performance: ✅ LGTM

No performance issues. Recording an access record and stack for each property access is opt-in test instrumentation, and resetAccesses() clears it.


📝 Documentation: ✅ LGTM

No issues found.


Reviewed commit: d22988e7

@jasnell
jasnell merged commit f92e412 into main Nov 12, 2026
23 of 25 checks passed
@jasnell
jasnell deleted the littledivy/2026-11-12-mock-property branch November 12, 2026 15:38
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.

3 participants