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.

More Vitest tasks

Back to the full Vitest cheat sheet