bun test instead of npx jest, yarn test, etc.
terminal
There’s often no need for code changes.
- Bun internally re-writes imports from
@jest/globalsto use thebun:testequivalents. - If you’re relying on Jest to inject
test,expect, etc. as globals, Bun does that too.
bun:test imports, you can do that too.
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:
- 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, all test files in your project automatically get TypeScript support for Jest globals:
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:
expect().toHaveReturned()
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
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 flags become irrelevant or obsolete when using
bun test.
transform— Bun supports TypeScript & JSX. Other file types can be configured with Plugins.extensionsToTreatAsEsmhaste— Bun uses it’s 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. Please file a feature request if something important is missing.
See also: