--coverage to see which lines and functions are exercised by your tests.
Enabling coverage
- % Funcs — percentage of functions called during tests
- % Lines — percentage of executable lines that were run
- Uncovered Line #s — specific lines that were not executed
Always-on coverage
Enable coverage by default inbunfig.toml:
bunfig.toml
Coverage thresholds
Fail the test run if coverage falls below a specified level. This is useful in CI to enforce minimum quality gates.Simple threshold
Apply the same threshold to lines, functions, and statements:bunfig.toml
Per-metric thresholds
Set different thresholds for each coverage metric:bunfig.toml
bun test exits with a non-zero code if coverage is below the threshold.
Coverage reporters
By default, coverage is printed as a text table. UsecoverageReporter to add additional output formats:
bunfig.toml
| Reporter | Description |
|---|---|
text | Prints a summary table to the console (default) |
lcov | Writes an lcov.info file to the coverage directory |
LCOV integration
The LCOV format is supported by a wide range of tools:VS Code
Extensions like Coverage Gutters can display coverage inline in the editor using the
lcov.info file.Codecov / Coveralls
Upload
coverage/lcov.info as a coverage artifact to track coverage trends over time.GitHub Actions
Use the
codecov/codecov-action to upload and comment on PRs automatically.GitLab CI
Configure
coverage_report artifacts to display coverage in merge requests.Excluding files from coverage
Skip test files
By default, test files themselves are included in the coverage report. Exclude them with:bunfig.toml
Path ignore patterns
Exclude specific files or directories using glob patterns:bunfig.toml
CI/CD integration
GitHub Actions
.github/workflows/test.yml
GitLab CI
.gitlab-ci.yml
Running coverage on a subset of tests
Run coverage for specific files or patterns:Sourcemaps
Bun transpiles all files internally and automatically applies sourcemaps so that coverage reports reference your original source lines. To disable this (rarely needed):bunfig.toml
Coverage defaults
By default, coverage reports:- Exclude
node_modulesdirectories - Exclude files loaded through non-JS/TS loaders (e.g.,
.css,.txt) - Include test files (disable with
coverageSkipTestFiles = true)
Best practices
Focus on quality, not just percentage
Focus on quality, not just percentage
High coverage does not guarantee good tests. Write assertions that verify behavior, not just lines that execute without throwing.
Test edge cases to increase meaningful coverage
Test edge cases to increase meaningful coverage
Use coverage to find gaps, not as the goal
Use coverage to find gaps, not as the goal
Run
bun test --coverage periodically to identify untested branches. Use the uncovered line numbers to guide where to add tests next.Only run coverage in CI for large projects
Only run coverage in CI for large projects
Coverage instrumentation adds overhead. For fast feedback during development, run tests without
--coverage and let CI enforce thresholds.