Skip to content

feat!: clear mocks by default before each test#10613

Merged
sheremet-va merged 4 commits into
vitest-dev:mainfrom
sheremet-va:feat/clear-mocks-by-default
Jun 30, 2026
Merged

feat!: clear mocks by default before each test#10613
sheremet-va merged 4 commits into
vitest-dev:mainfrom
sheremet-va:feat/clear-mocks-by-default

Conversation

@sheremet-va

@sheremet-va sheremet-va commented Jun 18, 2026

Copy link
Copy Markdown
Member

Description

See #9664 (comment)

Please don't delete this checklist! Before submitting the PR, please make sure you do the following:

  • It's really useful if your PR references an issue where it is discussed ahead of time. If the feature is substantial or introduces breaking changes without a discussion, PR might be closed.
  • Ideally, include a test that fails without this PR but passes with it.
  • Please, don't make changes to pnpm-lock.yaml unless you introduce a new test example.
  • Please check Allow edits by maintainers to make review process faster. Note that this option is not available for repositories that are owned by Github organizations.

Tests

  • Run the tests with pnpm test:ci.

Documentation

  • If you introduce new functionality, document it. You can run documentation with pnpm run docs command.

Changesets

  • Changes in changelog are generated from PR name. Please, make sure that it explains your changes in an understandable manner. Please, prefix changeset messages with feat:, fix:, perf:, docs:, or chore:.

@sheremet-va
sheremet-va marked this pull request as draft June 18, 2026 10:19
@netlify

netlify Bot commented Jun 18, 2026

Copy link
Copy Markdown

Deploy Preview for vitest-dev ready!

Name Link
🔨 Latest commit 68a591a
🔍 Latest deploy log https://app.netlify.com/projects/vitest-dev/deploys/6a424777c8dc32000815cca5
😎 Deploy Preview https://deploy-preview-10613--vitest-dev.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.
🤖 Make changes Run an agent on this branch

To edit notification comments on pull requests, go to your Netlify project configuration.

@sheremet-va sheremet-va added this to the 5.0.0 milestone Jun 29, 2026
@sheremet-va
sheremet-va marked this pull request as ready for review June 29, 2026 10:23
Comment thread docs/guide/migration.md
```ts
import { expect, test, vi } from 'vitest'

const fn = vi.fn()

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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")

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, it's the same

@sheremet-va
sheremet-va merged commit 0f6463b into vitest-dev:main Jun 30, 2026
15 of 18 checks passed
@sheremet-va
sheremet-va deleted the feat/clear-mocks-by-default branch June 30, 2026 06:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants