Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 78ac17f

Browse filesBrowse files
marcopiracciniaduh95
authored andcommitted
module: fix coverage of mocked CJS modules imported from ESM
Signed-off-by: marcopiraccini <marco.piraccini@gmail.com> PR-URL: #62133 Fixes: #61709 Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: Marco Ippolito <marcoippolito54@gmail.com> Reviewed-By: Zeyu "Alex" Yang <himself65@outlook.com> Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: Jacob Smith <jacob@frende.me>
1 parent a035bd5 commit 78ac17f
Copy full SHA for 78ac17f

6 files changed

+81-1Lines changed: 81 additions & 1 deletion

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎lib/internal/modules/esm/translators.js‎

Copy file name to clipboardExpand all lines: lib/internal/modules/esm/translators.js
+3-1Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,9 @@ const kShouldNotSkipModuleHooks = { __proto__: null, shouldSkipModuleHooks: fals
107107
* @param {boolean} isMain - Whether the module is the entrypoint
108108
*/
109109
function loadCJSModule(module, source, url, filename, isMain) {
110-
const compileResult = compileFunctionForCJSLoader(source, filename, false /* is_sea_main */, false);
110+
// Use the full URL as the V8 resource name so that any search params
111+
// (e.g. ?node-test-mock) are preserved in coverage reports.
112+
const compileResult = compileFunctionForCJSLoader(source, url, false /* is_sea_main */, false);
111113

112114
const { function: compiledWrapper, sourceMapURL, sourceURL } = compileResult;
113115
// Cache the source map for the cjs module if present.
Collapse file
+9Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
throw new Error('This should never be called');
2+
3+
exports.dependency = function dependency() {
4+
return 'foo';
5+
}
6+
7+
exports.unused = function unused() {
8+
return 'bar';
9+
}
Collapse file
+5Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import { dependency } from './dependency.cjs';
2+
3+
export function subject() {
4+
return dependency();
5+
}
Collapse file
+11Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import { mock, test } from 'node:test';
2+
3+
const dependency = mock.fn(() => 'mock-return-value');
4+
mock.module('../coverage-with-mock/dependency.cjs', { namedExports: { dependency } });
5+
6+
const { subject } = await import('../coverage-with-mock/subject.mjs');
7+
8+
test('subject calls dependency', (t) => {
9+
t.assert.strictEqual(subject(), 'mock-return-value');
10+
t.assert.strictEqual(dependency.mock.callCount(), 1);
11+
});
Collapse file
+31Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
TAP version 13
2+
# Subtest: subject calls dependency
3+
ok 1 - subject calls dependency
4+
---
5+
duration_ms: *
6+
type: 'test'
7+
...
8+
1..1
9+
# tests 1
10+
# suites 0
11+
# pass 1
12+
# fail 0
13+
# cancelled 0
14+
# skipped 0
15+
# todo 0
16+
# duration_ms *
17+
# start of coverage report
18+
# -------------------------------------------------------------------------------
19+
# file | line % | branch % | funcs % | uncovered lines
20+
# -------------------------------------------------------------------------------
21+
# test | | | |
22+
# fixtures | | | |
23+
# test-runner | | | |
24+
# coverage-with-mock | | | |
25+
# subject.mjs | 100.00 | 100.00 | 100.00 |
26+
# output | | | |
27+
# coverage-with-mock-cjs.mjs | 100.00 | 100.00 | 100.00 |
28+
# -------------------------------------------------------------------------------
29+
# all files | 100.00 | 100.00 | 100.00 |
30+
# -------------------------------------------------------------------------------
31+
# end of coverage report
Collapse file
+22Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import * as common from '../common/index.mjs';
2+
import * as fixtures from '../common/fixtures.mjs';
3+
import { spawnAndAssert, defaultTransform, ensureCwdIsProjectRoot } from '../common/assertSnapshot.js';
4+
5+
if (!process.features.inspector) {
6+
common.skip('inspector support required');
7+
}
8+
9+
ensureCwdIsProjectRoot();
10+
await spawnAndAssert(
11+
fixtures.path('test-runner/output/coverage-with-mock-cjs.mjs'),
12+
defaultTransform,
13+
{
14+
flags: [
15+
'--disable-warning=ExperimentalWarning',
16+
'--test-reporter=tap',
17+
'--experimental-test-module-mocks',
18+
'--experimental-test-coverage',
19+
'--test-coverage-exclude=!test/**',
20+
],
21+
},
22+
);

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.