Running Tests: CLI and Watch Mode
What you'll learn: How to execute your Vitest test suite using different commands for one-time runs, continuous watching, and UI exploration.
Now that you've written your first test, you need to know how to actually run it. Vitest offers several execution modes, each suited for different workflows.
Single Run Mode
The simplest approach is a one-time test execution:
npx vitest run
This runs all tests once and exits—perfect for CI/CD pipelines where you just need a pass/fail result.
Watch Mode: Your Development Companion
Think of watch mode as having a vigilant assistant who re-runs tests whenever you save a file:
npx vitest
That's it! The default command starts watch mode. Vitest monitors your source and test files, re-executing only affected tests when changes occur. Press a to run all tests, f for failed tests only, or q to quit.
UI Mode: Visual Test Explorer
For a browser-based dashboard:
npx vitest --ui
This opens an interactive interface showing test results, coverage, and execution times—helpful when you want a visual overview.
Setting Up npm Scripts
Rather than typing these commands repeatedly, add them to your package.json:
{
"scripts": {
"test": "vitest",
"test:run": "vitest run",
"test:ui": "vitest --ui"
}
}
Now you can use npm test for watch mode or npm run test:run for single execution. These scripts become your standard interface—consistent across projects and easy for team members to discover.
Key Takeaway: Vitest's run command executes tests once (ideal for CI), the default command enables watch mode (best for development), --ui provides a visual dashboard, and npm scripts make these commands memorable and consistent.