Repository navigation
test_runner: support mocking non-existent modules - #62298
geeksilva97 wants to merge 2 commits into
Conversation
|
Review requested:
|
5d7b662 to
c1881e2
Compare
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
That's an edge case I could not think of. Would you have an example to illustrate the issue?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Thank you so much for the example, @ljharb . It makes so much sense.
Codecov Report✅ All modified and coverable lines are covered by tests. 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
🚀 New features to boost your workflow:
|
|
This pull request has been marked as stale due to 90 days of inactivity. |
|
This pull request has been automatically closed after 30 days of inactivity following its stale status (no activity for a total of 120 days). |
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>
Fixes #55891