Skip to content

test: exclude mock from coverage #59348

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Aug 11, 2025
Merged
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
8 changes: 7 additions & 1 deletion lib/internal/test_runner/coverage.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ const {
const { setupCoverageHooks } = require('internal/util');
const { tmpdir } = require('os');
const { join, resolve, relative } = require('path');
const { fileURLToPath } = require('internal/url');
const { fileURLToPath, URL } = require('internal/url');
const { kMappings, SourceMap } = require('internal/source_map/source_map');
const {
codes: {
Expand All @@ -37,6 +37,7 @@ const {
},
} = require('internal/errors');
const { matchGlobPattern } = require('internal/fs/glob');
const { kMockSearchParam } = require('internal/test_runner/mock/mock');

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

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

// This check filters out the node_modules/ directory, unless it is explicitly included.
return StringPrototypeIncludes(url, '/node_modules/');
}
Expand Down
5 changes: 5 additions & 0 deletions test/fixtures/test-runner/coverage-with-mock/module.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import {getData, sum} from './sum.js'

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

export const theModuleGetData = () => getData()
9 changes: 9 additions & 0 deletions test/fixtures/test-runner/coverage-with-mock/sum.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// console.log('imported')

const data = { type: 'object' }

// console.log(data)

export const sum = (a, b) => a + b

export const getData = () => data
17 changes: 17 additions & 0 deletions test/fixtures/test-runner/output/coverage-with-mock.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { describe, it, mock } from 'node:test';

describe('module test with mock', async () => {
mock.module('../coverage-with-mock/sum.js', {
namedExports: {
sum: (a, b) => 1,
getData: () => ({}),
},
});

const { theModuleSum, theModuleGetData } = await import('../coverage-with-mock/module.js');

it('tests correct thing', (t) => {
t.assert.strictEqual(theModuleSum(1, 2), 1);
t.assert.deepStrictEqual(theModuleGetData(), {});
});
});
38 changes: 38 additions & 0 deletions test/fixtures/test-runner/output/coverage-with-mock.snapshot
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
TAP version 13
# Subtest: module test with mock
# Subtest: tests correct thing
ok 1 - tests correct thing
---
duration_ms: *
type: 'test'
...
1..1
ok 1 - module test with mock
---
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 | | | |
# module.js | 100.00 | 100.00 | 100.00 |
# output | | | |
# coverage-with-mock.mjs | 100.00 | 100.00 | 100.00 |
# ---------------------------------------------------------------------------
# all files | 100.00 | 100.00 | 100.00 |
# ---------------------------------------------------------------------------
# end of coverage report
4 changes: 2 additions & 2 deletions test/fixtures/test-runner/output/typescript-coverage.snapshot
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,11 @@ ok 1 - foo
# fixtures | | | |
# test-runner | | | |
# coverage | | | |
# bar.mts | 0.00 | 100.00 | 100.00 | 1-3
# bar.mts | 33.33 | 100.00 | 0.00 | 2-3
# foo.mts | 100.00 | 100.00 | 100.00 |
# output | | | |
# typescript-coverage.mts | 100.00 | 100.00 | 100.00 |
# ----------------------------------------------------------------------------
# all files | 85.29 | 100.00 | 85.71 |
# all files | 93.55 | 100.00 | 85.71 |
Comment on lines +32 to +37
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since bar.mts is imported before the mock in typescript-coverage.ts, that one line should be covered.

# ----------------------------------------------------------------------------
# end of coverage report
9 changes: 9 additions & 0 deletions test/parallel/test-runner-output.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,15 @@ const tests = [
'--experimental-test-coverage',
'--test-coverage-exclude=!test/**']
} : false,
process.features.inspector ? {
name: 'test-runner/output/coverage-with-mock.mjs',
flags: ['--disable-warning=ExperimentalWarning',
'--test-reporter=tap',
'--experimental-transform-types',
'--experimental-test-module-mocks',
'--experimental-test-coverage',
'--test-coverage-exclude=!test/**']
} : false,
]
.filter(Boolean)
.map(({ flags, name, tty, transform, cwd }) => ({
Expand Down
Loading