Skip to main content
Bun’s test runner supports built-in code coverage reporting. Use it to see how much of your codebase is covered by tests and find areas that are not currently well-tested.

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 your bunfig.toml:
bunfig.toml
By default coverage reports will include test files and exclude sourcemaps. This is usually what you want, but it can be configured otherwise in bunfig.toml.
bunfig.toml

Coverage Thresholds

It is possible to specify a coverage threshold in bunfig.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
Setting any of these thresholds enables 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 an lcov.info file in the coverage directory.
bunfig.toml
terminal
The LCOV format is widely supported by various tools and services:
  • 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
This will exclude files matching test patterns (e.g., *.test.ts, *.spec.js) from the coverage report.

Ignore Specific Paths and Patterns

You can exclude specific files or file patterns from coverage reports using coveragePathIgnorePatterns:
bunfig.toml
This option accepts glob patterns and works similarly to Jest’s 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, set test.coverageIgnoreSourcemaps to true; this will rarely be desirable outside of advanced use cases.
bunfig.toml
When using this option, you probably want to stick a // @bun comment at the top of the source file to opt out of the transpilation process.

Coverage Defaults

By default, coverage reports:
  • Exclude node_modules directories
  • 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:
  1. Check if source maps are working correctly
  2. Verify file patterns in coveragePathIgnorePatterns
  3. 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
Consider running coverage only on CI or specific branches rather than every test run during development.