Vitest
Vitest is a Vite-native test runner with a Jest-compatible API. It uses the same transform pipeline as your Vite app, which makes ESM, TypeScript, and path aliases work with less config.
Detailed Explanation
Vitest was built for Vite projects: one vite.config (or vitest.config) drives both vite and vitest. Tests are ESM-first. The API looks like Jest on purpose: describe, test, expect, vi.fn(), vi.spyOn(), vi.mock(). Teams migrate by renaming jest to vi and adjusting a few module-mock edges.
Watch mode and HMR-style invalidation are the everyday win. Changing a unit re-runs the affected files quickly. Threads or forks isolate tests (pool: "forks" is safer when tests leak state). Browser mode can drive real Chromium for component tests; the default is still Node plus optional jsdom / happy-dom.
vi.mock is similar to Jest but not identical about hoist timing and factory closures. Prefer importing vi from vitest over relying on globals if you want explicitness (globals: true is opt-in). TypeScript types for expect and vi come from the vitest package.
Coverage uses v8 or istanbul providers. Snapshots exist. test.each exists. expect.poll and vi.waitFor help when you assert after a microtask or a store update.
What it costs you: another runner if the rest of the company is on Jest, and occasional plugin mismatches (a Vite plugin that assumes a browser). Node library repos without Vite can still use Vitest; they just lose the "same config as the app" story.
Reach for Vitest on Vite / Vue / modern React toolchains. Reach for Jest when the existing config and CI are Jest and nobody wants to migrate. Reach for Playwright for end-to-end flows.
Code Examples
import { describe, expect, test, vi } from "vitest";
function add(a, b) {
return a + b;
}
describe("add", () => {
test("sums", () => {
expect(add(1, 2)).toBe(3);
});
});
test("spy", () => {
const fn = vi.fn((n) => n * 2);
expect(fn(4)).toBe(8);
expect(fn).toHaveBeenCalledWith(4);
});import { expect, test } from "vitest";
test("microtask", async () => {
let n = 0;
queueMicrotask(() => {
n = 1;
});
await expect.poll(() => n).toBe(1);
});Common Gotchas
`vi.mock` factories have hoist rules
do not close over uninitialised imports. Use `vi.hoisted(() => ...)` when you need shared state.
`happy-dom` / `jsdom` are not Chrome
use browser mode or Playwright for layout and real `fetch` cookies.
Enable `globals` only if you want Jest-like implicit `test`
otherwise import from `vitest` so unused-import lint stays honest.
Worker pools can hide leaked mocks
`afterEach(() => vi.restoreAllMocks())` and prefer forks if tests share modules.