module
bun:test
The 'bun:test' module is a fast, built-in test runner that aims for Jest compatibility. Tests are executed with the Bun runtime, providing significantly improved performance over traditional test runners.
Key features include TypeScript and JSX support, lifecycle hooks (beforeAll, beforeEach, afterEach, afterAll), snapshot testing, UI & DOM testing, watch mode, and script pre-loading. The API supports test assertions with expect, test grouping with describe, mocks, and more.
While Bun aims for compatibility with Jest, not everything is implemented yet. Use bun test to automatically discover and run tests matching common patterns like *.test.ts or *.spec.js.
Functions 10
- afterAll()
Runs a function, once, after all the tests.
- afterEach()
Runs a function after each test.
- aroundAll()
Wraps all the tests of the file or
describe()block in a function. - aroundEach()
Wraps each test in a function.
- beforeAll()
Runs a function, once, before all the tests.
- beforeEach()
Runs a function before each test.
- onTestFinished()
Runs a function after the test finishes, once all
afterEachhooks have completed. - setDefaultTimeout()
Sets the default timeout for all tests in the current file.
- setSystemTime()
Control the system time used by.
- spyOn()
Create a spy on a property's getter.
Interfaces 21
- interface AsymmetricMatchers
Extend this interface with declaration merging to add type support for custom asymmetric matchers.
- interface AsymmetricMatchersBuiltin
- interface Describe
Describes a group of related tests.
- interface Expect
Extend this interface with declaration merging to add type support for custom asymmetric matchers.
- interface ExpectState
- interface ExtendTest
The type of
test.extend. - interface FakeTimersOptions
- interface FixtureOptions
- interface MatcherResult
- interface Matchers
Extend this interface with declaration merging to add type support for custom matchers.
- interface MatchersBuiltin
- interface ModuleMockOptions
- interface SnapshotSerializerConfig
The
pretty-formatoptions that snapshots are printed with. - interface Test
Runs a test.
- interface TestAnnotation
- interface TestContext
The object passed to
test.for()callbacks and to the callbacks of atestmade bytest.extend(). - interface TesterContext
- interface TestOptions
- interface TestSuite
- interface TestTask
- interface TestWithContext
A
testwhose callbacks receive the TestContext instead of adonecallback.
Types 15
- type AsymmetricMatcher
An asymmetric matcher returned by a static
expectmethod such asexpect.anything()orexpect.stringContaining("..."). - type AsyncMatchers
The matchers available after
.resolvesand.rejects. - type CustomMatcher
- type CustomMatchersDetected
All non-builtin matchers and asymmetric matchers that have been type-registered through declaration merging
- type EqualsFunction
- type ExpectExtendMatchers
If a matcher's type has been defined through declaration merging, enforces it.
- type FakeableAPI
A function or clock that
useFakeTimers()can fake. - type FixtureFunction
Sets up a fixture, passes it to
use(), and tears it down after the promise returned byuse()resolves. - type Fixtures
- type Mock
- type Mocked
Twith every nested function and class typed as a Mock. - type ModuleMockFactory
Returns a mocked module's exports, or a promise for them.
- type SnapshotPrinter
Prints a value nested inside the one a SnapshotSerializer is printing.
- type SnapshotSerializer
A
pretty-formatplugin for Expect.addSnapshotSerializer. - type Tester
Custom equality tester
Variables 8
- const describe
Describes a group of related tests.
- const expect
Asserts that a value matches some criteria.
- const expectTypeOf
- const mock
- const test
Runs a test.
- const vi
Vitest-compatible mocking utilities, for migrating tests from Vitest to Bun.
- const xdescribe
Skips a group of related tests.
- const xtest
Skips a test.