Skip to content
masterclass Buddy

Vitest · Function

vi.mock

vi.mock replaces imports from a module with a factory result, a mock file, or automatic mocks. Vitest hoists it before imports and caches the factory result.

Last updated

Syntax

function mock(path: string, factory?: MockOptions | MockFactory<unknown>): void
function mock<T>(module: Promise<T>, factory?: MockOptions | MockFactory<T>): void

In-depth API docs are only for subscribers: parameters, examples, common mistakes and interview practice.

Subscribe
  • vi.doMock
  • vi.hoisted
  • vi.importActual
  • vi.importMock
  • vi.unmock
  • vi.doUnmock
  • vi.resetModules
  • vi.mocked

Source: Vi | Vitest. Checked 2026-11-03.