How To Install Vitest
Prerequisites
Step 1 — Install Vitest
Step 2 — Configure Vitest
Browser Configuration
At the root of your project (whether React or NestJS), create a vitest.config.ts
file with the following content:
Create a setup.web.ts
file to configure the testing environment before running tests:
Node.js Configuration
Create a setup.node.ts
file to configure the testing environment before running tests:
Step 3 — Add Testing Scripts
Add the following scripts to your package.json to streamline the testing process:
Step 4 — Integrate Vitest with Your Code Editor
To enhance your development workflow, integrate Vitest with your code editor to run tests and view results seamlessly.
Visual Studio Code
Install the Vitest Extension:
Open VS Code
Go to the Extensions view by clicking the square con in the sidebar or pressing CTRL+SHIFT+X
Search for “Vitest” and install the extension by antfu
Configure the Extension:
The extension should automatically detect your Vitest configuration
You can run and debug tests directly from the editor’s interface
Run Tests from the Editor:
Open a test file
Click the run icon next to the test cases to execute them individually