bun test via bunfig.toml file and command-line options. This page documents the available configuration options for bun test.
Configuration File
You can configurebun test behavior by adding a [test] section to your bunfig.toml file:
bunfig.toml
Test Discovery
root
Theroot option specifies a root directory for test discovery, overriding the default behavior of scanning from the project root.
bunfig.toml
- Limit test discovery to specific directories
- Exclude certain parts of your project from test scanning
- Organize tests in a specific subdirectory structure
Examples
bunfig.toml
Preload Scripts
Load scripts before running tests using thepreload option:
bunfig.toml
--preload on the command line:
terminal
Common Preload Use Cases
Timeouts
Default Timeout
Set the default timeout for all tests:bunfig.toml
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
- CI environments with limited memory
- Large test suites that consume significant memory
- Development environments with memory constraints
Test execution
concurrentTestGlob
Automatically run test files matching a glob pattern with concurrent test execution enabled. This is useful for gradually migrating test suites to concurrent execution or for running specific test types concurrently.bunfig.toml
--concurrent flag was passed, running all tests within those files concurrently. This allows you to:
- Gradually migrate your test suite to concurrent execution
- Run integration tests concurrently while keeping unit tests sequential
- Separate fast concurrent tests from tests that require sequential execution
--concurrent CLI flag will override this setting when specified, 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. Failed tests will be retried up to this many times. Per-test{ retry: N } overrides this value. Default 0 (no retries).
bunfig.toml
--retry CLI flag will override this setting when specified.
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 (e.g.,*.test.ts) from the coverage report:
bunfig.toml
Coverage Thresholds
The coverage threshold can be specified either as a number or as an object with specific 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
Internally, Bun transpiles every file. That means code coverage must also go through sourcemaps before they can be reported. We expose this as a flag to allow you to opt out of this behavior, but it will be confusing because during the transpilation process, Bun may move code around and change variable names. This option is mostly useful for debugging coverage issues.bunfig.toml
Install Settings Inheritance
Thebun test command inherits relevant network and installation configuration (registry, cafile, prefer, exact, etc.) from the [install] section of bunfig.toml. This is important if tests need to interact with private registries or require specific install behaviors triggered during the test run.
bunfig.toml
Environment Variables
Environment variables for tests should be set using.env files. Bun automatically loads .env files from your project root. For test-specific variables, create a .env.test file:
.env.test
--env-file:
terminal
Complete Configuration Example
Here’s a comprehensive example showing all available test configuration options:bunfig.toml
CLI Override Behavior
Command-line options always override configuration file settings:bunfig.toml
terminal
Conditional Configuration
You can use different configurations for different environments:bunfig.toml
terminal
Validation and Troubleshooting
Invalid Configuration
Bun will warn about invalid configuration options:bunfig.toml
Common Configuration Issues
- Path Resolution: Relative paths in config are resolved relative to the config file location
- Pattern Matching: Glob patterns use standard glob syntax
- Type Mismatches: Ensure numeric values are not quoted unless they should be strings
bunfig.toml
Debugging Configuration
To see what configuration is being used:terminal