test_runner: add support for coverage thresholds
Fixes #48739 (closed)
This PR adds support for coverage thresholds.
The following new CLI flags are added:
--test-coverage-branches
--test-coverage-functions
--test-coverage-lines
Notable Change
Node.js now supports requiring code coverage to meet a specific threshold before the process exits successfully. To use this feature, you need to enable the --experimental-test-coverage
flag.
You can set thresholds for the following types of coverage:
-
Branch coverage: Use
--test-coverage-branches=<threshold>
-
Function coverage: Use
--test-coverage-functions=<threshold>
-
Line coverage: Use
--test-coverage-lines=<threshold>
<threshold>
should be an integer between 0 and 100. If an invalid value is provided, a TypeError
will be thrown.
If the code coverage fails to meet the specified thresholds for any category, the process will exit with code 1
.
For instance, to enforce a minimum of 80% line coverage and 60% branch coverage, you can run:
$ node --experimental-test-coverage --test-coverage-lines=80 --test-coverage-branches=60 example.js
# start of coverage report
# ...
# ...
# ...
# end of coverage report
# Error: 53.45% line coverage does not meet the threshold of 80%.
# Error: 40.30% branch coverage does not meet the threshold of 60%.