Skip to content

Commit 2d4537f

Browse files
authored
docs: add mockClear/mockReset/mockRestore examples (#7633)
1 parent fcf3cf5 commit 2d4537f

File tree

2 files changed

+98
-2
lines changed

2 files changed

+98
-2
lines changed

docs/api/mock.md

Lines changed: 49 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,21 @@ function mockClear(): MockInstance<T>
5252

5353
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.
5454

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+
5570
To automatically call this method before each test, enable the [`clearMocks`](/config/#clearmocks) setting in the configuration.
5671

5772
## mockName
@@ -197,14 +212,30 @@ await asyncMock() // throws Error<'Async error'>
197212
function mockReset(): MockInstance<T>
198213
```
199214

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.
201216
This also resets all "once" implementations.
202217

203218
Note that resetting a mock from `vi.fn()` will set implementation to an empty function that returns `undefined`.
204219
resetting a mock from `vi.fn(impl)` will restore implementation to `impl`.
205220

206221
This is useful when you want to reset a mock to its original state.
207222

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+
208239
To automatically call this method before each test, enable the [`mockReset`](/config/#mockreset) setting in the configuration.
209240

210241
## mockRestore
@@ -213,11 +244,27 @@ To automatically call this method before each test, enable the [`mockReset`](/co
213244
function mockRestore(): MockInstance<T>
214245
```
215246

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.
217248

218249
Note that restoring a mock from `vi.fn()` will set implementation to an empty function that returns `undefined`.
219250
Restoring a mock from `vi.fn(impl)` will restore implementation to `impl`.
220251

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+
221268
To automatically call this method before each test, enable the [`restoreMocks`](/config/#restoremocks) setting in the configuration.
222269

223270
## mockResolvedValue

test/core/test/jest-mock.test.ts

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -559,4 +559,53 @@ describe('jest mock compat layer', () => {
559559
fn.mockImplementationOnce(temporaryMockImplementation)
560560
expect(fn.getMockImplementation()).toBe(temporaryMockImplementation)
561561
})
562+
563+
describe('docs example', () => {
564+
it('mockClear', () => {
565+
const person = {
566+
greet: (name: string) => `Hello ${name}`,
567+
}
568+
const spy = vi.spyOn(person, 'greet').mockImplementation(() => 'mocked')
569+
expect(person.greet('Alice')).toBe('mocked')
570+
expect(spy.mock.calls).toEqual([['Alice']])
571+
572+
// clear call history but keep mock implementation
573+
spy.mockClear()
574+
expect(spy.mock.calls).toEqual([])
575+
expect(person.greet('Bob')).toBe('mocked')
576+
expect(spy.mock.calls).toEqual([['Bob']])
577+
})
578+
579+
it('mockReset', () => {
580+
const person = {
581+
greet: (name: string) => `Hello ${name}`,
582+
}
583+
const spy = vi.spyOn(person, 'greet').mockImplementation(() => 'mocked')
584+
expect(person.greet('Alice')).toBe('mocked')
585+
expect(spy.mock.calls).toEqual([['Alice']])
586+
587+
// clear call history and reset implementation, but method is still spied
588+
spy.mockReset()
589+
expect(spy.mock.calls).toEqual([])
590+
expect(person.greet).toBe(spy)
591+
expect(person.greet('Bob')).toBe('Hello Bob')
592+
expect(spy.mock.calls).toEqual([['Bob']])
593+
})
594+
595+
it('mockRestore', () => {
596+
const person = {
597+
greet: (name: string) => `Hello ${name}`,
598+
}
599+
const spy = vi.spyOn(person, 'greet').mockImplementation(() => 'mocked')
600+
expect(person.greet('Alice')).toBe('mocked')
601+
expect(spy.mock.calls).toEqual([['Alice']])
602+
603+
// clear call history and restore spied object method
604+
spy.mockRestore()
605+
expect(spy.mock.calls).toEqual([])
606+
expect(person.greet).not.toBe(spy)
607+
expect(person.greet('Bob')).toBe('Hello Bob')
608+
expect(spy.mock.calls).toEqual([])
609+
})
610+
})
562611
})

0 commit comments

Comments
 (0)