Configuring TypeScript for Test Files
You'll learn how to make TypeScript recognize Vitest testing functions and properly type-check your test files without compiler errors.
Why TypeScript Needs Test Configuration
By default, TypeScript doesn't know about Vitest's global functions like describe, it, expect, or vi. When you write tests, your IDE shows red squiggles everywhere, and tsc throws errors about undefined variables. You need to tell TypeScript two things: which type definitions to include, and which files count as tests.
The Two-Part Fix
First, add Vitest types to your tsconfig.json:
{
"compilerOptions": {
"types": ["vitest/globals"]
}
}
This pulls in Vitest's type definitions, making all global test functions recognized by TypeScript.
Second, include test file patterns:
{
"include": [
"src/**/*",
"src/**/*.test.ts",
"src/**/*.test.tsx",
"src/**/*.spec.ts",
"src/**/*.spec.tsx"
]
}
This tells TypeScript to treat your test files as part of the project. Without this, TypeScript might ignore them entirely during type-checking.
Complete Example
{
"compilerOptions": {
"target": "ES2020",
"module": "ESNext",
"jsx": "react-jsx",
"types": ["vitest/globals"],
"lib": ["ES2020", "DOM", "DOM.Iterable"]
},
"include": [
"src/**/*.ts",
"src/**/*.tsx",
"src/**/*.test.ts",
"src/**/*.test.tsx"
]
}
Now your IDE autocompletes expect(), shows proper error messages, and type-checks assertions like expect(result).toBe(5).
Key Takeaway: Adding "types": ["vitest/globals"] to compilerOptions and test patterns to include gives you full TypeScript support in test files with proper IDE autocomplete and type safety.