Under The Hood
frontend bun Bun 1.x (`bun test`)

bun test: Snapshot Engine, Native Mock Hoisting, and Jest Compatibility

Last updated
Prerequisites:
Jest / Vitest testing basics
Unit testing and mocking concepts
  • bun
  • test
  • bun-test
  • jest
  • mocking
  • snapshots
  • testing

Read at your depth

The practical view

`bun test` is Bun's built-in, Jest-compatible test runner. Run tests with `bun test`. It imports standard `describe`, `it`, `test`, `expect`, `beforeEach`, `afterEach`, and `mock` from `bun:test`. It natively supports TypeScript and JSX out of the box with zero configuration files (`babel.config.js` or `tsconfig.json` test loaders are not required).

The same idea in other frameworks

node equivalent
Jest / Vitest runner running in Node.js.

Legacy vs modern

Jest Configuration Overload vs Zero-Config bun:test

Jest requires multiple npm loaders and config files for TypeScript; `bun test` works out of the box.

before → after
Jest TypeScript Setup
npm i -D jest ts-jest @types/jest; // ❌ Complex config and slow startup
Native bun:test Suite
import { test, expect, mock } from 'bun:test'; // ✅ NEW: Built-in, zero config

Interview gotchas

Context

Interviewers test your knowledge of dependency mocking in Bun.

The mechanical answer

Use `mock.module('module-name', () => { return { myExport: () => 'mocked' }; })`. `mock.module` intercepts the module resolution map inside the JavaScriptCore runtime. Any subsequent import of `'module-name'` throughout the test suite will receive the mocked exports.

Trap

`mock.module` hoists automatically, but to restore original module implementations, call `mock.restore()` or `mock.module()` with the original factory.

Under The Hood — a multi-depth technical interview hub.

Third-party licenses Press ⌘ K to search.