Skip to content
masterclass Buddy

Vitest · Type Alias

Mock

Vitest mock functions record calls and let tests control behavior. Use vi.fn for a new mock and vi.spyOn for an existing object's method.

Last updated

Since
4.1.0 for mockThrow and mockThrowOnce; otherwise not stated

Syntax

const mock = vi.fn(implementation?)
const spy = vi.spyOn(object, method)
mock.mockImplementation(fn)
mock.mockReturnValue(value)
mock.mockResolvedValue(value)
mock.mockRejectedValue(value)

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

Subscribe
  • vi.fn
  • vi.spyOn
  • mockClear
  • mockReset
  • mockRestore
  • mockImplementation
  • mockImplementationOnce
  • mockResolvedValue

Source: Mocks | Guide | Vitest. Checked 2026-11-03.