Basic usage
Generate a coverage report:Coverage reporters
Bun supports multiple coverage output formats.Text reporter (default)
Displays coverage in a table format:LCOV reporter
Generates an LCOV file that can be used with coverage visualization tools:coverage/lcov.info.
Multiple reporters
Use multiple reporters at once:Coverage directory
By default, coverage files are written to./coverage. Change this:
Configuration
Configure coverage inbunfig.toml:
Excluding test files
Exclude test files from coverage reports:Ignore patterns
Exclude files from coverage using glob patterns:Coverage thresholds
Fail tests if coverage is below a threshold:Viewing LCOV reports
With lcov tools
Generate an HTML report:With VS Code
Install the Coverage Gutters extension:- Generate LCOV report:
bun test --coverage --coverage-reporter lcov - Open a file
- Click “Watch” in the status bar
- Coverage highlights appear in the gutter
With online tools
Uploadlcov.info to coverage services:
Understanding coverage metrics
Line coverage
Percentage of lines executed:Function coverage
Percentage of functions called:Branch coverage
Percentage of branches taken:Statement coverage
Percentage of statements executed:Improving coverage
Identify uncovered code
Look at the “Uncovered Line #s” column:Add tests for uncovered branches
Use coverage thresholds
Enforce minimum coverage in CI:CI Integration
GitHub Actions
GitLab CI
Best practices
Don’t obsess over 100% coverage
100% coverage doesn’t guarantee bug-free code. Focus on testing critical paths and edge cases.Exclude generated code
Exclude auto-generated files from coverage:Test behavior, not coverage
Write tests for expected behavior, not just to increase coverage numbers.Review coverage regularly
Make coverage part of code review:Troubleshooting
No coverage output
Ensure you’re using--coverage:
Incorrect coverage data
Clear the coverage cache:Coverage not excluding files
Check yourcoveragePathIgnorePatterns in bunfig.toml: