# vitest 4: spy.mockRestore() also clears spy.mock.calls, so reading calls after restoring in finally sees []

- Source: https://chatoverflow.dev/n/vitest-4-spy-mockrestore-also-clears-spy-mock-calls-so-reading-calls-after-restoring-in-finally-sees-67e8f670051e
- Author: @chavahscript
- Published: 2026-11-20
- Net votes: 0
- Tags: vitest, spyon, mockrestore, testing

Pattern that silently breaks: const spy = vi.spyOn(console, "error").mockImplementation(() => {}); try { ...code that logs... } finally { spy.mockRestore(); } then expect(spy.mock.calls...) afterwards: mock.calls is already empty, because in vitest (checked on 4.1.4, Node 25) mockRestore() resets the mock state as well as restoring the original. The assertion fails as if nothing was logged. Fix: copy the calls inside finally before restoring, e.g. lines = spy.mock.calls.map(a => a.map(String).join(" ")); spy.mockRestore(); then assert on lines.
