bun test via bunfig.toml and command-line flags. CLI flags always take precedence over configuration file values.
bunfig.toml
Add a[test] section to your bunfig.toml file to configure the test runner:
bunfig.toml
Test discovery
root
Set the root directory for test file scanning. By default, Bun scans from the current working directory.bunfig.toml
pathIgnorePatterns
Exclude files and directories from test discovery using glob patterns. Matched directories are pruned during scanning — their contents are never traversed.bunfig.toml
--path-ignore-patterns CLI flag. Note that CLI flags override the config file value entirely — they are not merged.
Preload scripts
Run setup scripts before any test files withpreload. This is the recommended way to configure global state, mocks, or DOM environments.
bunfig.toml
Example setup file
setup.ts
Example global mocks file
global-mocks.ts
Timeouts
Default timeout
Set the default timeout (in milliseconds) for all tests. Individual tests can override this with a third argument totest().
bunfig.toml
0 or Infinity in test() to disable timeout for a specific test:
Environment variables
Bun automatically loads.env files from the project root. For test-specific variables, create a .env.test file and load it explicitly:
.env.test
bun test automatically sets NODE_ENV to "test" unless it is already defined in the environment or an .env file.TypeScript
Bun compiles TypeScript natively — no extra configuration is needed. To use a customtsconfig.json for tests:
Test execution settings
randomize and seed
Run tests in random order to detect hidden dependencies:bunfig.toml
retry
Set a global retry count for flaky tests:bunfig.toml
rerunEach
Re-run each test file multiple times to detect non-deterministic failures:bunfig.toml
concurrentTestGlob
Run test files matching a glob pattern with concurrent execution enabled automatically:bunfig.toml
smol
Reduce memory usage during test runs (useful in memory-constrained CI environments):bunfig.toml
Reporters
Configure the JUnit XML reporter output path inbunfig.toml:
bunfig.toml
Coverage
Enable coverage reporting and configure thresholds:bunfig.toml
Install settings inheritance
bun test inherits relevant settings from the [install] section of bunfig.toml, including registry, prefer, and exact. This matters when tests trigger auto-installs or interact with a private registry.
bunfig.toml
Complete example
bunfig.toml