Globals, Environments & Setup Files in Vitest

From the Vitest cheat sheet · Setup & Configuration · verified Nov 2026

Globals, Environments & Setup Files

Enable Jest-style globals, pick a DOM environment, and run setup code before each file

typescript
import { defineConfig } from 'vitest/config'

export default defineConfig({
  test: {
    globals: true,                      // describe/test/expect without imports
    environment: 'jsdom',               // npm i -D jsdom
    setupFiles: ['./vitest.setup.ts'],  // runs before every test file
  },
})

// tsconfig.json: "types": ["vitest/globals"]
📌 Globals are off by default; add vitest/globals to tsconfig types
🟢 node is the default environment; install jsdom or happy-dom for DOM
💡 setupFiles run before every file; globalSetup runs once per run
⚠️ Browser Mode is not an environment; use a browser project instead
globalsenvironmentjsdomsetupFiles

Continue with Vitest

Save the full cheat sheet or work through every related task.

More Vitest tasks

Back to the full Vitest cheat sheet