diff --git a/lib/internal/test_runner/coverage.js b/lib/internal/test_runner/coverage.js index d8b4cfd8b5e..80811dffb1e 100644 --- a/lib/internal/test_runner/coverage.js +++ b/lib/internal/test_runner/coverage.js @@ -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: { @@ -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 (?\d+ )?\*\//; @@ -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/'); } diff --git a/test/fixtures/test-runner/coverage-with-mock/module.js b/test/fixtures/test-runner/coverage-with-mock/module.js new file mode 100644 index 00000000000..aa80ca1950d --- /dev/null +++ b/test/fixtures/test-runner/coverage-with-mock/module.js @@ -0,0 +1,5 @@ +import {getData, sum} from './sum.js' + +export const theModuleSum = (a, b) => sum(a,b) + +export const theModuleGetData = () => getData() diff --git a/test/fixtures/test-runner/coverage-with-mock/sum.js b/test/fixtures/test-runner/coverage-with-mock/sum.js new file mode 100644 index 00000000000..3227f83209e --- /dev/null +++ b/test/fixtures/test-runner/coverage-with-mock/sum.js @@ -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 diff --git a/test/fixtures/test-runner/output/coverage-with-mock.mjs b/test/fixtures/test-runner/output/coverage-with-mock.mjs new file mode 100644 index 00000000000..5d5b2b14f66 --- /dev/null +++ b/test/fixtures/test-runner/output/coverage-with-mock.mjs @@ -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(), {}); + }); +}); diff --git a/test/fixtures/test-runner/output/coverage-with-mock.snapshot b/test/fixtures/test-runner/output/coverage-with-mock.snapshot new file mode 100644 index 00000000000..2dd3443265b --- /dev/null +++ b/test/fixtures/test-runner/output/coverage-with-mock.snapshot @@ -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 diff --git a/test/fixtures/test-runner/output/typescript-coverage.snapshot b/test/fixtures/test-runner/output/typescript-coverage.snapshot index eaeba2bfa11..5d628e2b085 100644 --- a/test/fixtures/test-runner/output/typescript-coverage.snapshot +++ b/test/fixtures/test-runner/output/typescript-coverage.snapshot @@ -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 | # ---------------------------------------------------------------------------- # end of coverage report diff --git a/test/parallel/test-runner-output.mjs b/test/parallel/test-runner-output.mjs index 8adac4dfa72..ccfca45e502 100644 --- a/test/parallel/test-runner-output.mjs +++ b/test/parallel/test-runner-output.mjs @@ -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 }) => ({