Skip to content

clearMocks: true in vitest.config is not executed on test retry #7664

@JJBocanegra

Description

@JJBocanegra

Describe the bug

Using the reproduction link, you can see that with this configuration:

retry: 2,
clearMocks: true

Vitest won't execute the clearMocks when a test is retried.

Here is how it will look when a failing test is retried with this configuration:

   × Foo 4ms (retry x2)
     → expected "execute" to be called 6 times, but got 1 times
     → expected "execute" to be called 6 times, but got 2 times
     → expected "execute" to be called 6 times, but got 3 times

Now, if we add vi.clearAllMocks() to the beforeEach()or afterEach() of the same test file, or even the ones in the setup file, it will look like this, which is the expected behavior:

   × Foo 3ms (retry x2)
     → expected "execute" to be called 6 times, but got 1 times
     → expected "execute" to be called 6 times, but got 1 times
     → expected "execute" to be called 6 times, but got 1 times

Reproduction

Link to reproduction

System Info

System:
    OS: macOS 15.3.1
    CPU: (8) arm64 Apple M1
    Memory: 108.84 MB / 16.00 GB
    Shell: 5.9 - /bin/zsh
  Binaries:
    Node: 22.14.0 - ~/.local/state/fnm_multishells/89598_1741644445420/bin/node
    npm: 11.2.0 - ~/.local/state/fnm_multishells/89598_1741644445420/bin/npm
    pnpm: 10.6.1 - ~/.local/state/fnm_multishells/89598_1741644445420/bin/pnpm
  Browsers:
    Chrome: 134.0.6998.45
    Edge: 134.0.3124.51
    Safari: 18.3
  npmPackages:
    vitest: 3.0.8 => 3.0.8 
    vitest-when: 0.6.0 => 0.6.0

Used Package Manager

pnpm

Validations

Metadata

Metadata

Assignees

No one assigned

    Labels

    p3-minor-bugAn edge case that only affects very specific usage (priority)

    Type

    Projects

    Status

    Approved

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions