TypeScript rejects the test block in vite.config.ts
'test' does not exist in type 'UserConfigExport'Short answer
Vite's own types do not know the test option, so TypeScript flags it. Tell TypeScript about Vitest by adding the triple-slash reference types line at the top of vite.config.ts, or import defineConfig from vitest/config instead of vite. Either change adds the test types, and the error goes away.
Fix it fast
1Find out which cause you have
Look at the first lines of vite.config.ts. See where defineConfig comes from and whether a reference types line exists.
head -n 5 vite.config.ts2Match what you see, then jump to its fix
What does the error look like?
'test' does not exist in type 'UserConfigExport'
Cause 1 of 2
The Vitest types are not loaded in a config that imports from vite
1Check
Look for the line referencing vitest/config at the very top of the file.
grep -n 'vitest/config' vite.config.ts2Fix
Add the triple-slash reference so the test options are included in the types.
/// <reference types="vitest/config" />
import { defineConfig } from 'vite'
export default defineConfig({
test: {
// options here
},
})3Verify
The editor error disappears and tsc reports no error for the file.
npx tsc --noEmitConfirmed by vitest.dev
Cause 2 of 2
defineConfig should come from vitest/config
1Check
Check the import line of defineConfig.
grep -n 'defineConfig' vite.config.ts vitest.config.ts2Fix
Import defineConfig from vitest/config instead of vite.
import { defineConfig } from 'vitest/config'
export default defineConfig({
test: {
// options here
},
})3Verify
Run the tests.
npx vitest runConfirmed by vitest.dev
How do I stop it coming back?
- Pick one approach per project and keep it in the config template.
- Run tsc --noEmit in CI so type errors in the config are caught.
Sources
How was this page made?
I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.
Which stack is behind this page?
Projects of mine built on the same technologies:
- TaxiDexMy own multi-tenant taxi dispatch SaaS: API, dispatcher portal, driver and passenger apps, and the servers underneath.
- QistLockA NestJS and React SaaS driving an Android Device Owner agent that locks financed phones when payments lapse.
- TaskvareA task and project-management suite: React on the web, Flutter on mobile, TypeScript underneath.
Related fixes
Web apps
tsconfig rejects allowImportingTsExtensions
An error that allowImportingTsExtensions needs noEmit or emitDeclarationOnly
allowImportingTsExtensions lets files import each other with .ts or .tsx extensions, and TypeScript only allows it together with noEmit or emitDeclarationOnly.
2 causes, each with a fixWeb apps
TypeScript refuses JSX because the jsx option is not set
TS17004: Cannot use JSX unless the '--jsx' flag is provided.
TypeScript compiles JSX only when you name the file with a .tsx extension and enable the jsx compiler option.
2 causes, each with a fixWeb apps
TypeScript rejects a component that returns an array or other non-Element
Its return type 'Element[]' is not a valid JSX element.
TypeScript before 5.1 required every component to return JSX.Element or null, so arrays, strings and Promises were rejected even though React renders them.
2 causes, each with a fixWeb apps
Cypress refuses to start with 'Your configFile is invalid'
Your configFile threw an error from: /path/to/cypress.config.ts
Cypress could not load cypress.config.js or .ts, or found something wrong in it.
5 causes, each with a fixWeb apps
ESLint cannot decide which copy of the @typescript-eslint plugin to use
The message lists two file paths for the same plugin, each with the name of the config that loaded it
ESLint found the same plugin at two different file paths and will not pick one.
3 causes, each with a fixWeb apps
ESLint says it cannot resolve a path to a module that does exist
Unable to resolve path to module 'react'. import/no-unresolved
import/no-unresolved fails when eslint-plugin-import cannot find the file or package on disk with its resolver.
4 causes, each with a fix