Enabling Coverage
bun:test supports seeing which lines of code are covered by tests. To use this feature, pass --coverage to the CLI. It will print out a coverage report to the console:
terminal
Enable by Default
To always enable coverage reporting by default, add the following line to yourbunfig.toml:
bunfig.toml
bunfig.toml.
bunfig.toml
Coverage Thresholds
It is possible to specify a coverage threshold inbunfig.toml. If your test suite does not meet or exceed this threshold, bun test will exit with a non-zero exit code to indicate the failure.
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, coverage reports will be printed to the console. For persistent code coverage reports in CI environments and for other tools, you can pass a--coverage-reporter=lcov CLI option or coverageReporter option in bunfig.toml.
bunfig.toml
Available Reporters
LCOV Coverage Reporter
To generate an lcov report, you can use the lcov reporter. This will generate anlcov.info file in 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
By default, test files themselves are included in coverage reports. You can exclude them with:bunfig.toml
*.test.ts, *.spec.js) from the coverage report.
Ignore Specific Paths and Patterns
You can exclude specific files or file patterns from coverage reports usingcoveragePathIgnorePatterns:
bunfig.toml
collectCoverageFrom ignore patterns. Files matching any of these patterns will be excluded from coverage calculation and reporting in both text and LCOV outputs.
Common Use Cases
bunfig.toml
Sourcemaps
Internally, Bun transpiles all files by default, so Bun automatically generates an internal source map that maps lines of your original source code onto Bun’s internal representation. If for any reason you want to disable this, settest.coverageIgnoreSourcemaps to true; this will rarely be desirable outside of advanced use cases.
bunfig.toml
Coverage Defaults
By default, coverage reports:- Exclude
node_modulesdirectories - Exclude files loaded via non-JS/TS loaders (e.g.,
.css,.txt) unless a custom JS loader is specified - Include test files themselves (can be disabled with
coverageSkipTestFiles = true) - 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 that were called during tests
- % Lines: Percentage of executable lines that were run during tests
- Uncovered Line #s: Specific line numbers that were not 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, they might not be imported by your tests. Coverage only tracks files that are actually 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