Skip to main content
Bun’s test runner includes built-in code coverage support. Generate coverage reports to see which lines of your code are tested.

Basic usage

Generate a coverage report:
This runs all tests and displays a coverage summary:

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:
This creates 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 in bunfig.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:
Tests will fail if any metric is below the threshold:

Viewing LCOV reports

With lcov tools

Generate an HTML report:

With VS Code

Install the Coverage Gutters extension:
  1. Generate LCOV report: bun test --coverage --coverage-reporter lcov
  2. Open a file
  3. Click “Watch” in the status bar
  4. Coverage highlights appear in the gutter

With online tools

Upload lcov.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:
Lines 12-15 and 23 are not covered.

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 your coveragePathIgnorePatterns in bunfig.toml: