Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion lib/internal/modules/esm/translators.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,9 @@ const kShouldSkipModuleHooks = { __proto__: null, shouldSkipModuleHooks: true };
* @param {boolean} isMain - Whether the module is the entrypoint
*/
function loadCJSModule(module, source, url, filename, isMain) {
const compileResult = compileFunctionForCJSLoader(source, filename, false /* is_sea_main */, false);
// Use the full URL as the V8 resource name so that any search params
// (e.g. ?node-test-mock) are preserved in coverage reports.
const compileResult = compileFunctionForCJSLoader(source, url, false /* is_sea_main */, false);

const { function: compiledWrapper, sourceMapURL, sourceURL } = compileResult;
// Cache the source map for the cjs module if present.
Expand Down
9 changes: 9 additions & 0 deletions test/fixtures/test-runner/coverage-with-mock/dependency.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
throw new Error('This should never be called');

exports.dependency = function dependency() {
return 'foo';
}

exports.unused = function unused() {
return 'bar';
}
5 changes: 5 additions & 0 deletions test/fixtures/test-runner/coverage-with-mock/subject.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { dependency } from './dependency.cjs';

export function subject() {
return dependency();
}
11 changes: 11 additions & 0 deletions test/fixtures/test-runner/output/coverage-with-mock-cjs.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { mock, test } from 'node:test';

const dependency = mock.fn(() => 'mock-return-value');
mock.module('../coverage-with-mock/dependency.cjs', { namedExports: { dependency } });

const { subject } = await import('../coverage-with-mock/subject.mjs');

test('subject calls dependency', (t) => {
t.assert.strictEqual(subject(), 'mock-return-value');
t.assert.strictEqual(dependency.mock.callCount(), 1);
});
31 changes: 31 additions & 0 deletions test/fixtures/test-runner/output/coverage-with-mock-cjs.snapshot
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
TAP version 13
# Subtest: subject calls dependency
ok 1 - subject calls dependency
---
duration_ms: *
type: 'test'
...
1..1
# tests 1
# suites 0
# pass 1
# fail 0
# cancelled 0
# skipped 0
# todo 0
# duration_ms *
# start of coverage report
# -------------------------------------------------------------------------------
# file | line % | branch % | funcs % | uncovered lines
# -------------------------------------------------------------------------------
# test | | | |
# fixtures | | | |
# test-runner | | | |
# coverage-with-mock | | | |
# subject.mjs | 100.00 | 100.00 | 100.00 |
# output | | | |
# coverage-with-mock-cjs.mjs | 100.00 | 100.00 | 100.00 |
# -------------------------------------------------------------------------------
# all files | 100.00 | 100.00 | 100.00 |
# -------------------------------------------------------------------------------
# end of coverage report
22 changes: 22 additions & 0 deletions test/test-runner/test-output-coverage-with-mock-cjs.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import * as common from '../common/index.mjs';
import * as fixtures from '../common/fixtures.mjs';
import { spawnAndAssert, defaultTransform, ensureCwdIsProjectRoot } from '../common/assertSnapshot.js';

if (!process.features.inspector) {
common.skip('inspector support required');
}

ensureCwdIsProjectRoot();
await spawnAndAssert(
fixtures.path('test-runner/output/coverage-with-mock-cjs.mjs'),
defaultTransform,
{
flags: [
'--disable-warning=ExperimentalWarning',
'--test-reporter=tap',
'--experimental-test-module-mocks',
'--experimental-test-coverage',
'--test-coverage-exclude=!test/**',
],
},
);
Loading