Skip to main content
Bun automatically discovers test files based on naming conventions and directory structure.

Default discovery patterns

Bun recognizes test files with these patterns:

File extensions

  • *.test.ts
  • *.test.tsx
  • *.test.js
  • *.test.jsx
  • *.test.mjs
  • *.test.cjs
And with underscore:
  • *_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:
Bun searches the current directory and subdirectories for test files.

Running specific tests

Specific file

Multiple files

Directory

Pattern matching

Test file structure

Colocate tests with source code:
Or separate test directory:

Configuration

Configure test discovery in bunfig.toml:

Root directory

Set the root directory for test discovery:
Bun will only search for tests in ./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

Use tsconfig.json path mapping:

Preloading modules

Load modules before running tests:
Preloaded files run before any test files are loaded. Use this for:
  • Setting up global test utilities
  • Mocking built-in modules
  • Configuring test environment
#test/setup.ts

Test execution order

Bun runs tests in this order:
  1. Load preload modules (from bunfig.toml)
  2. Discover test files
  3. Load each test file
  4. Execute tests in each file:
    • beforeAll hooks
    • For each test:
      • beforeEach hooks
      • Test function
      • afterEach hooks
    • afterAll hooks

Parallel execution

Bun can run test files in parallel:
Or configure in bunfig.toml:
Tests within the same file always run sequentially. Only different test files run in parallel.

Filtering tests

By name pattern

Only tests whose names match the pattern will run.

By file pattern

By label

Coming soon: Filter tests by custom labels.

Debugging test discovery

See which tests Bun discovers:
This lists all test files without running them.

Performance tips

Reduce test discovery time

Limit search scope:

Optimize test file size

Split large test files:

Use concurrent tests

Mark tests as concurrent when possible: