Skip to main content
Define tests with a Jest-like API imported from the built-in bun:test module. All test functions (test, describe, expect, beforeAll, etc.) are also available as globals — no import required — for full Jest compatibility.

Basic usage

math.test.ts

Grouping tests

Use describe() to group related tests into a suite:
math.test.ts

Async tests

Tests can be async. Bun awaits the returned promise before marking the test as complete.
Alternatively, use the done callback. If you declare it as a parameter, you must call it or the test will time out.

Timeouts

Pass a timeout in milliseconds as the third argument to test(). The default is 5000ms.

Test modifiers

test.skip

Skip a test without deleting it. Skipped tests are not executed.

test.todo

Mark a test as a planned but unimplemented case:
Run bun test --todo to find any todo tests that are unexpectedly passing.

test.only

Run only the tests (or suites) marked with .only. Use bun test --only to enforce this.

test.if / test.skipIf / test.todoIf

Run, skip, or mark a test as todo based on a runtime condition:
These modifiers also work on describe blocks, affecting all tests within.

test.failing

Use test.failing to mark a test you know is currently broken. The test passes when it fails, and fails when it unexpectedly passes:

test.concurrent / test.serial

Control concurrency at the individual test level. See Test Runner for details.

Assertion counting

Use expect.assertions() to verify a specific number of assertions ran — essential for async code with conditional branches:
Use expect.hasAssertions() to require at least one assertion:

Retries and repeats

You cannot use both retry and repeats on the same test.

Parameterized tests

test.each

Run the same test with multiple sets of data:
Pass objects to access values by name in the title using $key syntax:

describe.each

Create a parameterized suite that runs all its tests for each case:

Format specifiers

Type testing

Bun includes expectTypeOf for type-level assertions, compatible with Vitest. These are no-ops at runtime — run bunx tsc --noEmit to validate types.

Matchers reference

Basic

Strings and arrays

Objects

Numbers

Functions and errors

Mock functions

Snapshots

Best practices