Skip to content

test_runner: support mocking non-existent modules - #62298

Closed
geeksilva97 wants to merge 2 commits into
nodejs:mainfrom
geeksilva97:mock-virtual-modules
Closed

geeksilva97 wants to merge 2 commits into
nodejs:mainfrom
geeksilva97:mock-virtual-modules

Conversation

@geeksilva97

Copy link
Copy Markdown
Contributor

Fixes #55891

@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 Mar 17, 2026
@geeksilva97
geeksilva97 force-pushed the mock-virtual-modules branch from 5d7b662 to c1881e2 Compare March 17, 2026 17:05
Comment thread lib/internal/test_runner/mock/loader.js Outdated

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.

if it does exist on disk, though, it could be imported/required via other means, so it seems prudent to ensure the actual resolved specifier is still mocked out?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

That's an edge case I could not think of. Would you have an example to illustrate the issue?

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.

Let's say you have an exports specifier "foo": "./foo/index.js" in package pkg.

That can be required, or imported, by any of the following paths relative to the package root:

  • pkg/foo
  • ./foo/index.js
  • ./foo (if it's a CJS file)
  • ./foo/index (if it's a CJS file)

If you don't fully resolve to a list of potential paths and confirm that, in fact, nothing exists in any of them, then shadowing a subset of them would create a scenario where you have more than one source of truth about that resource's contents.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Thank you so much for the example, @ljharb . It makes so much sense.

@codecov

codecov Bot commented Mar 17, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 89.69%. Comparing base (68d7b6f) to head (6f8bafa).
⚠️ Report is 1611 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main   #62298      +/-   ##
==========================================
- Coverage   89.77%   89.69%   -0.08%     
==========================================
  Files         673      676       +3     
  Lines      203820   206777    +2957     
  Branches    39175    39605     +430     
==========================================
+ Hits       182987   185477    +2490     
- Misses      13152    13439     +287     
- Partials     7681     7861     +180     
Files with missing lines Coverage Δ
lib/internal/test_runner/mock/loader.js 99.44% <100.00%> (+0.09%) ⬆️
lib/internal/test_runner/mock/mock.js 98.80% <100.00%> (+0.07%) ⬆️

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

@github-actions

Copy link
Copy Markdown
Contributor

This pull request has been marked as stale due to 90 days of inactivity.
It will be automatically closed in 30 days if no further activity occurs. If this is still relevant, please leave a comment or update it to keep it open.

@github-actions github-actions Bot added the stale Issues and PRs marked stale due to inactivity and scheduled for automatic closure. label Jul 28, 2026
@github-actions

Copy link
Copy Markdown
Contributor

This pull request has been automatically closed after 30 days of inactivity following its stale status (no activity for a total of 120 days).
If this is still relevant, feel free to reopen it or leave a comment with additional details so we can continue the discussion.

@github-actions github-actions Bot closed this Aug 27, 2026
SahinurDEV added a commit to SahinurDEV/node that referenced this pull request Oct 12, 2026
Add a `virtual` option to `mock.module()`. When it is `true` and the
specifier cannot be resolved, the mock is registered under a stable URL
instead of throwing `ERR_MODULE_NOT_FOUND`:

- bare specifiers map to a synthetic `node-test-mock:` URL;
- relative, absolute and file: URL specifiers map to the file: URL they
  would resolve to, so different paths to the same missing file share
  one mock.

The module loader's resolve hook and the CommonJS `Module._load` patch
only fall back to a virtual mock when normal resolution fails with a
"module not found" error, so existing modules are never shadowed. If
the module does exist, `virtual: true` has no effect and the module is
mocked as usual, so every path that resolves to it sees the mock.

Fixes: nodejs#55891
Refs: nodejs#62298
Co-authored-by: Edy Silva <edigleyssonsilva@gmail.com>
Signed-off-by: Sahinur <infosahinur@gmail.com>
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. stale Issues and PRs marked stale due to inactivity and scheduled for automatic closure. test_runner Issues and PRs related to the test runner subsystem.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

--experimental-test-module-mocks is not working as expected

3 participants