Add mock.property to node:test #8383
<!-- durable-reviewer:review run=75adf01c-9508-48a1-a3b2-4fd2e1f7d58e -->
✅ Approved — A faithful port of Node's mock.property with matching tests; no correctness, security, performance or documentation issues found.
<sub>The decision is advisory: this review is a comment and never approves, blocks or requests changes on the pull request.</sub>
---
<details>
<summary>✅ All checks passed</summary>
🔒 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.
</details>
---
<sub>Reviewed commit: d22988e7</sub>
Approved these changes.
Approved these changes.
ryanking13
littledivy
Node v24.3.0 added
mock.property(object, propertyName[, value])to thenode:testMockTracker, but workerd's port only hasfn,method,getterandsetter, so code that mocks a plain data property fails withmock.property is not a function.In this patch:
MockPropertyContextandMockTracker.propertyare ported from Node'slib/internal/test_runner/mock/mock.js, withaccesses,accessCount(),mockImplementation(),mockImplementationOnce(),resetAccesses()andrestore().reset()andrestoreAll()restore property mocks too.mock.propertytests fromtest-runner-mocking.jsare ported. The one that relies on per-testt.mockauto-restore is replaced with arestoreAll()/reset()check, since workerd'snode:testhas no per-test context.| | Before | After |
|---|---|---|
|
mock.property(obj, 'foo', 42)|TypeError: mock.property is not a function| proxy whereobj.foo === 42||
prop.mock.accessCount()| n/a |1after one read ||
mock.restoreAll()| n/a |obj.fooback to its original value |This adds a method that is only reachable through
node:testundernodejs_compat, so it ships without a compatibility flag.