Sub topic 1
Installing Vitest and Running Your First Test
Vitest is a test runner for JavaScript and TypeScript built on Vite. It finds your test files, runs them, and reports which assertions passed or failed. It runs ES modules without any extra setup, and its watch mode reruns only the tests affected by a change. This chapter covers installing it, writing one test, and running it in both modes.
Prerequisites
This chapter assumes you have:
- Node.js 20 or later installed (check with
node --version). - Working knowledge of ES module syntax (
importandexport). - Experience using npm and running commands in a terminal.
Installing Vitest
Create a project and add Vitest as a development dependency:
mkdir vitest-demo
cd vitest-demo
npm init -y
npm install -D vitestThen add two scripts to package.json. One runs tests in watch mode and the other runs them once:
{
"name": "vitest-demo",
"version": "1.0.0",
"type": "module",
"scripts": {
"test": "vitest",
"test:run": "vitest run"
},
"devDependencies": {
"vitest": "^3.0.0"
}
}Vitest transforms test files itself, so "type": "module" is not strictly required. It does keep Node's behaviour consistent with your tests when you run your own code outside Vitest.
Writing a First Test
Vitest looks for files whose names end in .test.js or .spec.js (plus the .ts, .jsx, .tsx, .mjs, and .cjs variants). Create a function and a test file next to it:
// sum.js
export function sum(a, b) {
return a + b
}// sum.test.js
import { test, expect } from 'vitest'
import { sum } from './sum.js'
test('adds 2 and 3 to make 5', () => {
expect(sum(2, 3)).toBe(5)
})test registers a named test case. expect(...).toBe(...) asserts that a value is strictly equal to the expected one. Later chapters cover structure and matchers in detail.
Watch Mode and Single-Run Mode
Run npm test. Vitest runs the suite, prints the results, and keeps watching. Edit sum.js and the related test reruns. Press h for the list of keyboard shortcuts and q to quit.
Run npm run test:run (or npx vitest run) to execute every test once and exit. The exit code is non-zero if any test fails, which is what CI pipelines check. When the CI environment variable is set or the terminal is not interactive, plain vitest also runs once instead of watching.
Common Errors
| Symptom | Cause | Fix |
|---|---|---|
| `No test files found` | File name lacks `.test` or `.spec` | Rename to `sum.test.js` |
| `ReferenceError: test is not defined` | Globals are off by default | Import `test` and `expect` from `'vitest'` |
| CI job hangs | Watch mode started in a pipeline | Use `vitest run` in CI |
| Unsupported engine warning or startup crash | Node.js version too old | Upgrade Node.js |
Exercise
You add "test": "vitest" to package.json and call npm test in a GitHub Actions job. Will the job hang waiting for file changes? What script would make the intent explicit?
Show answer
No. GitHub Actions sets the CI environment variable, so Vitest runs once and exits. Using vitest run (for example, a test:run script) states the single-run intent directly and does not depend on the environment.
Exercise
Your test file is named sum.check.js and Vitest reports "No test files found". Why, and what is the smallest fix?
Show answer
Vitest's default include pattern only matches files containing .test. or .spec. before the extension. Rename the file to sum.test.js (or sum.spec.js).