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.
SubscribeRelated
vi.fnvi.spyOnmockClearmockResetmockRestoremockImplementationmockImplementationOncemockResolvedValue