docs/guide/index.md
Vitest (pronounced as "veetest") is a next generation testing framework powered by Vite.
You can learn more about the rationale behind the project in the Why Vitest section.
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.
<CourseLink href="https://vueschool.io/lessons/how-to-install-vitest?friend=vueuse">Learn how to install by Video</CourseLink>
::: code-group
npm install -D vitest
yarn add -D vitest
pnpm add -D vitest
bun add -D vitest
:::
:::tip Vitest requires Vite >=v6.0.0 and Node >=v20.0.0 :::
It is recommended that you install a copy of vitest in your package.json, using one of the methods listed above. However, if you would prefer to run vitest directly, you can use npx vitest (the npx tool comes with npm and Node.js).
The npx tool will execute the specified command. By default, npx will first check if the command exists in the local project's binaries. If it is not found there, npx will look in the system's $PATH and execute it if found. If the command is not found in either location, npx will install it in a temporary location prior to execution.
As an example, we will write a simple test that verifies the output of a function that adds two numbers.
export function sum(a, b) {
return a + b
}
import { expect, test } from 'vitest'
import { sum } from './sum.js'
test('adds 1 + 2 to equal 3', () => {
expect(sum(1, 2)).toBe(3)
})
::: tip
By default, tests must contain .test. or .spec. in their file name.
:::
Next, in order to execute the test, add the following section to your package.json:
{
"scripts": {
"test": "vitest"
}
}
Finally, run npm run test, yarn test or pnpm test, depending on your package manager, and Vitest will print this message:
✓ sum.test.js (1)
✓ adds 1 + 2 to equal 3
Test Files 1 passed (1)
Tests 1 passed (1)
Start at 02:15:44
Duration 311ms
::: warning
If you are using Bun as your package manager, make sure to use bun run test command instead of bun test, otherwise Bun will run its own test runner.
:::
Your first test is passing! Continue to Writing Tests to learn about organizing tests, reading test output, and the core testing patterns you'll use every day.
To run tests once without watching for file changes, use vitest run. You can also pass additional flags like --reporter or --coverage. For a full list of CLI options, run npx vitest --help or see the CLI guide.
Vitest reads your vite.config.* by default, so your existing Vite plugins and configuration work out-of-the-box. You can also create a dedicated vitest.config.* for test-specific settings. See the Config Reference for details.
We also provided an official extension for Visual Studio Code to enhance your testing experience with Vitest.
Install from VS Code Marketplace
Learn more about IDE Integrations
| Example | Source | Playground |
|---|---|---|
basic | GitHub | Play Online |
fastify | GitHub | Play Online |
in-source-test | GitHub | Play Online |
lit | GitHub | Play Online |
vue | GitHub | Play Online |
marko | GitHub | Play Online |
preact | GitHub | Play Online |
qwik | GitHub | Play Online |
react | GitHub | Play Online |
solid | GitHub | Play Online |
svelte | GitHub | Play Online |
profiling | GitHub | Not Available |
typecheck | GitHub | Play Online |
projects | GitHub | Play Online |
If you have questions or need help, reach out to the community at Discord and GitHub Discussions.