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 8
- afterAll()
Runs a function, once, after all the tests.
- afterEach()
Runs a function after each test.
- 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 an object property or method
Interfaces 10
- 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 MatcherResult
- interface Matchers
Extend this interface with declaration merging to add type support for custom matchers.
- interface MatchersBuiltin
- interface Test
Runs a test.
- interface TesterContext
- interface TestOptions
Types 7
- type AsymmetricMatcher
An asymmetric matcher returned by a static
expectmethod such asexpect.anything()orexpect.stringContaining("..."). - 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 Mock
- 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.