Skip to content

Commit

Permalink
fixup! test_runner: add support for coverage via run()
Browse files Browse the repository at this point in the history
  • Loading branch information
atlowChemi committed Sep 5, 2024
1 parent 9a9c295 commit 4ab6491
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 10 deletions.
27 changes: 27 additions & 0 deletions doc/api/test.md
Original file line number Diff line number Diff line change
Expand Up @@ -1246,6 +1246,9 @@ added:
- v18.9.0
- v16.19.0
changes:
- version: REPLACEME
pr-url: https://github.com/nodejs/node/pull/53937
description: Added coverage options.
- version: REPLACEME
pr-url: https://github.com/nodejs/node/pull/53927
description: Added the `isolation` option.
Expand Down Expand Up @@ -1319,6 +1322,29 @@ changes:
that specifies the index of the shard to run. This option is _required_.
* `total` {number} is a positive integer that specifies the total number
of shards to split the test files to. This option is _required_.
* `coverage` {boolean} enable [code coverage][] collection.
**Default:** `false`.
* `coverageExcludeGlobs` {string|Array} Excludes specific files from code coverage
using a glob pattern, which can match both absolute and relative file paths.
This property is only applicable when `coverage` was set to `true`.
If both `coverageExcludeGlobs` and `coverageIncludeGlobs` are provided,
files must meet **both** criteria to be included in the coverage report.
**Default:** `undefined`.
* `coverageIncludeGlobs` {string|Array} Includes specific files in code coverage
using a glob pattern, which can match both absolute and relative file paths.
This property is only applicable when `coverage` was set to `true`.
If both `coverageExcludeGlobs` and `coverageIncludeGlobs` are provided,
files must meet **both** criteria to be included in the coverage report.
**Default:** `undefined`.
* `lineCoverage` {number} Require a minimum percent of covered lines. If code
coverage does not reach the threshold specified, the process will exit with code `1`.
**Default:** `0`.
* `branchCoverage` {number} Require a minimum percent of covered branches. If code
coverage does not reach the threshold specified, the process will exit with code `1`.
**Default:** `0`.
* `functionCoverage` {number} Require a minimum percent of covered functions. If code
coverage does not reach the threshold specified, the process will exit with code `1`.
**Default:** `0`.
* Returns: {TestsStream}

**Note:** `shard` is used to horizontally parallelize test running across
Expand Down Expand Up @@ -3532,6 +3558,7 @@ Can be used to abort test subtasks when the test has been aborted.
[`run()`]: #runoptions
[`suite()`]: #suitename-options-fn
[`test()`]: #testname-options-fn
[code coverage]: #collecting-code-coverage
[describe options]: #describename-options-fn
[it options]: #testname-options-fn
[stream.compose]: stream.md#streamcomposestreams
Expand Down
12 changes: 6 additions & 6 deletions lib/internal/test_runner/runner.js
Original file line number Diff line number Diff line change
Expand Up @@ -517,9 +517,6 @@ function run(options = kEmptyObject) {
shard,
coverageExcludeGlobs,
coverageIncludeGlobs,
lineCoverage,
branchCoverage,
functionCoverage,
} = options;
const {
concurrency,
Expand All @@ -534,6 +531,9 @@ function run(options = kEmptyObject) {
only,
globPatterns,
coverage,
lineCoverage,
branchCoverage,
functionCoverage,
} = options;

if (files != null) {
Expand Down Expand Up @@ -682,9 +682,9 @@ function run(options = kEmptyObject) {
coverage,
coverageExcludeGlobs,
coverageIncludeGlobs,
lineCoverage,
branchCoverage,
functionCoverage,
lineCoverage: lineCoverage ?? 0,
branchCoverage: branchCoverage ?? 0,
functionCoverage: functionCoverage ?? 0,
};
const root = createTestTree(rootTestOptions, globalOptions);
let testFiles = files ?? createTestFileList(globPatterns);
Expand Down
8 changes: 4 additions & 4 deletions test/parallel/test-runner-run-coverage.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -121,9 +121,9 @@ describe('require(\'node:test\').run Coverage settings', { concurrency: true },
// eslint-disable-next-line no-unused-vars
for await (const _ of stream);
});

it('should run with coverage and exclude by glob', async () => {
const stream = run({ files, coverage: true, coverageExcludePatterns: ['test/*/test-runner/invalid-tap.js'] });
const stream = run({ files, coverage: true, coverageExcludeGlobs: ['test/*/test-runner/invalid-tap.js'] });
stream.on('test:fail', common.mustNotCall());
stream.on('test:pass', common.mustCall(1));
stream.on('test:coverage', common.mustCall(({ summary: { files } }) => {
Expand All @@ -133,9 +133,9 @@ describe('require(\'node:test\').run Coverage settings', { concurrency: true },
// eslint-disable-next-line no-unused-vars
for await (const _ of stream);
});

it('should run with coverage and include by glob', async () => {
const stream = run({ files, coverage: true, coverageIncludePatterns: ['test/*/test-runner/invalid-tap.js'] });
const stream = run({ files, coverage: true, coverageIncludeGlobs: ['test/*/test-runner/invalid-tap.js'] });
stream.on('test:fail', common.mustNotCall());
stream.on('test:pass', common.mustCall(1));
stream.on('test:coverage', common.mustCall(({ summary: { files } }) => {
Expand Down

0 comments on commit 4ab6491

Please sign in to comment.