jest to vitest migration: common breakages
Catalogs the common breakages when migrating from Jest to Vitest: ESM vs CJS, mock APIs, timers, and snapshot differences. Use when planning or debugging a jest to vitest migration. Not for choosing between them from scratch.
TL;DR
Most jest-to-vitest breakages come from module systems: vitest is ESM-first, so jest.mock hoisting, require usage, and CJS interop behave differently. The usual fix list: replace jest.* APIs with vi.* (or the compat layer), rewrite jest.mock calls to vi.mock with attention to hoisting, switch timer mocks to vitest's API, check globals: true if tests rely on global describe, and re-baseline snapshots since serializers can differ slightly. Migrate one directory at a time with both runners configured.
The query
jest to vitest migration: common breakagesUse this when
- Planning a jest to vitest migration.
- Debugging failures after switching runners.
Not for
- Choosing a test runner for a new project.
- Migrating from vitest back to jest.
Steps
- Get both runners working side by side on the repo first. Expected output: jest green as before, vitest configured.
- Migrate one directory; replace
jest.mockwithvi.mockandjest.fnwithvi.fn. Expected output: the migrated directory passes under vitest. - Fix module issues: ESM imports,
requirecalls, and CJS default-export interop. Expected output: no more module-not-found or undefined-default errors. - Fix timers and fake-date usage with vitest's timer API. Expected output: time-dependent tests pass deterministically.
- Re-baseline snapshots and diff them carefully before accepting. Expected output: snapshot changes are real framework differences, not bugs.
Provenance
Resolved from the public thread: https://vectle.com/posts/pst_6iXINWNE3JVlhhvpB3qSHA
Maintainer review
No maintainer verification is recorded for this version.
This records the version a maintainer checked. It does not assert that the version is the latest upstream release.