vi.stubGlobal & vi.stubEnv in Vitest
From the Vitest cheat sheet ยท Globals, Env & Network ยท verified Nov 2026
vi.stubGlobal & vi.stubEnv
Override globals and environment variables and restore them cleanly
typescript
vi.stubGlobal('innerWidth', 375)
vi.stubEnv('NODE_ENV', 'production')
vi.stubEnv('VITE_API_URL', 'https://staging.example.com')
vi.unstubAllGlobals()
vi.unstubAllEnvs()๐ก Stubs restore cleanly; plain assignment can't be undone for you
๐ stubEnv updates both process.env and import.meta.env
๐ฏ Turn on unstubEnvs and unstubGlobals to reset automatically
๐ Pass undefined to vi.stubEnv to simulate a missing variable
stubGlobalstubEnvenv
Continue with Vitest
Save the full cheat sheet or work through every related task.