From 4ab6491abeb9b7d416b6ccd1288a7c8f6a568df7 Mon Sep 17 00:00:00 2001 From: atlowChemi Date: Thu, 5 Sep 2024 07:52:45 +0300 Subject: [PATCH] fixup! test_runner: add support for coverage via run() --- doc/api/test.md | 27 ++++++++++++++++++++++ lib/internal/test_runner/runner.js | 12 +++++----- test/parallel/test-runner-run-coverage.mjs | 8 +++---- 3 files changed, 37 insertions(+), 10 deletions(-) diff --git a/doc/api/test.md b/doc/api/test.md index a7af114880ea87..2ccdf3fa2f5d3d 100644 --- a/doc/api/test.md +++ b/doc/api/test.md @@ -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. @@ -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 @@ -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 diff --git a/lib/internal/test_runner/runner.js b/lib/internal/test_runner/runner.js index ba795a38a982da..2f54260ee86441 100644 --- a/lib/internal/test_runner/runner.js +++ b/lib/internal/test_runner/runner.js @@ -517,9 +517,6 @@ function run(options = kEmptyObject) { shard, coverageExcludeGlobs, coverageIncludeGlobs, - lineCoverage, - branchCoverage, - functionCoverage, } = options; const { concurrency, @@ -534,6 +531,9 @@ function run(options = kEmptyObject) { only, globPatterns, coverage, + lineCoverage, + branchCoverage, + functionCoverage, } = options; if (files != null) { @@ -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); diff --git a/test/parallel/test-runner-run-coverage.mjs b/test/parallel/test-runner-run-coverage.mjs index 14a04620f45471..475ec893c0dd0d 100644 --- a/test/parallel/test-runner-run-coverage.mjs +++ b/test/parallel/test-runner-run-coverage.mjs @@ -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 } }) => { @@ -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 } }) => {