@@ -52,6 +52,21 @@ function mockClear(): MockInstance<T>
52
52
53
53
Clears all information about every call . After calling it , all properties on ` .mock ` will return to their initial state . This method does not reset implementations . It is useful for cleaning up mocks between different assertions .
54
54
55
+ ` ` ` ts
56
+ const person = {
57
+ greet: (name: string) => ` Hello $ {name }` ,
58
+ }
59
+ const spy = vi.spyOn(person, 'greet').mockImplementation(() => 'mocked')
60
+ expect(person.greet('Alice')).toBe('mocked')
61
+ expect(spy.mock.calls).toEqual([['Alice']])
62
+
63
+ // clear call history but keep mock implementation
64
+ spy.mockClear()
65
+ expect(spy.mock.calls).toEqual([])
66
+ expect(person.greet('Bob')).toBe('mocked')
67
+ expect(spy.mock.calls).toEqual([['Bob']])
68
+ ` ` `
69
+
55
70
To automatically call this method before each test , enable the [` clearMocks ` ](/ config/ #clearmocks ) setting in the configuration .
56
71
57
72
## mockName
@@ -197,14 +212,30 @@ await asyncMock() // throws Error<'Async error'>
197
212
function mockReset(): MockInstance<T>
198
213
` ` `
199
214
200
- Does what ` mockClear ` does and resets inner implementation to the original function .
215
+ Does what [ ` mockClear ` ](# mockClear ) does and resets inner implementation to the original function .
201
216
This also resets all "once" implementations.
202
217
203
218
Note that resetting a mock from ` vi.fn() ` will set implementation to an empty function that returns `undefined`.
204
219
resetting a mock from `vi.fn(impl )` will restore implementation to `impl`.
205
220
206
221
This is useful when you want to reset a mock to its original state .
207
222
223
+ ` ` ` ts
224
+ const person = {
225
+ greet: (name: string) => ` Hello $ {name }` ,
226
+ }
227
+ const spy = vi.spyOn(person, 'greet').mockImplementation(() => 'mocked')
228
+ expect(person.greet('Alice')).toBe('mocked')
229
+ expect(spy.mock.calls).toEqual([['Alice']])
230
+
231
+ // clear call history and reset implementation, but method is still spied
232
+ spy.mockReset()
233
+ expect(spy.mock.calls).toEqual([])
234
+ expect(person.greet).toBe(spy)
235
+ expect(person.greet('Bob')).toBe('Hello Bob')
236
+ expect(spy.mock.calls).toEqual([['Bob']])
237
+ ` ` `
238
+
208
239
To automatically call this method before each test , enable the [` mockReset ` ](/ config/ #mockreset ) setting in the configuration .
209
240
210
241
## mockRestore
@@ -213,11 +244,27 @@ To automatically call this method before each test, enable the [`mockReset`](/co
213
244
function mockRestore(): MockInstance<T>
214
245
` ` `
215
246
216
- Does what ` mockReset ` does and restores original descriptors of spied - on objects .
247
+ Does what [ ` mockReset ` ](# mockReset ) does and restores original descriptors of spied - on objects .
217
248
218
249
Note that restoring a mock from ` vi.fn() ` will set implementation to an empty function that returns `undefined`.
219
250
Restoring a mock from `vi.fn(impl )` will restore implementation to `impl`.
220
251
252
+ ` ` ` ts
253
+ const person = {
254
+ greet: (name: string) => ` Hello $ {name }` ,
255
+ }
256
+ const spy = vi.spyOn(person, 'greet').mockImplementation(() => 'mocked')
257
+ expect(person.greet('Alice')).toBe('mocked')
258
+ expect(spy.mock.calls).toEqual([['Alice']])
259
+
260
+ // clear call history and restore spied object method
261
+ spy.mockRestore()
262
+ expect(spy.mock.calls).toEqual([])
263
+ expect(person.greet).not.toBe(spy)
264
+ expect(person.greet('Bob')).toBe('Hello Bob')
265
+ expect(spy.mock.calls).toEqual([])
266
+ ` ` `
267
+
221
268
To automatically call this method before each test , enable the [` restoreMocks ` ](/ config/ #restoremocks ) setting in the configuration .
222
269
223
270
## mockResolvedValue
0 commit comments