Skip to main content
Bun ships with a fast, built-in, Jest-compatible test runner. Tests are executed with the Bun runtime and support the following features:
  • TypeScript and JSX out of the box
  • Lifecycle hooks (beforeAll, beforeEach, afterEach, afterAll)
  • Snapshot testing
  • UI and DOM testing
  • Watch mode with --watch
  • Script preloading with --preload
Bun aims for compatibility with Jest, but not everything is implemented yet. To track progress, see the Jest compatibility tracking issue.

Running tests

Tests are written in JavaScript or TypeScript with a Jest-like API imported from bun:test.
math.test.ts

Test discovery

The runner recursively searches the working directory for files matching these patterns:
  • *.test.{js|jsx|ts|tsx}
  • *_test.{js|jsx|ts|tsx}
  • *.spec.{js|jsx|ts|tsx}
  • *_spec.{js|jsx|ts|tsx}
By default, node_modules and hidden directories (starting with .) are excluded.

Run specific files

Pass a path starting with ./ or / to run a specific file:
Pass a substring filter to match files by path:
This matches any file whose path contains auth, such as src/auth/login.test.ts.

Filter by test name

Use -t / --test-name-pattern to filter by test name using a substring or regex pattern:
The pattern is matched against the full test title, including all parent describe block labels joined with spaces.

Watch mode

Use --watch to re-run tests automatically when files change:
Only the affected test files are re-run when a source file changes.

Timeouts

Use --timeout to set a per-test timeout in milliseconds. The default is 5000ms.
You can also set a per-test timeout as the third argument to test():

Bail on failure

Use --bail to stop the test run after a given number of failures. This is useful in CI to avoid spending time on subsequent tests after early failures.

Concurrent execution

By default, tests run sequentially within each file. Use --concurrent to run async tests in parallel:
Control the maximum concurrency with --max-concurrency (default: 20):
You can also mark individual tests to run concurrently or serially:

Retrying failed tests

Use --retry to automatically retry failed tests:
Override the retry count per test with the { retry: N } option:

Randomizing test order

Use --randomize to run tests in a random order, which helps detect hidden dependencies between tests:
The seed used for randomization is printed in the summary. Use --seed to reproduce a specific order:

Reporters

Bun supports multiple output formats for test results.
The default reporter prints human-readable results to the console with color support.

CI/CD integration

GitHub Actions

Bun automatically detects GitHub Actions and emits annotations. No extra configuration is needed beyond installing Bun:
.github/workflows/test.yml

AI agent integration

When running under an AI coding assistant, set one of these environment variables for quieter output that hides passing tests but preserves failures and the summary:

Lifecycle hooks

Bun supports beforeAll, beforeEach, afterEach, and afterAll hooks for setup and teardown. See Test Lifecycle for full documentation.

Mocks

Create mock functions with mock() from bun:test. See Mocks & Spies for full documentation.

Snapshot testing

Use .toMatchSnapshot() to save and compare output across test runs. See Snapshots for full documentation.
Update snapshots with: