feat!: clear mocks by default before each test#10613
Merged
sheremet-va merged 4 commits intoJun 30, 2026
Merged
Conversation
sheremet-va
marked this pull request as draft
June 18, 2026 10:19
✅ Deploy Preview for vitest-dev ready!
To edit notification comments on pull requests, go to your Netlify project configuration. |
sheremet-va
marked this pull request as ready for review
June 29, 2026 10:23
AriPerkkio
approved these changes
Jun 30, 2026
hi-ogawa
reviewed
Jun 30, 2026
| ```ts | ||
| import { expect, test, vi } from 'vitest' | ||
|
|
||
| const fn = vi.fn() |
Collaborator
There was a problem hiding this comment.
Just to confirm my understanding, does the example works same with vi.fn() from factory mock?
import { fn } from "some-lib"
vi.mock("some-lib", () => {
return { fn: vi.fn() }
})Also what about the if it is automock?
import { fn } from "some-lib"
vi.mock("some-lib")
Member
Author
There was a problem hiding this comment.
As long as they are created outside the test body, it works the same way. In your case the function is not called anywhere so it doesn't matter. If it was called in the factory, then its state would be empty in the first test
Collaborator
There was a problem hiding this comment.
I meant when the continuation is same as the example here i.e.
import { fn } from "some-lib"
vi.mock("some-lib", () => {
return { fn: vi.fn() }
})
test('first', () => {
fn()
expect(fn).toHaveBeenCalledTimes(1)
})
test('second', () => {
fn()
// v4: the call from "first" was kept, so this was 2 // [!code --]
// expect(fn).toHaveBeenCalledTimes(2) // [!code --]
// v5: history is cleared before each test, so only this test's call counts // [!code ++]
expect(fn).toHaveBeenCalledTimes(1) // [!code ++]
})import { fn } from "some-lib"
vi.mock("some-lib"); // assuming this exports `fn`
test('first', () => {
fn()
expect(fn).toHaveBeenCalledTimes(1)
})
test('second', () => {
fn()
// v4: the call from "first" was kept, so this was 2 // [!code --]
// expect(fn).toHaveBeenCalledTimes(2) // [!code --]
// v5: history is cleared before each test, so only this test's call counts // [!code ++]
expect(fn).toHaveBeenCalledTimes(1) // [!code ++]
})Are these pass?
hi-ogawa
approved these changes
Jun 30, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
See #9664 (comment)
Please don't delete this checklist! Before submitting the PR, please make sure you do the following:
pnpm-lock.yamlunless you introduce a new test example.Tests
pnpm test:ci.Documentation
pnpm run docscommand.Changesets
feat:,fix:,perf:,docs:, orchore:.