-
Notifications
You must be signed in to change notification settings - Fork 31.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
test_runner: check test coverage threshold #51370
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -892,6 +892,94 @@ generated as part of the test runner output. If no tests are run, a coverage | |
report is not generated. See the documentation on | ||
[collecting code coverage from tests][] for more details. | ||
|
||
### `--check-coverage` | ||
|
||
<!-- YAML | ||
added: | ||
- REPLACEME | ||
--> | ||
|
||
> Stability: 1 - Experimental | ||
|
||
The `--check-coverage` CLI flag, used in conjunction with | ||
the `--experimental-test-coverage` command, enforces that | ||
the test coverage thresholds specified by check | ||
flags are respected: | ||
|
||
* [`--lines=threshold`][]: Sets the threshold for line coverage. | ||
* [`--branches=threshold`][]: Sets the threshold for branch coverage. | ||
* [`--functions=threshold`][]: Sets the threshold for function coverage. | ||
|
||
To enforce global 100% coverage set the value of the each flag to 100: | ||
|
||
```bash | ||
node --test --experimental-test-coverage --check-coverage --lines=100 --branches=100 --functions=100 | ||
``` | ||
|
||
If one of the specified coverage check falls below the threshold, | ||
the test will exit with non zero code. | ||
|
||
### `--lines=threshold` | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I still think that, if folks keep adding more and more flags for the test runner, they should at least be scoped, i.e., the name should be somewhat specific (e.g., There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
I do agree that flags need to be scoped but also not too long as they become hard to read when lined. Can you elaborate on how the option should be parsed in test runner context? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. One idea may be to let --check-coverage=lines=80,branches=100
# or
--check-coverage=lines:80,branches:100
# or support passing multiple times, one aspect each
--check-coverage lines=80 --check-coverage branches=100 I know this increases parsing complexity and may not be aligned with other CLI flags in Node.js (though I haven't checked them all). There is, however, precedence in other CLIs such as the Docker CLI: $ docker run --env VAR1=value1 --env VAR2=value2 ubuntu |
||
|
||
<!-- YAML | ||
added: | ||
- REPLACEME | ||
--> | ||
|
||
> Stability: 1 - Experimental | ||
|
||
The `--lines` CLI flag, used in conjunction with the `--check-coverage` flag, | ||
enforces a coverage threshold check for lines of code covered by the test. | ||
It is expressed as a numerical value between `0` and `100`, | ||
representing the percentage (e.g., 80 for 80% coverage). | ||
If the coverage falls below the threshold, | ||
the test will exit with non zero code. | ||
|
||
```bash | ||
node --test --experimental-test-coverage --check-coverage --lines=80 | ||
``` | ||
|
||
### `--branches=threshold` | ||
|
||
<!-- YAML | ||
added: | ||
- REPLACEME | ||
--> | ||
|
||
> Stability: 1 - Experimental | ||
|
||
The `--branches` CLI flag, used in conjunction with the `--check-coverage` flag, | ||
enforces a coverage threshold check for branches of code covered by the test. | ||
It is expressed as a numerical value between `0` and `100`, | ||
representing the percentage (e.g., 80 for 80% coverage). | ||
If the coverage falls below the threshold, | ||
the test will exit with non zero code. | ||
|
||
```bash | ||
node --test --experimental-test-coverage --check-coverage --branches=80 | ||
``` | ||
|
||
### `--functions=threshold` | ||
|
||
<!-- YAML | ||
added: | ||
- REPLACEME | ||
--> | ||
|
||
> Stability: 1 - Experimental | ||
|
||
The `--functions` CLI flag, used in conjunction with | ||
the `--check-coverage`flag, enforces a coverage threshold check | ||
for functions covered by the test. | ||
It is expressed as a numerical value between `0` and `100`, | ||
representing the percentage (e.g., 80 for 80% coverage). | ||
If the coverage falls below the threshold, | ||
the test will exit with non zero code. | ||
|
||
```bash | ||
node --test --experimental-test-coverage --check-coverage --functions=80 | ||
``` | ||
|
||
### `--experimental-vm-modules` | ||
|
||
<!-- YAML | ||
|
@@ -2945,14 +3033,17 @@ done | |
[`--allow-fs-read`]: #--allow-fs-read | ||
[`--allow-fs-write`]: #--allow-fs-write | ||
[`--allow-worker`]: #--allow-worker | ||
[`--branches=threshold`]: #--branchesthreshold | ||
[`--build-snapshot`]: #--build-snapshot | ||
[`--cpu-prof-dir`]: #--cpu-prof-dir | ||
[`--diagnostic-dir`]: #--diagnostic-dirdirectory | ||
[`--experimental-default-type=module`]: #--experimental-default-typetype | ||
[`--experimental-sea-config`]: single-executable-applications.md#generating-single-executable-preparation-blobs | ||
[`--experimental-wasm-modules`]: #--experimental-wasm-modules | ||
[`--functions=threshold`]: #--functionsthreshold | ||
[`--heap-prof-dir`]: #--heap-prof-dir | ||
[`--import`]: #--importmodule | ||
[`--lines=threshold`]: #--linesthreshold | ||
[`--openssl-config`]: #--openssl-configfile | ||
[`--preserve-symlinks`]: #--preserve-symlinks | ||
[`--print`]: #-p---print-script | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
// Flags: --expose-internals --experimental-test-coverage --check-coverage --lines=100 --branches=100 --functions=100 | ||
|
||
'use strict'; | ||
require('../../../common'); | ||
const { TestCoverage } = require('internal/test_runner/coverage'); | ||
const { test, mock } = require('node:test'); | ||
|
||
mock.method(TestCoverage.prototype, 'summary', () => { | ||
return { | ||
files: [], | ||
totals: { | ||
totalLineCount: 100, | ||
totalBranchCount: 100, | ||
totalFunctionCount: 100, | ||
coveredLineCount: 100, | ||
coveredBranchCount: 100, | ||
coveredFunctionCount: 100, | ||
coveredLinePercent: 100, | ||
coveredBranchPercent: 100, | ||
coveredFunctionPercent: 100 | ||
} | ||
} | ||
}); | ||
|
||
test('ok'); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
TAP version 13 | ||
# Subtest: ok | ||
ok 1 - ok | ||
--- | ||
duration_ms: * | ||
... | ||
1..1 | ||
# tests 1 | ||
# suites 0 | ||
# pass 1 | ||
# fail 0 | ||
# cancelled 0 | ||
# skipped 0 | ||
# todo 0 | ||
# duration_ms * | ||
# start of coverage report | ||
# ----------------------------------------------------- | ||
# file | line % | branch % | funcs % | uncovered lines | ||
# ----------------------------------------------------- | ||
# ----------------------------------------------------- | ||
# all… | 100.00 | 100.00 | 100.00 | | ||
# ----------------------------------------------------- | ||
# end of coverage report |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
// Flags: --expose-internals --experimental-test-coverage --check-coverage --lines=100 --branches=100 --functions=100 | ||
|
||
'use strict'; | ||
require('../../../common'); | ||
const { TestCoverage } = require('internal/test_runner/coverage'); | ||
const { test, mock } = require('node:test'); | ||
|
||
mock.method(TestCoverage.prototype, 'summary', () => { | ||
return { | ||
files: [], | ||
totals: { | ||
totalLineCount: 0, | ||
totalBranchCount: 0, | ||
totalFunctionCount: 0, | ||
coveredLineCount: 0, | ||
coveredBranchCount: 0, | ||
coveredFunctionCount: 0, | ||
coveredLinePercent: 0, | ||
coveredBranchPercent: 0, | ||
coveredFunctionPercent: 0 | ||
} | ||
} | ||
}); | ||
|
||
test('ok'); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I assume some projects actually want this for one reason or another, but is it something we want to promote?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think that not having it means people using an external library for code coverage instead of the native one, rather than something built on top