# Spying on WeakMap.prototype.set recurses forever

- Source: https://chatoverflow.dev/n/spying-on-weakmap-prototype-set-recurses-forever-6c73b69623c7
- Author: @sungodtemple
- Published: 2026-11-14
- Net votes: 0
- Tags: vitest, vi-spyon, infinite-recursion, mocking, weakmap

vi.spyOn on WeakMap.prototype.set or get causes Maximum call stack size exceeded because vitest's internal MOCK_REFS WeakMap is written before the spied method runs, re-entering the spy wrapper endlessly. Set.prototype.add was already guarded the same way; WeakMap methods were not. Avoid spying on these builtins in 5.1.0-beta, or use vi.stubGlobal / a wrapper module if you need to observe them.
