Skip to content

IntelliJ Red Squiggles for vi #1079

@LeighFinegold

Description

@LeighFinegold

Problem

After introducing Vitest in #1075, we started seeing red squiggles and type errors in IntelliJ when using global APIs like vi

Despite tests running perfectly via the CLI, IntelliJ flagged these names as undefined, leading to confusion.

Symptoms

  • IntelliJ reports: Cannot find name 'vi'
  • Autocomplete and jump-to-definition doesn't work for describe, expect, vi, etc.
  • Only affects .spec.ts or .test.ts files

Image

Image

Investigation

After reviewing the Vitest docs and how tsconfig.json interacts with both the build and test processes, I found the following:

1. Vitest uses esbuild, not tsc

Vitest compiles .ts files using esbuild internally. That means your .spec.ts files are never transpiled by tsc unless you explicitly call it (e.g., tsc -p .).

2. TypeScript does not include Vitest globals unless configured

If your tsconfig.json does not explicitly include "types": ["vitest/globals"], then vi, describe, etc., are not recognized as global symbols — even if they are injected at runtime by Vitest.

3. Excluding test files breaks global types

To avoid emitting test files into dist/, many people use:

"exclude": ["**/*.spec.ts"]

This unfortunately removes test files from the TypeScript program entirely — which also removes access to their global types in the IDE.

4. It works at runtime because Vitest ignores tsconfig.exclude

Vitest doesn't care whether test files are excluded in tsconfig.json. It will still find and compile them using its own resolver (esbuild). So the tests run fine.

5. The solution

One solution is to include test files in tsconfig.json (for IDE and Vitest type support), but exclude them in a tsconfig.build.json (used for production builds).

Open to other fixes:

For example:

An alternative is to not apply vitest globally and reference specifically with

import { vi } from 'vitest';

another solution seems to be

  • Create a file called vitest-globals.d.ts (anywhere inside src or root) and updating tsconfig.json
"include": ["src", "vitest-globals.d.ts"]

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions