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
16 changes: 13 additions & 3 deletions lib/internal/test_runner/coverage.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ const {
},
} = require('internal/errors');
const { matchGlobPattern } = require('internal/fs/glob');
const { constants: { kMockSearchParam } } = require('internal/test_runner/mock/loader');
const { constants: { kMockSearchParam }, mocks } = require('internal/test_runner/mock/loader');

const kCoverageFileRegex = /^coverage-(\d+)-(\d{13})-(\d+)\.json$/;
const kIgnoreRegex = /\/\* node:coverage ignore next (?<count>\d+ )?\*\//;
Expand Down Expand Up @@ -498,8 +498,18 @@ class TestCoverage {
return true;
}

const searchParams = new URL(url).searchParams;
if (searchParams.get(kMockSearchParam)) {
const parsedURL = new URL(url);
if (parsedURL.searchParams.get(kMockSearchParam)) {
return true;
}

// Check if the base URL (without query params) is registered as a mock.
// This handles the case where a CJS module is mocked and imported into
// an ESM module - V8 coverage reports the original file URL without the
// mock search parameter.
parsedURL.search = '';
const mock = mocks.get(parsedURL.href);
if (mock?.active === true) {
return true;
}

Expand Down
13 changes: 13 additions & 0 deletions test/fixtures/test-runner/coverage-with-mock-cjs/dependency.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
'use strict';

const data = { type: 'cjs-object' };

function sum(a, b) {
return a + b;
}

function getData() {
return data;
}

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

export const theModuleSum = (a, b) => sum(a, b);

export const theModuleGetData = () => getData();
17 changes: 17 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,17 @@
import { describe, it, mock } from 'node:test';

describe('coverage with mocked CJS module in ESM', async () => {
mock.module('../coverage-with-mock-cjs/dependency.cjs', {
namedExports: {
sum: (a, b) => 42,
getData: () => ({ mocked: true }),
},
});

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

it('uses mocked CJS exports', (t) => {
t.assert.strictEqual(theModuleSum(1, 2), 42);
t.assert.deepStrictEqual(theModuleGetData(), { mocked: true });
});
});
38 changes: 38 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,38 @@
TAP version 13
# Subtest: coverage with mocked CJS module in ESM
# Subtest: uses mocked CJS exports
ok 1 - uses mocked CJS exports
---
duration_ms: *
type: 'test'
...
1..1
ok 1 - coverage with mocked CJS module in ESM
---
duration_ms: *
type: 'suite'
...
1..1
# tests 1
# suites 1
# 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-cjs | | | |
# 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
25 changes: 25 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,25 @@
// Test that mocked CJS modules imported into ESM are excluded from coverage.
// This tests the fix for https://github.com/nodejs/node/issues/61709
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-transform-types',
'--experimental-test-module-mocks',
'--experimental-test-coverage',
'--test-coverage-exclude=!test/**',
],
},
);