rembrembdocs

Skip to main content

Bun home pagelight logodark logo

[Runtime

](../../../index.md)[Package Manager

](../../../pm/cli/install/index.md)[Bundler

](../../../bundler/index.md)[Test Runner

](../../../test/index.md)[Guides

](../../index.md)[Reference

](https://bun.com/reference)[Blog

](https://bun.com/blog)[Feedback

](../../../feedback/index.md)

In many cases, Bun’s test runner can run Jest test suites with no code changes. Just run bun test instead of npx jest, yarn test, etc.

terminal

npx jest
yarn test
bun test

There’s often no need for code changes.

But if you’d rather switch to the bun:test imports, you can do that too.

https://mintcdn.com/bun-1dd33a4e/JUhaF6Mf68z_zHyy/icons/typescript.svg?fit=max&auto=format&n=JUhaF6Mf68z_zHyy&q=85&s=7ac549adaea8d5487d8fbd58cc3ea35btest.ts

import { test, expect } from "@jest/globals"; 
import { test, expect } from "bun:test"; 

Since Bun v1.2.19, you can enable TypeScript support for global test functions with a single triple-slash directive. This makes migrating from Jest even easier since you only need to add the directive once in your entire project: Add this directive to just one file in your project, such as:

https://mintcdn.com/bun-1dd33a4e/JUhaF6Mf68z_zHyy/icons/typescript.svg?fit=max&auto=format&n=JUhaF6Mf68z_zHyy&q=85&s=7ac549adaea8d5487d8fbd58cc3ea35bglobal.d.ts

/// <reference types="bun-types/test-globals" />

Once added, all test files in your project automatically get TypeScript support for Jest globals:

https://mintcdn.com/bun-1dd33a4e/JUhaF6Mf68z_zHyy/icons/typescript.svg?fit=max&auto=format&n=JUhaF6Mf68z_zHyy&q=85&s=7ac549adaea8d5487d8fbd58cc3ea35bmath.test.ts

describe("my test suite", () => {
  test("should work", () => {
    expect(1 + 1).toBe(2);
  });

  beforeAll(() => {
    // setup code
  });

  afterEach(() => {
    // cleanup code
  });
});

Bun implements the vast majority of Jest’s matchers, but compatibility isn’t 100% yet. Refer to the full compatibility table at Docs > Test runner > Writing tests. Some notable missing features:


If you’re using testEnvironment: "jsdom" to run your tests in a browser-like environment, you should follow the DOM testing with Bun and happy-dom guide to inject browser APIs into the global scope. This guide relies on happy-dom, which is a leaner and faster alternative to jsdom. At the moment jsdom does not work in Bun due to its internal use of V8 APIs. Track support for it here.

bunfig.toml

[test]
preload = ["./happy-dom.ts"]

Replace bail in your Jest config with the --bail CLI flag.

terminal

bun test --bail=3

Replace collectCoverage with the --coverage CLI flag.

terminal

bun test --coverage

Replace testTimeout with the --test-timeout CLI flag.

terminal

bun test --timeout 10000

Many other flags become irrelevant or obsolete when using bun test.


Settings that aren’t mentioned here are not supported or have no equivalent. Please file a feature request if something important is missing.


See also:

Was this page helpful?

Suggest editsRaise issue

[

Run tests in watch mode with Bun

Previous

](../watch-mode/index.md)[

Mock functions in `bun test`

Next

](../mock-functions/index.md)