
The Mock Queue Trap: A Vitest 4 Bug That Took Hours to Find
vi.clearAllMocks() doesn't do what you think it does. And it cost me half a day. Originally published on Form Follows Function The Setup 10 tests failing. Routes returning 200 instead of 403. The ownership checks were correct — I proved it by running them in isolation. Same code, same mocks, passing alone, failing together. Classic test pollution, except nothing obvious was leaking. The test file had about 180 tests across multiple describe blocks. Each block had a tidy beforeEach(() => vi.clearAllMocks()) . Clean slate, right? The Rabbit Hole I wrote isolated debug tests. The routes worked perfectly. I traced mock states. I checked module caching. I read the Vitest source. Hours vanished. The failing tests were in describe blocks that ran after another set of tests — tests for routes that didn't exist yet (intentional TDD-style failing tests). Those earlier tests used mockResolvedValueOnce() to queue up return values for mock functions. But since the routes returned 404 before ever ca
Continue reading on Dev.to Webdev
Opens in a new tab

