Skip to content

Commit 4ab6491

Browse files
committed
fixup! test_runner: add support for coverage via run()
1 parent 9a9c295 commit 4ab6491

File tree

3 files changed

+37
-10
lines changed

3 files changed

+37
-10
lines changed

doc/api/test.md

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1246,6 +1246,9 @@ added:
12461246
- v18.9.0
12471247
- v16.19.0
12481248
changes:
1249+
- version: REPLACEME
1250+
pr-url: https://github.com/nodejs/node/pull/53937
1251+
description: Added coverage options.
12491252
- version: REPLACEME
12501253
pr-url: https://github.com/nodejs/node/pull/53927
12511254
description: Added the `isolation` option.
@@ -1319,6 +1322,29 @@ changes:
13191322
that specifies the index of the shard to run. This option is _required_.
13201323
* `total` {number} is a positive integer that specifies the total number
13211324
of shards to split the test files to. This option is _required_.
1325+
* `coverage` {boolean} enable [code coverage][] collection.
1326+
**Default:** `false`.
1327+
* `coverageExcludeGlobs` {string|Array} Excludes specific files from code coverage
1328+
using a glob pattern, which can match both absolute and relative file paths.
1329+
This property is only applicable when `coverage` was set to `true`.
1330+
If both `coverageExcludeGlobs` and `coverageIncludeGlobs` are provided,
1331+
files must meet **both** criteria to be included in the coverage report.
1332+
**Default:** `undefined`.
1333+
* `coverageIncludeGlobs` {string|Array} Includes specific files in code coverage
1334+
using a glob pattern, which can match both absolute and relative file paths.
1335+
This property is only applicable when `coverage` was set to `true`.
1336+
If both `coverageExcludeGlobs` and `coverageIncludeGlobs` are provided,
1337+
files must meet **both** criteria to be included in the coverage report.
1338+
**Default:** `undefined`.
1339+
* `lineCoverage` {number} Require a minimum percent of covered lines. If code
1340+
coverage does not reach the threshold specified, the process will exit with code `1`.
1341+
**Default:** `0`.
1342+
* `branchCoverage` {number} Require a minimum percent of covered branches. If code
1343+
coverage does not reach the threshold specified, the process will exit with code `1`.
1344+
**Default:** `0`.
1345+
* `functionCoverage` {number} Require a minimum percent of covered functions. If code
1346+
coverage does not reach the threshold specified, the process will exit with code `1`.
1347+
**Default:** `0`.
13221348
* Returns: {TestsStream}
13231349

13241350
**Note:** `shard` is used to horizontally parallelize test running across
@@ -3532,6 +3558,7 @@ Can be used to abort test subtasks when the test has been aborted.
35323558
[`run()`]: #runoptions
35333559
[`suite()`]: #suitename-options-fn
35343560
[`test()`]: #testname-options-fn
3561+
[code coverage]: #collecting-code-coverage
35353562
[describe options]: #describename-options-fn
35363563
[it options]: #testname-options-fn
35373564
[stream.compose]: stream.md#streamcomposestreams

lib/internal/test_runner/runner.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -517,9 +517,6 @@ function run(options = kEmptyObject) {
517517
shard,
518518
coverageExcludeGlobs,
519519
coverageIncludeGlobs,
520-
lineCoverage,
521-
branchCoverage,
522-
functionCoverage,
523520
} = options;
524521
const {
525522
concurrency,
@@ -534,6 +531,9 @@ function run(options = kEmptyObject) {
534531
only,
535532
globPatterns,
536533
coverage,
534+
lineCoverage,
535+
branchCoverage,
536+
functionCoverage,
537537
} = options;
538538

539539
if (files != null) {
@@ -682,9 +682,9 @@ function run(options = kEmptyObject) {
682682
coverage,
683683
coverageExcludeGlobs,
684684
coverageIncludeGlobs,
685-
lineCoverage,
686-
branchCoverage,
687-
functionCoverage,
685+
lineCoverage: lineCoverage ?? 0,
686+
branchCoverage: branchCoverage ?? 0,
687+
functionCoverage: functionCoverage ?? 0,
688688
};
689689
const root = createTestTree(rootTestOptions, globalOptions);
690690
let testFiles = files ?? createTestFileList(globPatterns);

test/parallel/test-runner-run-coverage.mjs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -121,9 +121,9 @@ describe('require(\'node:test\').run Coverage settings', { concurrency: true },
121121
// eslint-disable-next-line no-unused-vars
122122
for await (const _ of stream);
123123
});
124-
124+
125125
it('should run with coverage and exclude by glob', async () => {
126-
const stream = run({ files, coverage: true, coverageExcludePatterns: ['test/*/test-runner/invalid-tap.js'] });
126+
const stream = run({ files, coverage: true, coverageExcludeGlobs: ['test/*/test-runner/invalid-tap.js'] });
127127
stream.on('test:fail', common.mustNotCall());
128128
stream.on('test:pass', common.mustCall(1));
129129
stream.on('test:coverage', common.mustCall(({ summary: { files } }) => {
@@ -133,9 +133,9 @@ describe('require(\'node:test\').run Coverage settings', { concurrency: true },
133133
// eslint-disable-next-line no-unused-vars
134134
for await (const _ of stream);
135135
});
136-
136+
137137
it('should run with coverage and include by glob', async () => {
138-
const stream = run({ files, coverage: true, coverageIncludePatterns: ['test/*/test-runner/invalid-tap.js'] });
138+
const stream = run({ files, coverage: true, coverageIncludeGlobs: ['test/*/test-runner/invalid-tap.js'] });
139139
stream.on('test:fail', common.mustNotCall());
140140
stream.on('test:pass', common.mustCall(1));
141141
stream.on('test:coverage', common.mustCall(({ summary: { files } }) => {

0 commit comments

Comments
 (0)