I've been working with Vue, and Vitest and was trying to get my IntelliJ configuration up to snuff so I could write and debug tests. I had a few problems along the way to get tests working at both the command line and inside IntelliJ.
Here are some of the problems.
Manual Import of Test Commands
I needed to manually import test commands like describe and It. IntelliJ could not auto import them for some reason. It is a pain to have to write this for every test file:
I initial fixed this by adding an include in the vitest.config.lts file:
That fixed the issue in the command line, and I could run tests without explicit imports. And IntelliJ no longer saw the lack of an import as an error. However IntelliJ started giving me a different error when trying to run a test.
Describe Not Defined
IntelliJ started telling me that "Describe was not defined" when trying to run the tests, even though command line worked fine. Being able to run and debug tests in IntelliJ is a great time saver, and I wanted to fix that.
After some searching on the error, I discovered that if I set globals to true, that solved the issue in all cases, like this:
Once I had this, I removed the global include from the previous section. Woot!
The Complete Config
Sometimes when reading about configs, people talk about properties and I never know where to share them, so here is my full config:
2import { mergeConfig, defineConfig, configDefaults } from "vitest/config";
3import viteConfig from "./vite.config"; // this is a standard vite config
4
5export default mergeConfig(
6 viteConfig,
7 defineConfig({
8 test: {
9 environment: "jsdom",
10 globals: true,
11 exclude: [...configDefaults.exclude]
12 },
13 })
14);
I hope this helps someone!