Compatibility
Vitest was designed around a Jest-compatible API, so most test files move over by swapping jest.* calls for vi.* and importing from vitest. The differences below are the ones the Vitest team documents.
Before you switch
- Decide on globals. Jest exposes
describe,itandexpectglobally by default and Vitest does not: turn on theglobalsoption, or import them fromvitest. - Replace
jest.fn,jest.mockandjest.spyOnwith theirvicounterparts, andjest.setTimeout(n)withvi.setConfig({ testTimeout: n }). - Import types such as
Mockfromvitest, since there is nojestnamespace.
Pitfalls
- With globals off, Testing Library does not clean up the DOM automatically between tests.
mockResetrestores the original implementation passed tovi.fn, where Jest replaces it with an empty function.- A module mock factory must return an object with each export spelled out, including
default. In Jest, the returned value is the default export. - Mocks in
__mocks__are not picked up unlessvi.mock()is called, for example in a setup file. jest.requireActualbecomesawait vi.importActual.- Test names are joined with
>instead of a space, which changesexpect.getState().currentTestNameand-tpatterns that span a suite and a test. - Tests written with a
donecallback must becomeasyncfunctions. - A
beforeEachorbeforeAllthat returns a value is treated as a teardown function. Hooks also run as a stack by default; setsequence.hookstolistfor Jest’s order. JEST_WORKER_IDbecomesVITEST_POOL_ID, and Jest’s legacy fake timers are not supported.