VitestのmockClear, mockReset, mockRestore
vi.mockClear()
Clears all information about every call. After calling it, all properties on .mock will return empty state. This method does not reset implementations. It is useful if you need to clean up mock between different assertions.
vi.clearAllMocks()
vi.mockReset()
Does what mockClear does and makes inner implementation an empty function (returning undefined when invoked). This also resets all “once” implementations. This is useful when you want to completely reset a mock to the default state.
vi.resetAllMocks()
vi.mockRestore()
Does what mockReset does and restores inner implementation to the original function.
Note that restoring mock from vi.fn() will set implementation to an empty function that returns undefined. Restoring a vi.fn(impl) will restore implementation to impl.
vi.restoreAllMocks()
全てのspyに対してmockRestore()
を呼ぶ。
例えば、以下のコードでは
vi.restoreAllMocks()
を呼ぶまでは10を返すgetApples()
関数のモックが有効vi.restoreAllMocks()
を呼んだ後は42を返すgetApples()
関数の元の実装が有効
となる。