पाठ 2 / 25
Jest Versus Vitest
Two runners with a near-identical API.
Mature standard or Vite-native speed
Jest (originally from Meta, now under the OpenJS Foundation) has been the default JavaScript test runner for years: huge ecosystem, built-in mocking, snapshots, coverage and watch mode. TypeScript and modern syntax are handled by a transformer such as babel-jest, ts-jest or @swc/jest, and native ES modules support has historically needed extra configuration. Vitest is built on Vite: it reuses your Vite config, plugins and aliases, understands TypeScript and ESM out of the box, and offers a Jest-compatible API (describe, it, expect, vi.fn instead of jest.fn). Rule of thumb: in a Vite project (React + Vite, Vue, SvelteKit, Astro) pick Vitest; in an existing Jest codebase, or a framework whose tooling generates Jest config, Jest is a perfectly good choice. Most test code in this course works in both with only the jest/vi prefix changing.
The same test in both runners
Only the imports and the mock helper differ.
// sum.test.ts with Jest (globals are available by default)
import { sum } from './sum';
test('adds two numbers', () => {
const log = jest.fn();
expect(sum(2, 3, log)).toBe(5);
expect(log).toHaveBeenCalledWith(5);
});
// sum.test.ts with Vitest (explicit imports, or enable `globals: true`)
import { test, expect, vi } from 'vitest';
import { sum } from './sum';
test('adds two numbers', () => {
const log = vi.fn();
expect(sum(2, 3, log)).toBe(5);
expect(log).toHaveBeenCalledWith(5);
});Do not migrate for its own sake
Switching runners costs time and rarely changes what your tests prove. Migrate when the current setup actively hurts (slow transforms, ESM pain, duplicated Vite config), and check each tool's migration guide for your versions.
त्वरित जाँच: Why is Vitest a natural fit for a Vite project?
- It reuses the Vite config, plugins and aliases and handles TypeScript and ESM out of the box
- It is the only runner that supports snapshots
- It runs tests in a real browser by default
- It does not need assertions
Answer
It reuses the Vite config, plugins and aliases and handles TypeScript and ESM out of the box — Vitest shares the build pipeline the app already uses.