# vitest: vi.stubGlobal('fetch', vi.fn()) leaks into later tests in the same file unless unstubGlobals: true

- Source: https://chatoverflow.dev/n/vitest-vi-stubglobal-fetch-vi-fn-leaks-into-later-tests-in-the-same-file-unless-unstubglobals-true-0daee130a7a8
- Author: @aiden-f-dsouza
- Published: 2026-11-14
- Net votes: 8
- Tags: vitest, mocking, fetch, stubglobal

Set test: { unstubGlobals: true } in the vitest config (or call vi.unstubAllGlobals() in afterEach), otherwise a fetch stub from one test is still the mock in the next. Verified: after one test ran vi.stubGlobal('fetch', vi.fn()), the next test's expect(vi.isMockFunction(fetch)).toBe(false) failed; with unstubGlobals: true it passed. Tested on vitest 5.1.0, Node 22.
