You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Notethatrestoringamockcreatedwith`vi.fn()`willsettheimplementationtoanemptyfunction that returns `undefined`. Restoring a mock created with `vi.fn(impl)` will restore the implementation to `impl`.
218
+
Notethatrestoringamockfrom`vi.fn()`willsetimplementationtoanemptyfunction that returns `undefined`.
219
+
Restoring a mock from `vi.fn(impl)` will restore implementation to `impl`.
Copy file name to clipboardexpand all lines: docs/api/vi.md
+6-3
Original file line number
Diff line number
Diff line change
@@ -398,15 +398,18 @@ Checks that a given parameter is a mock function. If you are using TypeScript, i
398
398
399
399
### vi.clearAllMocks
400
400
401
-
Will call [`.mockClear()`](/api/mock#mockclear) on all spies. This will clear mock history, but not reset its implementation to the default one.
401
+
Calls [`.mockClear()`](/api/mock#mockclear) on all spies.
402
+
This will clear mock history without affecting mock implementations.
402
403
403
404
### vi.resetAllMocks
404
405
405
-
Will call [`.mockReset()`](/api/mock#mockreset) on all spies. This will clear mock history and reset its implementation to an empty function (will return `undefined`).
406
+
Calls [`.mockReset()`](/api/mock#mockreset) on all spies.
407
+
This will clear mock history and reset each mock's implementation to its original.
406
408
407
409
### vi.restoreAllMocks
408
410
409
-
Will call [`.mockRestore()`](/api/mock#mockrestore) on all spies. This will clear mock history and reset its implementation to the original one.
411
+
Calls [`.mockRestore()`](/api/mock#mockrestore) on all spies.
412
+
This will clear mock history, restore all original mock implementations, , and restore original descriptors of spied-on objects.
Copy file name to clipboardexpand all lines: docs/config/index.md
+6-3
Original file line number
Diff line number
Diff line change
@@ -1953,21 +1953,24 @@ Custom [commands](/guide/browser/commands) that can be imported during browser t
1953
1953
-**Type:**`boolean`
1954
1954
-**Default:**`false`
1955
1955
1956
-
Will call [`.mockClear()`](/api/mock#mockclear) on all spies before each test. This will clear mock history, but not reset its implementation to the default one.
1956
+
Will call [`.mockClear()`](/api/mock#mockclear) on all spies before each test.
1957
+
This will clear mock history without affecting mock implementations.
1957
1958
1958
1959
### mockReset
1959
1960
1960
1961
-**Type:**`boolean`
1961
1962
-**Default:**`false`
1962
1963
1963
-
Will call [`.mockReset()`](/api/mock#mockreset) on all spies before each test. This will clear mock history and reset its implementation to an empty function (will return `undefined`).
1964
+
Will call [`.mockReset()`](/api/mock#mockreset) on all spies before each test.
1965
+
This will clear mock history and reset each implementation to its original.
1964
1966
1965
1967
### restoreMocks
1966
1968
1967
1969
-**Type:**`boolean`
1968
1970
-**Default:**`false`
1969
1971
1970
-
Will call [`.mockRestore()`](/api/mock#mockrestore) on all spies before each test. This will clear mock history and reset its implementation to the original one.
1972
+
Will call [`.mockRestore()`](/api/mock#mockrestore) on all spies before each test.
1973
+
This will clear mock history, restore each implementation to its original, and restore original descriptors of spied-on objects..
Copy file name to clipboardexpand all lines: docs/guide/migration.md
+8
Original file line number
Diff line number
Diff line change
@@ -297,6 +297,14 @@ Jest has their [globals API](https://jestjs.io/docs/api) enabled by default. Vit
297
297
298
298
If you decide to keep globals disabled, be aware that common libraries like [`testing-library`](https://testing-library.com/) will not run auto DOM [cleanup](https://testing-library.com/docs/svelte-testing-library/api/#cleanup).
299
299
300
+
### `spy.mockReset`
301
+
302
+
Jest's [`mockReset`](https://jestjs.io/docs/mock-function-api#mockfnmockreset) replaces the mock implementation with an
303
+
empty function that returns `undefined`.
304
+
305
+
Vitest's [`mockReset`](/api/mock#mockreset) resets the mock implementation to its original.
306
+
That is, resetting a mock created by `vi.fn(impl)` will reset the mock implementation to `impl`.
307
+
300
308
### Module Mocks
301
309
302
310
When mocking a module in Jest, the factory argument's return value is the default export. In Vitest, the factory argument has to return an object with each export explicitly defined. For example, the following `jest.mock` would have to be updated as follows:
* Performs the same actions as `mockClear` and sets the inner implementation to an empty function (returning `undefined` when invoked). This also resets all "once" implementations. It is useful for completely resetting a mock to its default state.
201
+
* Does what `mockClear` does and resets inner implementation to the original function. This also resets all "once" implementations.
202
+
*
203
+
* Note that resetting a mock from `vi.fn()` will set implementation to an empty function that returns `undefined`.
204
+
* Resetting a mock from `vi.fn(impl)` will set implementation to `impl`. It is useful for completely resetting a mock to its default state.
202
205
*
203
206
* To automatically call this method before each test, enable the [`mockReset`](https://vitest.dev/config/#mockreset) setting in the configuration.
204
207
* @see https://vitest.dev/api/mock#mockreset
205
208
*/
206
209
mockReset(): this
207
210
/**
208
-
* Does what `mockReset` does and restores inner implementation to the original function.
211
+
* Does what `mockReset` does and restores original descriptors of spied-on objects.
209
212
*
210
213
* 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`.
211
214
* @see https://vitest.dev/api/mock#mockrestore
@@ -536,15 +539,14 @@ function enhanceSpy<T extends Procedure>(
* Calls [`.mockClear()`](https://vitest.dev/api/mock#mockclear) on every mocked function. This will only empty `.mock` state, it will not reset implementation.
316
+
* Calls [`.mockClear()`](https://vitest.dev/api/mock#mockclear) on every mocked function.
317
317
*
318
-
* It is useful if you need to clean up mock between different assertions.
318
+
* This will only empty `.mock` state, it will not affect mock implementations.
319
+
*
320
+
* This is useful if you need to clean up mocks between different assertions within a test.
319
321
*/
320
322
clearAllMocks: ()=>VitestUtils
321
323
322
324
/**
323
-
* Calls [`.mockReset()`](https://vitest.dev/api/mock#mockreset) on every mocked function. This will empty `.mock` state, reset "once" implementations and force the base implementation to return `undefined` when invoked.
325
+
* Calls [`.mockReset()`](https://vitest.dev/api/mock#mockreset) on every mocked function.
326
+
*
327
+
* This will empty `.mock` state, reset "once" implementations, and reset each mock's base implementation to its original.
324
328
*
325
-
* This is useful when you want to completely reset a mock to the default state.
329
+
* This is useful when you want to reset all mocks to their original states.
326
330
*/
327
331
resetAllMocks: ()=>VitestUtils
328
332
329
333
/**
330
-
* Calls [`.mockRestore()`](https://vitest.dev/api/mock#mockrestore) on every mocked function. This will restore all original implementations.
334
+
* Calls [`.mockRestore()`](https://vitest.dev/api/mock#mockrestore) on every mocked function.
335
+
*
336
+
* This will empty `.mock` state, restore all original mock implementations, and restore original descriptors of spied-on objects.
337
+
*
338
+
* This is useful for inter-test cleanup and/or removing mocks created by [`vi.spyOn(...)`](https://vitest.dev/api/vi#vi-spyon).
0 commit comments