Skip to content
Latchkey

Vitest "ReferenceError: describe is not defined" (globals) in CI

Unlike Jest, Vitest does not inject describe, it, and expect as globals by default. Either import them from vitest in each test, or set globals: true in the config so they are available everywhere.

What this error means

Tests fail with "ReferenceError: describe is not defined" or "expect is not defined", often only in CI where a local IDE config masked the difference.

Vitest
ReferenceError: describe is not defined
 ❯ src/sum.test.ts:1:1
      1| describe('sum', () => {
       | ^

Common causes

globals is not enabled and APIs are not imported

Vitest keeps test APIs off the global scope by default, so a Jest-style test that never imports them fails.

The globals TypeScript types were assumed

The code relied on vitest/globals types being present without actually turning on globals: true at runtime.

How to fix it

Enable globals in the config

  1. Set globals: true under test in the config.
  2. Add vitest/globals to types in tsconfig for editor support.
  3. Re-run so the APIs are injected globally.
vitest.config.ts
export default defineConfig({
  test: { globals: true },
})

Or import the APIs explicitly

Keep globals off and import what each file uses; this is the most portable option.

src/sum.test.ts
import { describe, it, expect } from 'vitest'

How to prevent it

  • Decide once: globals on, or import in every test.
  • Add vitest/globals to tsconfig types when using globals.
  • Do not assume Jest-style globals carry over to Vitest.

Frequently asked questions

What causes ""ReferenceError: describe is not defined""?
Vitest keeps test APIs off the global scope by default, so a Jest-style test that never imports them fails.
How do I fix "ReferenceError: describe is not defined"?
Enable globals in the config

Related guides

References

Latchkey auto-heals failures like this one - detected, fixed, and retried without you. Start free → 30-day trial · No credit card