Skip to main content
Define tests with a Jest-like API imported from the built-in bun:test module. Long term, Bun aims for complete Jest compatibility; at the moment, a limited set of expect matchers are supported.

Basic Usage

To define a test:
math.test.ts

Grouping Tests

Tests can be grouped into suites with describe.
math.test.ts

Async Tests

Tests can be async.
math.test.ts
Alternatively, use the done callback to signal completion. If you include the done callback as a parameter in your test definition, you must call it or the test will hang.
math.test.ts

Timeouts

Optionally specify a per-test timeout in milliseconds by passing a number as the third argument to test.
math.test.ts
In bun:test, test timeouts throw an uncatchable exception to force the test to stop running and fail. We also kill any child processes that were spawned in the test to avoid leaving behind zombie processes lurking in the background. The default timeout for each test is 5000ms (5 seconds) if not overridden by this timeout option or jest.setDefaultTimeout().

Retries and Repeats

test.retry

Use the retry option to automatically retry a test if it fails. The test passes if it succeeds within the specified number of attempts. This is useful for flaky tests that may fail intermittently.
example.test.ts

test.repeats

Use the repeats option to run a test multiple times regardless of pass/fail status. The test fails if any iteration fails. This is useful for detecting flaky tests or stress testing. Note that repeats: N runs the test N+1 times total (1 initial run + N repeats).
example.test.ts
You cannot use both retry and repeats on the same test.

🧟 Zombie Process Killer

When a test times out and processes spawned in the test via Bun.spawn, Bun.spawnSync, or node:child_process are not killed, they will be automatically killed and a message will be logged to the console. This prevents zombie processes from lingering in the background after timed-out tests.

Test Modifiers

test.skip

Skip individual tests with test.skip. These tests will not be run.
math.test.ts

test.todo

Mark a test as a todo with test.todo. These tests will not be run.
math.test.ts
To run todo tests and find any which are passing, use bun test --todo.
terminal
With this flag, failing todo tests will not cause an error, but todo tests which pass will be marked as failing so you can remove the todo mark or fix the test.

test.only

To run a particular test or suite of tests use test.only() or describe.only().
example.test.ts
The following command will only execute tests #2 and #3.
terminal
The following command will only execute tests #1, #2 and #3.
terminal

test.if

To run a test conditionally, use test.if(). The test will run if the condition is truthy. This is particularly useful for tests that should only run on specific architectures or operating systems.
example.test.ts

test.skipIf

To instead skip a test based on some condition, use test.skipIf() or describe.skipIf().
example.test.ts

test.todoIf

If instead you want to mark the test as TODO, use test.todoIf() or describe.todoIf(). Carefully choosing skipIf or todoIf can show a difference between, for example, intent of “invalid for this target” and “planned but not implemented yet.”
example.test.ts

test.failing

Use test.failing() when you know a test is currently failing but you want to track it and be notified when it starts passing. This inverts the test result:
  • A failing test marked with .failing() will pass
  • A passing test marked with .failing() will fail (with a message indicating it’s now passing and should be fixed)
math.test.ts
This is useful for tracking known bugs that you plan to fix later, or for implementing test-driven development.

Conditional Tests for Describe Blocks

The conditional modifiers .if(), .skipIf(), and .todoIf() can also be applied to describe blocks, affecting all tests within the suite:
example.test.ts

Parametrized Tests

test.each and describe.each

To run the same test with multiple sets of data, use test.each. This creates a parametrized test that runs once for each test case provided.
math.test.ts
You can also use describe.each to create a parametrized suite that runs once for each test case:
sum.test.ts

Argument Passing

How arguments are passed to your test function depends on the structure of your test cases:
  • If a table row is an array (like [1, 2, 3]), each element is passed as an individual argument
  • If a row is not an array (like an object), it’s passed as a single argument
example.test.ts

Format Specifiers

The following options are available for formatting the test title:

Examples

example.test.ts

Assertion Counting

Bun supports verifying that a specific number of assertions were called during a test:

expect.hasAssertions()

Use expect.hasAssertions() to verify that at least one assertion is called during a test:
example.test.ts
This is especially useful for async tests to ensure your assertions actually run.

expect.assertions(count)

Use expect.assertions(count) to verify that a specific number of assertions are called during a test:
example.test.ts
This helps ensure all your assertions run, especially in complex async code with multiple code paths.

Type Testing

Bun includes expectTypeOf for testing TypeScript types, compatible with Vitest.

expectTypeOf

These functions are no-ops at runtime - you need to run TypeScript separately to verify the type checks.
The expectTypeOf function provides type-level assertions that are checked by TypeScript’s type checker. To test your types:
  1. Write your type assertions using expectTypeOf
  2. Run bunx tsc --noEmit to check that your types are correct
example.test.ts
For full documentation on expectTypeOf matchers, see the API Reference.

Matchers

Bun implements the following matchers. Full Jest compatibility is on the roadmap; track progress here.

Basic Matchers

String and Array Matchers

Object Matchers

Number Matchers

Function and Class Matchers

Promise Matchers

Mock Function Matchers

Snapshot Matchers

Utility Matchers

Not Yet Implemented

Best Practices

Use Descriptive Test Names

example.test.ts
auth.test.ts

Use Appropriate Matchers

auth.test.ts

Test Error Conditions

example.test.ts

Use Setup and Teardown

example.test.ts