Skip to content

Commit fb8f062

Browse files
committed
test_runner: exclude test files from coverage by default
1 parent b17a1fb commit fb8f062

21 files changed

+302
-185
lines changed

doc/api/cli.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2290,6 +2290,7 @@ This option may be specified multiple times to exclude multiple glob patterns.
22902290

22912291
If both `--test-coverage-exclude` and `--test-coverage-include` are provided,
22922292
files must meet **both** criteria to be included in the coverage report.
2293+
However, if there is an overlap between the two, `--test-coverage-include` takes precedence.
22932294

22942295
### `--test-coverage-functions=threshold`
22952296

@@ -2318,6 +2319,10 @@ This option may be specified multiple times to include multiple glob patterns.
23182319

23192320
If both `--test-coverage-exclude` and `--test-coverage-include` are provided,
23202321
files must meet **both** criteria to be included in the coverage report.
2322+
However, if there is an overlap between the two, `--test-coverage-include` takes precedence.
2323+
2324+
By default, the test files are excluded from code coverage. They can be explicitly
2325+
included via this flag.
23212326

23222327
### `--test-coverage-lines=threshold`
23232328

doc/api/test.md

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1341,14 +1341,18 @@ changes:
13411341
* `coverageExcludeGlobs` {string|Array} Excludes specific files from code coverage
13421342
using a glob pattern, which can match both absolute and relative file paths.
13431343
This property is only applicable when `coverage` was set to `true`.
1344-
If both `coverageExcludeGlobs` and `coverageIncludeGlobs` are provided,
1344+
If both `--test-coverage-exclude` and `--test-coverage-include` are provided,
13451345
files must meet **both** criteria to be included in the coverage report.
1346+
However, if there is an overlap between the two, `--test-coverage-include` takes precedence.
13461347
**Default:** `undefined`.
13471348
* `coverageIncludeGlobs` {string|Array} Includes specific files in code coverage
13481349
using a glob pattern, which can match both absolute and relative file paths.
13491350
This property is only applicable when `coverage` was set to `true`.
1350-
If both `coverageExcludeGlobs` and `coverageIncludeGlobs` are provided,
1351+
If both `--test-coverage-exclude` and `--test-coverage-include` are provided,
13511352
files must meet **both** criteria to be included in the coverage report.
1353+
However, if there is an overlap between the two, `--test-coverage-include` takes precedence.
1354+
By default, the test files are excluded from code coverage. They can be explicitly
1355+
included via this flag.
13521356
**Default:** `undefined`.
13531357
* `lineCoverage` {number} Require a minimum percent of covered lines. If code
13541358
coverage does not reach the threshold specified, the process will exit with code `1`.

lib/internal/test_runner/coverage.js

Lines changed: 31 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,9 @@ const {
2525
readFileSync,
2626
rmSync,
2727
} = require('fs');
28-
const { setupCoverageHooks } = require('internal/util');
28+
const { setupCoverageHooks, isWindows, isMacOS } = require('internal/util');
2929
const { tmpdir } = require('os');
30-
const { join, resolve, relative, matchesGlob } = require('path');
30+
const { join, resolve, relative } = require('path');
3131
const { fileURLToPath } = require('internal/url');
3232
const { kMappings, SourceMap } = require('internal/source_map/source_map');
3333
const {
@@ -42,6 +42,25 @@ const kLineEndingRegex = /\r?\n$/u;
4242
const kLineSplitRegex = /(?<=\r?\n)/u;
4343
const kStatusRegex = /\/\* node:coverage (?<status>enable|disable) \*\//;
4444

45+
let minimatch;
46+
function lazyMinimatch() {
47+
minimatch ??= require('internal/deps/minimatch/index');
48+
return minimatch;
49+
}
50+
51+
function glob(path, pattern, windows) {
52+
return lazyMinimatch().minimatch(path, pattern, {
53+
__proto__: null,
54+
nocase: isMacOS || isWindows,
55+
windowsPathsNoEscape: true,
56+
nonegate: true,
57+
nocomment: true,
58+
optimizationLevel: 2,
59+
platform: windows ? 'win32' : 'posix',
60+
nocaseMagicOnly: true,
61+
});
62+
}
63+
4564
class CoverageLine {
4665
constructor(line, startOffset, src, length = src?.length) {
4766
const newlineLength = src == null ? 0 :
@@ -464,23 +483,24 @@ class TestCoverage {
464483
coverageExcludeGlobs: excludeGlobs,
465484
coverageIncludeGlobs: includeGlobs,
466485
} = this.options;
467-
// This check filters out files that match the exclude globs.
468-
if (excludeGlobs?.length > 0) {
469-
for (let i = 0; i < excludeGlobs.length; ++i) {
470-
if (matchesGlob(relativePath, excludeGlobs[i]) ||
471-
matchesGlob(absolutePath, excludeGlobs[i])) return true;
472-
}
473-
}
474486

475487
// This check filters out files that do not match the include globs.
476488
if (includeGlobs?.length > 0) {
477489
for (let i = 0; i < includeGlobs.length; ++i) {
478-
if (matchesGlob(relativePath, includeGlobs[i]) ||
479-
matchesGlob(absolutePath, includeGlobs[i])) return false;
490+
if (glob(relativePath, includeGlobs[i]) ||
491+
glob(absolutePath, includeGlobs[i])) return false;
480492
}
481493
return true;
482494
}
483495

496+
// This check filters out files that match the exclude globs.
497+
if (excludeGlobs?.length > 0) {
498+
for (let i = 0; i < excludeGlobs.length; ++i) {
499+
if (glob(relativePath, excludeGlobs[i]) ||
500+
glob(absolutePath, excludeGlobs[i])) return true;
501+
}
502+
}
503+
484504
// This check filters out the node_modules/ directory, unless it is explicitly included.
485505
return StringPrototypeIncludes(url, '/node_modules/');
486506
}

lib/internal/test_runner/harness.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,13 @@ function setupProcessState(root, globalOptions) {
249249
root.harness.teardown = exitHandler;
250250
}
251251

252+
function excludeTestFileFromCoverage(testFile, globalOptions) {
253+
if (globalOptions.coverageExcludeGlobs === undefined) {
254+
globalOptions.coverageExcludeGlobs = [];
255+
}
256+
ArrayPrototypePush(globalOptions.coverageExcludeGlobs, testFile);
257+
}
258+
252259
function lazyBootstrapRoot() {
253260
if (!globalRoot) {
254261
// This is where the test runner is bootstrapped when node:test is used
@@ -261,6 +268,7 @@ function lazyBootstrapRoot() {
261268
};
262269
const globalOptions = parseCommandLine();
263270
globalOptions.cwd = process.cwd();
271+
excludeTestFileFromCoverage(entryFile, globalOptions);
264272
createTestTree(rootTestOptions, globalOptions);
265273
globalRoot.reporter.on('test:summary', (data) => {
266274
if (!data.success) {

lib/internal/test_runner/runner.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -664,6 +664,12 @@ function run(options = kEmptyObject) {
664664
validateStringArray(argv, 'options.argv');
665665
validateStringArray(execArgv, 'options.execArgv');
666666

667+
let testFiles = files ?? createTestFileList(globPatterns, cwd);
668+
if (coverageExcludeGlobs === undefined) {
669+
coverageExcludeGlobs = [];
670+
}
671+
ArrayPrototypePush(coverageExcludeGlobs, ...testFiles);
672+
667673
const rootTestOptions = { __proto__: null, concurrency, timeout, signal };
668674
const globalOptions = {
669675
__proto__: null,
@@ -680,7 +686,6 @@ function run(options = kEmptyObject) {
680686
cwd,
681687
};
682688
const root = createTestTree(rootTestOptions, globalOptions);
683-
let testFiles = files ?? createTestFileList(globPatterns, cwd);
684689

685690
if (shard) {
686691
testFiles = ArrayPrototypeFilter(testFiles, (_, index) => index % shard.total === shard.index - 1);

test/fixtures/test-runner/output/coverage-width-100-uncovered-lines.snapshot

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15,18 +15,16 @@ ok 1 - Coverage Print Fixed Width 100
1515
# duration_ms *
1616
# start of coverage report
1717
# --------------------------------------------------------------------------------------------------
18-
# file | line % | branch % | funcs % | uncovered lines
18+
# file | line % | branch % | funcs % | uncovered lines
1919
# --------------------------------------------------------------------------------------------------
20-
# test | | | |
21-
# fixtures | | | |
22-
# test-runner | | | |
23-
# coverage-snap | | | |
24-
# a.js | 55.77 | 100.00 | 0.00 | 5-7 9-11 13-15 17-19 …
25-
# b.js | 45.45 | 100.00 | 0.00 | 5-7 9-11
26-
# many-uncovered-lines.js | 50.99 | 42.86 | 1.92 | 5-7 9-11 13-15 17-19 …
27-
# output | | | |
28-
# coverage-width-100-uncovered-lines.mjs | 100.00 | 100.00 | 100.00 |
20+
# test | | | |
21+
# fixtures | | | |
22+
# test-runner | | | |
23+
# coverage-snap | | | |
24+
# a.js | 55.77 | 100.00 | 0.00 | 5-7 9-11 13-15 17-19 29-30 40-42 45-…
25+
# b.js | 45.45 | 100.00 | 0.00 | 5-7 9-11
26+
# many-uncovered-lines.js | 50.99 | 42.86 | 1.92 | 5-7 9-11 13-15 17-19 29-30 40-42 45-…
2927
# --------------------------------------------------------------------------------------------------
30-
# all files | 52.80 | 60.00 | 1.61 |
28+
# all files | 51.44 | 55.56 | 1.61 |
3129
# --------------------------------------------------------------------------------------------------
3230
# end of coverage report

test/fixtures/test-runner/output/coverage-width-100.snapshot

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -14,18 +14,16 @@ ok 1 - Coverage Print Fixed Width 100
1414
# todo 0
1515
# duration_ms *
1616
# start of coverage report
17-
# --------------------------------------------------------------------------------------------------
18-
# file | line % | branch % | funcs % | uncovered lines
19-
# --------------------------------------------------------------------------------------------------
20-
# test | | | |
21-
# fixtures | | | |
22-
# test-runner | | | |
23-
# coverage-snap | | | |
24-
# a.js | 55.77 | 100.00 | 0.00 | 5-7 9-11 13-15 17-19 29-30 40-42 45-4…
25-
# b.js | 45.45 | 100.00 | 0.00 | 5-7 9-11
26-
# output | | | |
27-
# coverage-width-100.mjs | 100.00 | 100.00 | 100.00 |
28-
# --------------------------------------------------------------------------------------------------
29-
# all files | 60.81 | 100.00 | 0.00 |
30-
# --------------------------------------------------------------------------------------------------
17+
# -----------------------------------------------------------------------------------------------
18+
# file | line % | branch % | funcs % | uncovered lines
19+
# -----------------------------------------------------------------------------------------------
20+
# test | | | |
21+
# fixtures | | | |
22+
# test-runner | | | |
23+
# coverage-snap | | | |
24+
# a.js | 55.77 | 100.00 | 0.00 | 5-7 9-11 13-15 17-19 29-30 40-42 45-47 50-52
25+
# b.js | 45.45 | 100.00 | 0.00 | 5-7 9-11
26+
# -----------------------------------------------------------------------------------------------
27+
# all files | 53.97 | 100.00 | 0.00 |
28+
# -----------------------------------------------------------------------------------------------
3129
# end of coverage report

test/fixtures/test-runner/output/coverage-width-150-uncovered-lines.snapshot

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15,18 +15,16 @@ ok 1 - Coverage Print Fixed Width 150
1515
# duration_ms *
1616
# start of coverage report
1717
# ----------------------------------------------------------------------------------------------------------------------------------------------------
18-
# file | line % | branch % | funcs % | uncovered lines
18+
# file | line % | branch % | funcs % | uncovered lines
1919
# ----------------------------------------------------------------------------------------------------------------------------------------------------
20-
# test | | | |
21-
# fixtures | | | |
22-
# test-runner | | | |
23-
# coverage-snap | | | |
24-
# a.js | 55.77 | 100.00 | 0.00 | 5-7 9-11 13-15 17-19 29-30 40-42 45-47 50-52
25-
# b.js | 45.45 | 100.00 | 0.00 | 5-7 9-11
26-
# many-uncovered-lines.js | 50.99 | 42.86 | 1.92 | 5-7 9-11 13-15 17-19 29-30 40-42 45-47 50-52 55-57 59-61 63-65 67-69 91…
27-
# output | | | |
28-
# coverage-width-150-uncovered-lines.mjs | 100.00 | 100.00 | 100.00 |
20+
# test | | | |
21+
# fixtures | | | |
22+
# test-runner | | | |
23+
# coverage-snap | | | |
24+
# a.js | 55.77 | 100.00 | 0.00 | 5-7 9-11 13-15 17-19 29-30 40-42 45-47 50-52
25+
# b.js | 45.45 | 100.00 | 0.00 | 5-7 9-11
26+
# many-uncovered-lines.js | 50.99 | 42.86 | 1.92 | 5-7 9-11 13-15 17-19 29-30 40-42 45-47 50-52 55-57 59-61 63-65 67-69 91-93 96-98 100-1…
2927
# ----------------------------------------------------------------------------------------------------------------------------------------------------
30-
# all files | 52.80 | 60.00 | 1.61 |
28+
# all files | 51.44 | 55.56 | 1.61 |
3129
# ----------------------------------------------------------------------------------------------------------------------------------------------------
3230
# end of coverage report

test/fixtures/test-runner/output/coverage-width-150.snapshot

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -14,18 +14,16 @@ ok 1 - Coverage Print Fixed Width 150
1414
# todo 0
1515
# duration_ms *
1616
# start of coverage report
17-
# --------------------------------------------------------------------------------------------------------
18-
# file | line % | branch % | funcs % | uncovered lines
19-
# --------------------------------------------------------------------------------------------------------
20-
# test | | | |
21-
# fixtures | | | |
22-
# test-runner | | | |
23-
# coverage-snap | | | |
24-
# a.js | 55.77 | 100.00 | 0.00 | 5-7 9-11 13-15 17-19 29-30 40-42 45-47 50-52
25-
# b.js | 45.45 | 100.00 | 0.00 | 5-7 9-11
26-
# output | | | |
27-
# coverage-width-150.mjs | 100.00 | 100.00 | 100.00 |
28-
# --------------------------------------------------------------------------------------------------------
29-
# all files | 60.81 | 100.00 | 0.00 |
30-
# --------------------------------------------------------------------------------------------------------
17+
# -----------------------------------------------------------------------------------------------
18+
# file | line % | branch % | funcs % | uncovered lines
19+
# -----------------------------------------------------------------------------------------------
20+
# test | | | |
21+
# fixtures | | | |
22+
# test-runner | | | |
23+
# coverage-snap | | | |
24+
# a.js | 55.77 | 100.00 | 0.00 | 5-7 9-11 13-15 17-19 29-30 40-42 45-47 50-52
25+
# b.js | 45.45 | 100.00 | 0.00 | 5-7 9-11
26+
# -----------------------------------------------------------------------------------------------
27+
# all files | 53.97 | 100.00 | 0.00 |
28+
# -----------------------------------------------------------------------------------------------
3129
# end of coverage report

test/fixtures/test-runner/output/coverage-width-40.snapshot

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,7 @@ ok 1 - Coverage Print Fixed Width 40
2525
# c.js | 55.77 | 100.00 | 0.00 | …
2626
# a.js | 55.77 | 100.00 | 0.00 | …
2727
# b.js | 45.45 | 100.00 | 0.00 | …
28-
# output | | | |
29-
# …e-width-40.mjs | 100.00 | 100.00 | 100.00 |
3028
# --------------------------------------
31-
# all files | 59.06 | 100.00 | 0.00 |
29+
# all files | 54.78 | 100.00 | 0.00 |
3230
# --------------------------------------
3331
# end of coverage report

test/fixtures/test-runner/output/coverage-width-80-color.snapshot

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,18 +9,16 @@
99
[34mℹ duration_ms *[39m
1010
[34mℹ start of coverage report
1111
ℹ ------------------------------------------------------------------------------
12-
ℹ file | [31mline %[34m | [31mbranch %[34m | [31mfuncs %[34m | uncovered
12+
ℹ file | [31mline %[34m | [31mbranch %[34m | [31mfuncs %[34m | uncovered lines
1313
ℹ ------------------------------------------------------------------------------
14-
ℹ test | [31m [34m | [31m [34m | [31m [34m |
15-
ℹ fixtures | [31m [34m | [31m [34m | [31m [34m |
16-
ℹ test-runner | [31m [34m | [31m [34m | [31m [34m |
17-
ℹ coverage-snap | [31m [34m | [31m [34m | [31m [34m |
18-
ℹ [33ma.js [34m | [33m 55.77[34m | [32m 100.00[34m | [31m 0.00[34m | 5-7 9-11 1…
19-
ℹ [31mb.js [34m | [31m 45.45[34m | [32m 100.00[34m | [31m 0.00[34m | 5-7 9-11
20-
ℹ output | [31m [34m | [31m [34m | [31m [34m |
21-
ℹ [32mcoverage-width-80-color.mjs [34m | [32m100.00[34m | [32m 100.00[34m | [32m 100.00[34m |
14+
ℹ test | [31m [34m | [31m [34m | [31m [34m |
15+
ℹ fixtures | [31m [34m | [31m [34m | [31m [34m |
16+
ℹ test-runner | [31m [34m | [31m [34m | [31m [34m |
17+
ℹ coverage-snap | [31m [34m | [31m [34m | [31m [34m |
18+
ℹ [33ma.js [34m | [33m 55.77[34m | [32m 100.00[34m | [31m 0.00[34m | 5-7 9-11 13-15 17-19 29-…
19+
ℹ [31mb.js [34m | [31m 45.45[34m | [32m 100.00[34m | [31m 0.00[34m | 5-7 9-11
2220
ℹ ------------------------------------------------------------------------------
23-
ℹ all files | [33m 61.33[34m | [32m 100.00[34m | [31m 0.00[34m |
21+
ℹ all files | [33m 53.97[34m | [32m 100.00[34m | [31m 0.00[34m |
2422
ℹ ------------------------------------------------------------------------------
2523
ℹ end of coverage report
2624
[39m

0 commit comments

Comments
 (0)