3.6 KiB
Getting Started
Overview
Vitest is a blazing fast unit test framework powered by Vite.
You can learn more about the rationale behind the project in the Why Vite section.
Trying Vitest Online
You can try Vitest online on StackBlitz. It runs Vitest directly in the browser, and it is almost identical to the local setup but doesn't require installing anything on your machine.
Adding Vitest to your Project
$ npm install -D vitest
:::tip Vitest requires Vite >=v2.7 and Node >=v14 :::
Configuring Vitest
One of the main advantages of Vitest is its unified configuration with Vite. If present, vitest will read your root vite.config.ts to match with the plugins and setup as your Vite app. For example, your Vite resolve.alias and plugins configuration will work out-of-the-box. If you want a different configuration during testing, you can:
- Create
vitest.config.ts, which will have the higher priority - Pass
--configoption to CLI, e.g.vitest --config ./path/to/vitest.config.ts - Use
process.env.VITESTto conditionally apply different configuration invite.config.ts
To configure vitest itself, add test property in your Vite config
// vite.config.ts
import { defineConfig } from 'vite'
export default defineConfig({
test: {
// ...
},
})
See the list of config options in the Config Reference
Command Line Interface
In a project where Vitest is installed, you can use the vitest binary in your npm scripts, or run it directly with npx vitest. Here is the default npm scripts in a scaffolded Vite project:
{
"scripts": {
"test": "vitest",
"coverage": "vitest --coverage"
}
}
You can specify additional CLI options like --port or --https. For a full list of CLI options, run npx vite --help in your project.
Examples
- Unit Testing
- Vue Component Testing
- React Component Testing
- Svelte Component Testing
- Lit Component Testing
- Vitesse Component Testing
Projects using Vitest
Using Unreleased Commits
If you can't wait for a new release to test the latest features, you will need to clone the vitest repo to your local machine and then build and link it yourself (pnpm is required):
git clone https://github.com/vitest-dev/vitest.git
cd vitest
pnpm install
cd packages/vitest
pnpm run build
pnpm link --global # you can use your preferred package manager for this step
Then go to the project where you are using Vitest and run pnpm link --global vitest (or the package manager that you used to link vitest globally).
Community
If you have questions or need help, reach out to the community at Discord and GitHub Discussions.