You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Regression in Vitest 5: spying on Set.prototype.add causes a stack overflowΒ #11298
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',()=>{constspy=vi.spyOn(Set.prototype,'add')try{newSet([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.
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.
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:
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.
Activity
hi-ogawa commentedon Sep 17, 2026
This specifically hit
Set.prototypebut technically other primitive likeMap/Array.prorotypeare 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 ofSet.prorotype.addseems 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:
fengmk2 commentedon Sep 17, 2026
Let me analyze further which other methods in the spy still need protection.
hi-ogawa commentedon Sep 17, 2026
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.addshould 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.fengmk2 commentedon Sep 17, 2026
fengmk2 commentedon Sep 17, 2026
@hi-ogawa We should defend as much as possible. All primitive types used in spy need to be considered.