Fixing type errors with Vite, vitest, and one's UserConfig
I find myself in JavaScript codebases with Vite and vitest often enough that I've fixed and forgotten how to fix this error.
You won’t notice this during development or even when running your tests unless
you open your vitest.config.ts
file. One has to invoke their type checker,
which in some cases runs as late as during deployment.
The error in question explains that a seemingly innocuous configuration file identical to the documentation is invalid:
vitest.config.mts:6:3 - error ts(2353): Object literal may only specify known properties, and ‘test’ does not exist in type ‘UserConfig’.
Even with a minimal configuration when using a framework like Astro, the error presents itself.
/// <reference types="vitest" />
import { getViteConfig } from "astro/config";
export default getViteConfig({
test: {
include: ["test/**/*.{test,spec}.?(c|m)[jt]s?(x)"],
},
});
The issue stems from the interplay between versions of Vite used by vitest and
Astro. To rid ourselves of the tedious little scamp, one only need override
their vite
version like so (assuming one is using the hard-linking pnpm
):
"pnpm": {
"overrides": {
"vite": "6.3.5"
}
}