11

As described in this answer and in the docs. I've added the reference types for Vitest at the top of my Vite config file.

/// <reference types="vitest" /> 

Why am I still getting the TypeScript warning 'test' does not exist in type 'UserConfigExport'?

3
  • 1
    Hard to tell. What gives you the warning? VS Code or TSC? Did you try to restart IDE? Or try alternative solution - import { defineConfig } from 'vitest/config'; Commented Jul 19, 2022 at 8:12
  • 1
    Thanks, using defineConfig from vitest/config fixes the problem with test: {...}, but introduces problems in other areas (.e.g 'polyfillDynamicImport' does not exist in type 'BuildOptions'). Multiple IDE's give me the same warning even after restart. Commented Jul 19, 2022 at 9:23
  • Maybe version mismatch between Vite and Vitest ? Commented Jul 19, 2022 at 9:51

2 Answers 2

5

I have found a workaround for this problem - creating separate vitest.config.ts file. As docs states:

Create vitest.config.ts, which will have the higher priority and will override the configuration from vite.config.ts

Now you will have intellisense for test option.

Example vitest.config.ts file:

import { defineConfig } from 'vitest/config'; export default defineConfig({ test: { globals: true, environment: 'jsdom', coverage: { provider: 'istanbul', // or 'c8', all: true, }, }, }); 
Sign up to request clarification or add additional context in comments.

3 Comments

You deserve a star
I did it the same way, however it worked for me without any issues with single vite.config.ts before, but then I tried moving the project to monorepo with pnpm workspaces and then the issue appeared, I don't understand why though. If someone has an idea, let me know please.
There is warning in docs that vitest.config.ts will override vite.config.ts, so be careful, you might end up with some weird behavior. See docs :vitest.dev/guide/#configuring-vitest
4

I defined my own type and extended it to UserConfig.

... import type { InlineConfig } from 'vitest'; import type { UserConfig } from 'vite'; interface VitestConfigExport extends UserConfig { test: InlineConfig; } ... 

Then I casted the type of config object to my custom interface -

export default defineConfig({ plugins: [solidPlugin()], server: { port: 3000, }, test: { environment: 'jsdom', globals: true, transformMode: { web: [/\.[jt]sx?$/], }, setupFiles: './setupVitest.ts', }, build: { target: 'esnext', }, } as VitestConfigExport); 

This also enabled intellisense for new test property. Also, you don't need to define /// <reference types="vitest" />.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.