Skip to main content
Bun’s test runner has built-in code coverage reporting. Use it to see how much of your codebase your tests cover and to find untested code.

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 your bunfig.toml:
bunfig.toml
By default, coverage reports exclude test files and use sourcemaps. Both are configurable in bunfig.toml.
bunfig.toml

Coverage Thresholds

Set a coverage threshold in bunfig.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
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, 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 an lcov.info file to the coverage directory.
bunfig.toml
terminal
Tools and services that read the LCOV format include:
  • 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
When 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
The option accepts glob patterns and works like Jest’s 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, set test.coverageIgnoreSourcemaps to true; you rarely want this 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 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:
  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.