Clear, Reset & Restore in Vitest

From the Vitest cheat sheet ยท Mock Functions ยท verified Nov 2026

Clear, Reset & Restore

Reset mock state between tests and understand the Vitest 5 clearMocks default

typescript
fn.mockClear()      // forget calls, keep implementation
fn.mockReset()      // forget calls + reset to the original impl
spy.mockRestore()   // reset + put the real method back (spyOn)

vi.clearAllMocks()
vi.resetAllMocks()
vi.restoreAllMocks()
๐Ÿ“Œ v5 turns clearMocks on by default; set it false for v4 behavior
โš ๏ธ Calls recorded in beforeAll or at module level are now cleared
๐Ÿ” Unlike Jest, mockReset restores vi.fn(impl) to impl, not a no-op
๐ŸŽฏ restoreMocks: true is a safe default if you use vi.spyOn a lot
mockClearmockResetmockRestoreclearMocks

Continue with Vitest

Save the full cheat sheet or work through every related task.

More Vitest tasks

Back to the full Vitest cheat sheet