Skip to content

Regression in Vitest 5: spying on Set.prototype.add causes a stack overflowΒ #11298

Closed

Description

@fengmk2

Describe the bug

Spying on Set.prototype.add causes infinite recursion in Vitest 5.0.1 when the method is called:

import { expect, it, vi } from 'vitest'

it('can spy on Set.prototype.add', () => {
  const spy = vi.spyOn(Set.prototype, 'add')
  try {
    new Set([1])
    expect(spy).toHaveBeenCalledWith(1)
  } finally {
    spy.mockRestore()
  }
})

Expected: the test passes and the spy records the call, as it does on Vitest 4.1.10 and 4.1.11.

Actual: the test fails with RangeError: Maximum call stack size exceeded. The stack repeatedly enters Set.add in @vitest/spy/dist/index.js.

This also reproduces with @vitest/spy alone in plain Node, without the Vitest runner or Vite. Each mock invocation in 5.0.1's spy implementation calls DIRTY_MOCK_STATES.add(...), which calls the spied method again. This bookkeeping was added in #11092.

Reproduction

Minimal reproduction repository

git clone https://github.com/why-reproductions-are-required/vitest-set-add-spy-recursion.git
cd vitest-set-add-spy-recursion
npm ci
npm test

For the smaller Node-only reproduction:

npm run repro:spy

Both commands exit with code 1 and a stack overflow on 5.0.1, which was the latest stable release when checked on 2026-09-17. Both pass on 4.1.10 and 4.1.11. The README contains the comparison commands. Vite remains pinned to 8.3.0 in all cases.

System Info

System:
  OS: macOS 26.2
  CPU: (16) arm64 Apple M4 Max
Binaries:
  Node: 24.21.0
  npm: 11.19.0
npmPackages:
  @vitest/spy: 5.0.1
  vite: 8.3.0
  vitest: 5.0.1

Used Package Manager

npm

Validations

Codex assisted with preparing and running the reproduction and drafting this report.

Activity

  1. added
    p3-minor-bug
    on Sep 17, 2026
  2. added theissue type on Sep 17, 2026
  3. hi-ogawa commented on Sep 17, 2026

    @hi-ogawa
    Collaborator

    This specifically hit Set.prototype but technically other primitive like Map/Array.prorotype are used internally of course, so likely those mocking will crash Vitest. I'm not sure how far we should guard against this in general, but fixing this specific regression of Set.prorotype.add seems reasonable to fix.

    If we were to be completely free from primitive mocking effect, then it would require some primordial utility like Node does internally and stash all relevant prototype methods, which is too tedious to deal with, so it's likely out of scope:

    const setAdd = Set.prototype.add.call.bind(Set.prototype.add)
    const mapSet = Map.prototype.set.call.bind(Map.prototype.set)
    ...
  4. fengmk2 commented on Sep 17, 2026

    @fengmk2
    ContributorAuthor

    Let me analyze further which other methods in the spy still need protection.

  5. hi-ogawa commented on Sep 17, 2026

    @hi-ogawa
    Collaborator

    Ah sorry, I forgot to share what I dug in https://gist.github.com/hi-ogawa/a7ef94747e90c8a16108fa8888e63fdd#fix-direction and my impression is that it's out of scope to guard against all pre-existing global prototype mocking, but this specific Set.prototype.add should be fixed since that's rather simple and the regression is regression. I'm not sure how much we carefully guard things like these going forward though.

  6. fengmk2 commented on Sep 17, 2026

    @fengmk2
    Author
  7. fengmk2 commented on Sep 17, 2026

    @fengmk2
    ContributorAuthor

    @hi-ogawa We should defend as much as possible. All primitive types used in spy need to be considered.

  8. locked and limited conversation to collaborators on Oct 3, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    p3-minor-bug

    Type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions