Skip to content
masterclass Buddy

Vitest · Function

vi.spyOn

vi.spyOn tracks calls to an object method or accessor while letting you keep or change its behavior. It returns a mock function that can be configured and restored.

Last updated

Syntax

function spyOn<T, K extends keyof T>(object: T, key: K, accessor?: 'get' | 'set'): Mock<T[K]>

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

Subscribe
  • vi.fn
  • vi.mock
  • vi.mocked
  • vi.clearAllMocks
  • vi.resetAllMocks
  • vi.restoreAllMocks

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