FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

test_runner: mock dual-package with conditional exports by maruthang · Pull Request #62943 · nodejs/node · GitHub

/ node Public

test_runner: mock dual-package with conditional exports - #62943

Closed
maruthang wants to merge 2 commits into
nodejs:mainfrom
maruthang:fix-58231-mock-dual-package
Closed

test_runner: mock dual-package with conditional exports#62943
maruthang wants to merge 2 commits into
nodejs:mainfrom
maruthang:fix-58231-mock-dual-package

Conversation

Copy link
Copy Markdown
Contributor

When mock.module() targets a package whose exports field maps
import and require to different files, the ESM resolver and the
CJS resolver disagree on the resolved path. Only the ESM path was
registered in mockMap, so require() of the mocked specifier
bypassed the mock and loaded the real CJS module.

Resolve the specifier through Module._resolveFilename from the
caller's directory in addition to the existing ESM resolution. When
the two paths differ, register the CJS path as a second key in
mockMap and invalidate Module._cache[cjsPath], restoring it on
restore(). Single-resolution packages keep their existing behavior.

Note: I was unable to run the test suite locally (no built out/Release/node available on this Windows host). All four files pass node --check; the resolver-divergence premise was verified empirically against system Node. Looking forward to CI verification.

Fixes: #58231

When `mock.module()` targets a package whose `exports` field maps
`import` and `require` to different files, the ESM resolver and the
CJS resolver disagree on the resolved path. Only the ESM path was
registered in `mockMap`, so `require()` of the mocked specifier
bypassed the mock and loaded the real CJS module.

Resolve the specifier through `Module._resolveFilename` from the
caller's directory in addition to the existing ESM resolution. When
the two paths differ, register the CJS path as a second key in
`mockMap` and invalidate `Module._cache[cjsPath]`, restoring it on
`restore()`. Single-resolution packages keep their existing behavior.

Fixes: nodejs#58231
Signed-off-by: Maruthan G <maruthang4@gmail.com>

Copy link
Copy Markdown
Collaborator

Review requested:

  • @nodejs/test_runner

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 Apr 25, 2026
JakobJingleheimer self-requested a review April 25, 2026 15:49

codecov Bot commented Apr 25, 2026
edited
Loading

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 82.95455% with 15 lines in your changes missing coverage. Please review.
✅ Project coverage is 89.64%. Comparing base (4744070) to head (8c4a8e4).
⚠️ Report is 8 commits behind head on main.

Files with missing lines Patch % Lines
lib/internal/test_runner/mock/mock.js 82.95% 13 Missing and 2 partials ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main   #62943      +/-   ##
==========================================
- Coverage   89.66%   89.64%   -0.03%     
==========================================
  Files         706      706              
  Lines      219370   219489     +119     
  Branches    42065    42095      +30     
==========================================
+ Hits       196694   196751      +57     
- Misses      14586    14638      +52     
- Partials     8090     8100      +10     
Files with missing lines Coverage Δ
lib/internal/test_runner/mock/mock.js 97.53% <82.95%> (-1.28%) ⬇️

... and 49 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.

JakobJingleheimer left a comment
edited
Loading

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

Thanks for this! The test is correct, and I see it's passing.

I'll do a pass on the implementation soon.

I can't remember whether http/s specifiers are still supported (I think that was maybe removed?).

ljharb commented Apr 26, 2026

Copy link
Copy Markdown
Member

It used to be experimental, but i believe it was removed a few majors ago.

Comment thread lib/internal/test_runner/mock/mock.js Outdated

Copy link
Copy Markdown
Contributor Author

Confirming what @ljharb said — --experimental-network-imports (and with it http: / https: ESM specifiers) was dropped in PR #53822 (commit 15a94e67b1c, landed in v22.6.0, July 2024). Network specifiers are no longer resolvable.

For this PR specifically, that means the http: / https: branches in resolveAsCJS (lib/internal/test_runner/mock/mock.js:1011-1012) are dead code in practice: the upstream sharedState.moduleLoader.resolveSync(...) call at line 681 errors out for any network specifier well before resolveAsCJS runs at line 708. They were defensive against a case that can't reach the function.

Happy to drop those two checks as a follow-up cleanup commit, or keep them as belt-and-braces defense — your call. The other branches (isBuiltin, node:, data:) all still guard live cases, so I'd leave those.

Copy link
Copy Markdown
Member

your call

My call is #62943 (comment)

please remove them from

🙂

JakobJingleheimer left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

🙌 Thanks!

JakobJingleheimer added the request-ci Add this label to start a Jenkins CI on a PR. label Aug 8, 2026
JakobJingleheimer moved this to In Progress in Test Runner Board Aug 8, 2026
github-actions Bot removed the request-ci Add this label to start a Jenkins CI on a PR. label Aug 8, 2026

This comment was marked as outdated.

aduh95 added the author ready PRs that have at least one approval, no outstanding review comments, and a CI started. label Aug 17, 2026

This comment was marked as outdated.

Copy link
Copy Markdown
Collaborator

trivikr pushed a commit that referenced this pull request Aug 21, 2026
When `mock.module()` targets a package whose `exports` field maps
`import` and `require` to different files, the ESM resolver and the
CJS resolver disagree on the resolved path. Only the ESM path was
registered in `mockMap`, so `require()` of the mocked specifier
bypassed the mock and loaded the real CJS module.

Resolve the specifier through `Module._resolveFilename` from the
caller's directory in addition to the existing ESM resolution. When
the two paths differ, register the CJS path as a second key in
`mockMap` and invalidate `Module._cache[cjsPath]`, restoring it on
`restore()`. Single-resolution packages keep their existing behavior.

Fixes: #58231
Signed-off-by: Maruthan G <maruthang4@gmail.com>
PR-URL: #62943
Reviewed-By: Aviv Keller <me@aviv.sh>
Reviewed-By: Moshe Atlow <moshe@atlow.co.il>
Reviewed-By: Jacob Smith <jacob@frende.me>

trivikr commented Aug 21, 2026

Copy link
Copy Markdown
Member

Landed in 9d94b65

trivikr closed this Aug 21, 2026
github-project-automation Bot moved this from In Progress to Done in Test Runner Board Aug 21, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

author ready PRs that have at least one approval, no outstanding review comments, and a CI started. needs-ci PRs that need a full CI run. test_runner Issues and PRs related to the test runner subsystem.

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

test-runner: requireing a mocked dual-package gets the original instead of the mock

8 participants


Back | FazBrowse Home | New Git URL