bun test with bunfig.toml and command-line options.
Configuration File
To configurebun test in bunfig.toml, add a [test] section:
bunfig.toml
Test Discovery
root
Theroot option sets the directory Bun scans for tests, instead of the project root.
bunfig.toml
Examples
bunfig.toml
Preload Scripts
Thepreload option loads scripts before the tests run:
bunfig.toml
--preload on the command line:
terminal
Common Preload Use Cases
test-setup.ts
global-mocks.ts
Path Ignore Patterns
pathIgnorePatterns excludes files and directories from test discovery entirely, using glob patterns. Unlike coveragePathIgnorePatterns, which only affects coverage reports, pathIgnorePatterns prevents matching paths from being discovered and run as tests.
Use it when your project contains submodules, vendored code, or other directories with *.test.ts files that you don’t want bun test to pick up.
bunfig.toml
--path-ignore-patterns on the command line:
terminal
Common Use Cases
bunfig.toml
Command-line
--path-ignore-patterns flags override the bunfig.toml value entirely — the two are not merged.Reporters
JUnit Reporter
Configure the JUnit reporter output file path directly in the config file:bunfig.toml
--reporter=junit and --reporter-outfile CLI flags:
terminal
Multiple Reporters
You can use multiple reporters simultaneously:terminal
bunfig.toml
Memory Usage
smol Mode
Enable the--smol memory-saving mode specifically for the test runner:
bunfig.toml
--smol flag on the command line:
terminal
smol mode reduces memory usage by:
- Using less memory for the JavaScript heap
- Being more aggressive about garbage collection
- Reducing buffer sizes where possible
Test execution
concurrentTestGlob
Run test files matching a glob pattern with concurrent test execution enabled.bunfig.toml
--concurrent flag was passed: every test in those files runs concurrently. Use this to migrate a test suite to concurrent execution gradually, or to run one kind of test (say, integration tests) concurrently while the rest stay sequential.
The --concurrent CLI flag overrides this setting, forcing all tests to run concurrently regardless of the glob pattern.
randomize
Run tests in random order to identify tests with hidden dependencies:bunfig.toml
seed
Specify a seed for reproducible random test order. Requiresrandomize = true:
bunfig.toml
retry
Default retry count for all tests. Bun retries a failed test up to this many times. Per-test{ retry: N } overrides this value. Default 0 (no retries).
bunfig.toml
--retry CLI flag overrides this setting.
rerunEach
Re-run each test file multiple times to identify flaky tests:bunfig.toml
Coverage Options
Basic Coverage Settings
bunfig.toml
Skip Test Files from Coverage
Exclude files matching test patterns (for example*.test.ts) from the coverage report:
bunfig.toml
Coverage Thresholds
Specify the coverage threshold as a single number or as an object with per-metric thresholds:bunfig.toml
fail_on_low_coverage, causing the test run to fail if coverage is below the threshold.
Threshold Examples
bunfig.toml
Coverage Path Ignore Patterns
Exclude specific files or file patterns from coverage reports using glob patterns:bunfig.toml
Common Ignore Patterns
bunfig.toml
Sourcemap Handling
Bun transpiles every file, so coverage results pass through sourcemaps before they’re reported.coverageIgnoreSourcemaps opts out of this, but the results will be confusing: during transpilation, Bun may move code around and rename variables. The option is mostly useful for debugging coverage issues.
bunfig.toml
Install Settings Inheritance
bun test inherits network and installation configuration (such as registry, cafile, prefer, and exact) from the [install] section of bunfig.toml. This matters if your tests reach a private registry or trigger installs during the run.
bunfig.toml
Environment Variables
Set environment variables for tests with.env files, which Bun loads from your project root automatically. For test-specific variables, create a .env.test file, which bun test loads automatically:
.env.test
Complete Configuration Example
An example showing the available test configuration options:bunfig.toml
CLI Override Behavior
Command-line options always override configuration file settings:bunfig.toml
terminal