Step 1: Replace the test command
- Before
- After
package.json scripts:
package.json
Step 2: Update imports (optional)
Bun internally re-maps imports from@jest/globals to bun:test equivalents, so you often do not need to change your imports. But if you prefer to use Bun’s module explicitly:
math.test.ts
Step 3: Enable global type support (optional)
Since Bun v1.2.19, you can add TypeScript type support for Jest-style globals with a single triple-slash directive. Add it to one file in your project — for example, aglobal.d.ts in the root:
global.d.ts
describe, test, expect, beforeAll, etc. without any per-file imports.
What is supported
Bun’s test runner implements the vast majority of Jest’s API:Test structure
describe, test, it, test.only, test.skip, test.todoLifecycle hooks
beforeAll, afterAll, beforeEach, afterEachMatchers
toBe, toEqual, toMatch, toThrow, toHaveBeenCalled, and most othersMocks and spies
jest.fn(), jest.spyOn(), jest.mock(), mockReturnValue, mockImplementationSnapshots
toMatchSnapshot(), toMatchInlineSnapshot(), bun test --update-snapshotsCoverage
bun test --coverage with configurable thresholdsJest config option equivalents
Replace Jest configuration options with Bun CLI flags orbunfig.toml settings:
| Jest config | Bun equivalent |
|---|---|
jest --coverage | bun test --coverage |
bail: 3 | bun test --bail=3 |
testTimeout: 10000 | bun test --timeout 10000 |
testPathPattern: "src" | bun test src |
--watch | bun test --watch |
verbose | logLevel = "debug" in bunfig.toml |
transform | Not needed — Bun transpiles TS/JSX natively |
jest.config.js entirely once they migrate.
DOM testing (jsdom replacement)
If your tests usetestEnvironment: "jsdom", use happy-dom instead — jsdom depends on V8 internals that are not available in Bun.
Install happy-dom and create a preload script:
happy-dom.ts
bunfig.toml:
bunfig.toml
Coverage
Run coverage with the--coverage flag:
bunfig.toml:
bunfig.toml
Known differences
expect().toHaveReturned()is not yet implemented.jsdomdoes not work in Bun. Usehappy-dominstead.- Some advanced Jest configuration keys (e.g.,
haste,watchman) have no equivalent in Bun.