bun test instead of npx jest or yarn test.
terminal
Your test files usually work as-is.
- Bun internally rewrites imports from
@jest/globalsto theirbun:testequivalents. - If you rely on Jest to inject globals like
testandexpect, Bun does that too.
bun:test directly, update the imports.
Since Bun v1.2.19, a triple-slash directive enables TypeScript support for global test functions. Add it to one file in your project, such as:
- A
global.d.tsfile in your project root - Your test
preload.tssetup file (if usingpreloadin bunfig.toml) - Any single
.tsfile that TypeScript includes in your compilation
Once added, every test file in your project gets TypeScript support for the Jest globals:
Bun implements most of Jest’s matchers, but compatibility isn’t 100%. See the compatibility table in Writing tests. Notably missing:
expect().toHaveReturned()
If you use
testEnvironment: "jsdom" to run your tests in a browser-like environment, follow the DOM testing with Bun and happy-dom guide to inject browser APIs into the global scope. That guide uses happy-dom, a leaner and faster alternative to jsdom.
jsdom does not work in Bun because it uses V8 APIs internally. Track support in issue #3554.
bunfig.toml
Replace
bail in your Jest config with the --bail CLI flag.
terminal
Replace
collectCoverage with the --coverage CLI flag.
terminal
Replace
testTimeout with the --test-timeout CLI flag.
terminal
Many other Jest settings are irrelevant in
bun test.
transform— Bun supports TypeScript & JSX. Configure other file types with plugins.extensionsToTreatAsEsmhaste— Bun uses its own internal source mapswatchman,watchPlugins,watchPathIgnorePatterns— use--watchto run tests in watch modeverbose— setlogLevel: "debug"inbunfig.toml
Settings that aren’t mentioned here are not supported or have no equivalent. File a feature request if something you need is missing.
See also: