Skip to content

Commit 6f7b948

Browse files
module: fix coverage of mocked CJS modules imported from ESM
Signed-off-by: marcopiraccini <[email protected]> PR-URL: #62133 Fixes: #61709 Reviewed-By: Matteo Collina <[email protected]> Reviewed-By: Marco Ippolito <[email protected]> Reviewed-By: Zeyu "Alex" Yang <[email protected]> Reviewed-By: Colin Ihrig <[email protected]> Reviewed-By: Jacob Smith <[email protected]>
1 parent 9e89867 commit 6f7b948

File tree

6 files changed

+81
-1
lines changed

6 files changed

+81
-1
lines changed

lib/internal/modules/esm/translators.js

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

113115
const { function: compiledWrapper, sourceMapURL, sourceURL } = compileResult;
114116
// Cache the source map for the cjs module if present.
Lines 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+
}
Lines 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+
}
Lines 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+
});
Lines 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
Lines 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)