Enabling Coverage
bun:test can report which lines of code your tests cover. Pass --coverage to print a coverage report to the console:
terminal
Enable by Default
To enable coverage reporting by default, add this to yourbunfig.toml:
bunfig.toml
bunfig.toml.
bunfig.toml
Coverage Thresholds
Set a coverage threshold inbunfig.toml. If your test suite does not meet or exceed it, bun test exits with a non-zero exit code.
Simple Threshold
bunfig.toml
Detailed Thresholds
bunfig.toml
fail_on_low_coverage, causing the test run to fail if coverage is below the threshold.
Coverage Reporters
By default, Bun prints coverage reports to the console. To save a report for CI or other tools, pass--coverage-reporter=lcov on the command line or set coverageReporter in bunfig.toml.
bunfig.toml
Available Reporters
LCOV Coverage Reporter
The lcov reporter writes anlcov.info file to the coverage directory.
bunfig.toml
terminal
- Code editors: VS Code extensions can show coverage inline
- CI/CD services: GitHub Actions, GitLab CI, CircleCI
- Coverage services: Codecov, Coveralls
- IDEs: WebStorm, IntelliJ IDEA
Using LCOV with GitHub Actions
.github/workflows/test.yml
Excluding Files from Coverage
Skip Test Files
Coverage reports exclude test files by default. To include them:bunfig.toml
coverageSkipTestFiles is true (the default), files matching test patterns (for example *.test.ts, *.spec.js) are excluded from the coverage report.
Ignore Specific Paths and Patterns
coveragePathIgnorePatterns excludes specific files or file patterns from coverage reports:
bunfig.toml
collectCoverageFrom ignore patterns. Files matching any of the patterns are excluded from coverage calculation and reporting in both text and LCOV output.
Common Use Cases
bunfig.toml
Sourcemaps
Bun transpiles all files by default, generating an internal source map that maps lines of your original source code onto Bun’s internal representation. To disable this, settest.coverageIgnoreSourcemaps to true; you rarely want this outside of advanced use cases.
bunfig.toml
Coverage Defaults
By default, coverage reports:- Exclude
node_modulesdirectories - Exclude files loaded with non-JS/TS loaders (for example
.css,.txt) unless a custom JS loader is specified - Exclude test files themselves (can be included with
coverageSkipTestFiles = false) - Can exclude additional files with
coveragePathIgnorePatterns
Advanced Configuration
Custom Coverage Directory
bunfig.toml
Multiple Reporters
bunfig.toml
Coverage with Specific Test Patterns
terminal
CI/CD Integration
GitHub Actions Example
.github/workflows/coverage.yml
GitLab CI Example
.gitlab-ci.yml
Interpreting Coverage Reports
Text Output Explanation
- % Funcs: Percentage of functions called during tests
- % Lines: Percentage of executable lines run during tests
- Uncovered Line #s: Line numbers that were never executed
What to Aim For
- 80%+ overall coverage: Generally considered good
- 90%+ critical paths: Important business logic should be well-tested
- 100% utility functions: Pure functions and utilities are easy to test completely
- Lower coverage for UI components: Often acceptable as they may require integration tests
Best Practices
Focus on Quality, Not Just Quantity
test.ts
Test Edge Cases
test.ts
Use Coverage to Find Missing Tests
terminal
Combine with Other Quality Metrics
Coverage is just one metric. Also consider:- Code review quality
- Integration test coverage
- Error handling tests
- Performance tests
- Type safety
Troubleshooting
Coverage Not Showing for Some Files
If files aren’t appearing in coverage reports, your tests might not import them. Coverage only tracks files that are loaded.test.ts
False Coverage Reports
If you see coverage reports that don’t match your expectations:- Check if source maps are working correctly
- Verify file patterns in
coveragePathIgnorePatterns - Ensure test files are actually importing the code to test
Performance Issues with Large Codebases
For large projects, coverage collection can slow down tests:bunfig.toml