mirror of
https://github.com/nodejs/node.git
synced 2025-08-15 13:48:44 +02:00
test: exclude mock from coverage
Fixes: https://github.com/nodejs/node/issues/59112 PR-URL: https://github.com/nodejs/node/pull/59348 Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: Pietro Marchini <pietro.marchini94@gmail.com>
This commit is contained in:
parent
8a2fec1f6f
commit
220f5c644e
7 changed files with 87 additions and 3 deletions
|
@ -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 (?<count>\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/');
|
||||
}
|
||||
|
|
5
test/fixtures/test-runner/coverage-with-mock/module.js
vendored
Normal file
5
test/fixtures/test-runner/coverage-with-mock/module.js
vendored
Normal file
|
@ -0,0 +1,5 @@
|
|||
import {getData, sum} from './sum.js'
|
||||
|
||||
export const theModuleSum = (a, b) => sum(a,b)
|
||||
|
||||
export const theModuleGetData = () => getData()
|
9
test/fixtures/test-runner/coverage-with-mock/sum.js
vendored
Normal file
9
test/fixtures/test-runner/coverage-with-mock/sum.js
vendored
Normal file
|
@ -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
test/fixtures/test-runner/output/coverage-with-mock.mjs
vendored
Normal file
17
test/fixtures/test-runner/output/coverage-with-mock.mjs
vendored
Normal file
|
@ -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
test/fixtures/test-runner/output/coverage-with-mock.snapshot
vendored
Normal file
38
test/fixtures/test-runner/output/coverage-with-mock.snapshot
vendored
Normal file
|
@ -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
|
|
@ -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
|
||||
|
|
|
@ -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 }) => ({
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue