Skip to content

vi.spyOn behavior regressed after upgrading Vitest (3.1.1 → 3.2.4) #9200

@simeng-li

Description

@simeng-li

Describe the bug

After upgrading Vitest from 3.1.1 to 3.2.4, tests that rely on top-level vi.spyOn + vi.clearAllMocks() start failing.

This setup worked in 3.1.1. After upgrading, call-count assertions consistently fail, implying the spy stops collecting calls once clearAllMocks runs. There’s no mention of this behavioral change in the release notes.

Need some help clarifying the expected behavior and best practices.

Reproduction

  1. const funcSpy = vi.spyOn(queries, 'getXXX') defined at module scope.
  2. beforeEach(() => vi.clearAllMocks())
  3. Tests that assert expect(funcSpy).toHaveBeenCalledTimes(1) or .toHaveBeenValledWith(...) start fail. Calls not tracked.
  4. However, the mocked return values from funcSpy.mockReturnedValueOnce still work as expected for each test case.

System Info

System:
    OS: macOS 26.1
    CPU: (10) arm64 Apple M1 Pro
    Memory: 76.73 MB / 32.00 GB
    Shell: 5.9 - /bin/zsh
  Binaries:
    Node: 22.14.0 - /Users/simengli/.nvm/versions/node/v22.14.0/bin/node
    npm: 10.9.2 - /Users/simengli/.nvm/versions/node/v22.14.0/bin/npm
    pnpm: 10.18.2 - /Users/simengli/Library/pnpm/pnpm
  Browsers:
    Chrome: 142.0.7444.176
    Safari: 26.1
  npmPackages:
    vitest: ^3.2.4 => 3.2.4

Used Package Manager

npm

Validations

Metadata

Metadata

Assignees

No one assigned

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions