Set a code coverage threshold with the Bun test runner
Bun's test runner has built-in code coverage reporting. Enable it with the --coverage flag.
terminal
bun test --coveragetest.test.ts:
✓ math > add [0.71ms]
✓ math > multiply [0.03ms]
✓ random [0.13ms]
-------------|---------|---------|-------------------
File | % Funcs | % Lines | Uncovered Line #s
-------------|---------|---------|-------------------
All files | 50.00 | 66.67 |
math.ts | 50.00 | 66.67 |
random.ts | 50.00 | 66.67 |
-------------|---------|---------|-------------------
3 pass
0 fail
3 expect() callsTo set a minimum coverage threshold, add the following to your bunfig.toml. A threshold of 0.9 requires that tests cover 90% of the lines and 90% of the functions of every file in the coverage report. Bun checks the threshold against each file, not against the All files average.
bunfig.toml
[test]
# to require 90% line-level and function-level coverage
coverageThreshold = 0.9If your test suite does not meet this threshold, bun test exits with a non-zero exit code to signal a failure.
terminal
bun test --coverage<test output>
$ echo $?
1 # this is the exit code of the previous commandYou can set different thresholds for line-level and function-level coverage.
bunfig.toml
[test]
# to set different thresholds for lines and functions
coverageThreshold = { lines = 0.5, functions = 0.7 }See Code coverage.