- 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
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}
node_modules and hidden directories (starting with .) are excluded.
Run specific files
Pass a path starting with./ or / to run a specific file:
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:
describe block labels joined with spaces.
Watch mode
Use--watch to re-run tests automatically when files change:
Timeouts
Use--timeout to set a per-test timeout in milliseconds. The default is 5000ms.
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:
--max-concurrency (default: 20):
Retrying failed tests
Use--retry to automatically retry failed tests:
{ retry: N } option:
Randomizing test order
Use--randomize to run tests in a random order, which helps detect hidden dependencies between tests:
--seed to reproduce a specific order:
Reporters
Bun supports multiple output formats for test results.- Default
- Dots
- JUnit XML
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 supportsbeforeAll, beforeEach, afterEach, and afterAll hooks for setup and teardown. See Test Lifecycle for full documentation.
Mocks
Create mock functions withmock() 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.