Default discovery patterns
Bun recognizes test files with these patterns:File extensions
*.test.ts*.test.tsx*.test.js*.test.jsx*.test.mjs*.test.cjs
*_test.ts*_test.tsx*_test.js*_test.jsx*_test.mjs*_test.cjs
Test directories
Files in__tests__ directories:
Running all tests
Run all discovered tests:Running specific tests
Specific file
Multiple files
Directory
Pattern matching
Test file structure
Recommended structure
Colocate tests with source code:Configuration
Configure test discovery inbunfig.toml:
Root directory
Set the root directory for test discovery:./src and its subdirectories.
Custom patterns
Configure custom test file patterns:Exclude patterns
Exclude directories from test discovery:Import resolution
Bun resolves imports in test files using the same rules as regular code:Relative imports
Package imports
Path mapping
Usetsconfig.json path mapping:
Preloading modules
Load modules before running tests:- Setting up global test utilities
- Mocking built-in modules
- Configuring test environment
#test/setup.ts
Test execution order
Bun runs tests in this order:- Load preload modules (from
bunfig.toml) - Discover test files
- Load each test file
- Execute tests in each file:
beforeAllhooks- For each test:
beforeEachhooks- Test function
afterEachhooks
afterAllhooks
Parallel execution
Bun can run test files in parallel:bunfig.toml: