Skip to content

test_runner: fix mock.property() restore for accessors - #67310

Open
Han5991 wants to merge 1 commit into
nodejs:mainfrom
Han5991:fix/test-runner-mock-property-accessor
Open

Han5991 wants to merge 1 commit into
nodejs:mainfrom
Han5991:fix/test-runner-mock-property-accessor

Conversation

@Han5991

@Han5991 Han5991 commented Nov 7, 2026 •

Copy link
Copy Markdown
Contributor

MockPropertyContext.restore() spreads the original descriptor and adds a
value field. For an accessor property that is an invalid descriptor, so
restore() throws a TypeError.

With t.mock, the restore runs while the test is being finalized, before it
is reported, so the run hangs with no output:

const { test } = require('node:test');
const fs = require('node:fs');

test('mock fs.promises', (t) => {
  t.mock.property(fs, 'promises', {}); // fs.promises is a getter
});

This restores the original descriptor as-is. Data properties are unaffected,
since their descriptor already holds the original value.

Disclosure: I used a coding agent to help investigate and draft this change. I reproduced the bug on a main build, reviewed the change, and verified that the new tests fail without the fix.

MockPropertyContext.restore() spread the original descriptor and added
a value field. For an accessor property this yields an invalid
descriptor, so restore() threw a TypeError. When the mock was created
with t.mock, the error was thrown while the test was being finalized,
before it was reported, and the run hung with no output.

Restore the original descriptor as-is. For data properties this is
unchanged, since the descriptor already holds the original value.

Signed-off-by: sangwook <rewq5991@gmail.com>
@nodejs-github-bot

Copy link
Copy Markdown
Collaborator

Review requested:

  • @nodejs/test_runner

@nodejs-github-bot nodejs-github-bot added needs-ci PRs that need a full CI run. test_runner Issues and PRs related to the test runner subsystem. labels Nov 7, 2026
@codecov

codecov Bot commented Nov 7, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 90.67%. Comparing base (a43bfb5) to head (784ba56).
⚠️ Report is 3 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main   #67310      +/-   ##
==========================================
- Coverage   93.00%   90.67%   -2.33%     
==========================================
  Files         428      797     +369     
  Lines      195818   279609   +83791     
  Branches    30283    53783   +23500     
==========================================
+ Hits       182117   253543   +71426     
- Misses      13375    16350    +2975     
- Partials      326     9716    +9390     
Files with missing lines Coverage Δ
lib/internal/test_runner/mock/mock.js 97.40% <100.00%> (+0.42%) ⬆️

... and 499 files with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

needs-ci PRs that need a full CI run. test_runner Issues and PRs related to the test runner subsystem.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants