Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Next.js and React

TypeScript rejects the test block in vite.config.ts

Error'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.

  • Applies to Vite 8, Vitest (current docs)
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 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.ts
  2. 2Match what you see, then jump to its fix

    1. 1defineConfig is imported from 'vite' and the file has a test blockThe Vitest types are not loaded in a config that imports from viteFix
    2. 2Your project does not otherwise need a separate Vite configdefineConfig should come from vitest/configFix

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.ts

2Fix

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 --noEmit

Confirmed by vitest.dev

Did this fix it?

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.ts

2Fix

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 run

Confirmed by vitest.dev

Did this fix it?

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:

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 fix

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.