Code Reference

Migration to Vitest

Jest · Reference cheat sheet

Migration to Vitest

Jest · Reference cheat sheet


📋 Overview

Vitest is Jest-compatible for many APIs (describe, expect, vi.fn) with Vite-native ESM speed. Migrate incrementally: install Vitest, map config, replace Jest globals, fix mocks.

🔧 Core concepts

JestVitest
jest.fnvi.fn
jest.mockvi.mock
jest.spyOnvi.spyOn
jest.useFakeTimersvi.useFakeTimers
jest.config.jsvitest.config.ts
jsdom envenvironment: 'jsdom'

Keep @testing-library/* — works with both.

💡 Examples

Install:

npm i -D vitest jsdom @vitest/coverage-v8

vitest.config.ts:

import { defineConfig } from 'vitest/config';

export default defineConfig({
  test: {
    globals: true,
    environment: 'jsdom',
    setupFiles: ['./vitest.setup.ts'],
    coverage: { provider: 'v8', reporter: ['text', 'html'] },
  },
});

API swap:

// before
jest.spyOn(api, 'get').mockResolvedValue(1);
// after
import { vi } from 'vitest';
vi.spyOn(api, 'get').mockResolvedValue(1);

Scripts:

{
  "scripts": {
    "test": "vitest run",
    "test:watch": "vitest"
  }
}

Codemod mindset: replace jest.vi., update snapshot paths, re-check module mocks under ESM.

⚠️ Pitfalls

  • Assuming 100% Jest mock hoisting parity under native ESM.
  • Leaving @types/jest conflicting with Vitest types.
  • Transforming with Jest while running Vitest (duplicate configs).
  • Coverage provider/path differences breaking CI thresholds.
  • Migrating everything at once — prefer package-by-package in monorepos.

On this page