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
Copy file name to clipboardExpand all lines: docs/guide/migration.md
+39Lines changed: 39 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -13,6 +13,45 @@ outline: deep
13
13
Vitest 5.0 is currently in beta. This section tracks breaking changes as they are merged and may change before the stable release.
14
14
:::
15
15
16
+
### `clearMocks` is Enabled by Default
17
+
18
+
[`clearMocks`](/config/#clearmocks) now defaults to `true`. Vitest calls [`vi.clearAllMocks()`](/api/vi#vi-clearallmocks) before every test, resetting the `mock.calls`, `mock.instances`, `mock.contexts` and `mock.results` of every mock. Mock implementations are left intact, so this only affects the recorded history.
19
+
20
+
In practice this means a mock no longer carries calls from one test into the next:
21
+
22
+
```ts
23
+
import { expect, test, vi } from'vitest'
24
+
25
+
const fn =vi.fn()
26
+
27
+
test('first', () => {
28
+
fn()
29
+
expect(fn).toHaveBeenCalledTimes(1)
30
+
})
31
+
32
+
test('second', () => {
33
+
fn()
34
+
// v4: the call from "first" was kept, so this was 2 // [!code --]
35
+
expect(fn).toHaveBeenCalledTimes(2) // [!code --]
36
+
// v5: history is cleared before each test, so only this test's call counts // [!code ++]
37
+
expect(fn).toHaveBeenCalledTimes(1) // [!code ++]
38
+
})
39
+
```
40
+
41
+
Tests that record calls outside of the test body (for example in a setup file, at the top level of a module, or in a `beforeAll` hook) are the most affected, because that history is cleared before the test that asserts on it runs.
42
+
43
+
To keep the previous behavior, set `clearMocks` back to `false`:
44
+
45
+
```ts [vitest.config.ts]
46
+
import { defineConfig } from'vitest/config'
47
+
48
+
exportdefaultdefineConfig({
49
+
test: {
50
+
clearMocks: false, // [!code ++]
51
+
},
52
+
})
53
+
```
54
+
16
55
### Benchmarking API Rewrite
17
56
18
57
The benchmarking API has been rewritten. `bench` is no longer a top-level import from `vitest`; it is a [test-context fixture](/guide/test-context#bench) accessed from inside a regular `test()`. See the [Benchmarking guide](/guide/benchmarking) for the new API.
0 commit comments