Installing Vitest and Testing Dependencies
What you'll learn: How to set up Vitest and the essential testing libraries needed to write React component tests in a modern development environment.
The Testing Stack
Now that you understand why Vitest is the modern choice for React testing, it's time to actually install it. Unlike Jest which requires complex configuration for modern JavaScript, Vitest works seamlessly with Vite projects. You'll need four key pieces:
Vitest is your test runner—it finds, executes, and reports on your tests. @testing-library/react provides utilities to render and interact with React components in tests. @testing-library/jest-dom adds helpful assertions like toBeInTheDocument(). Finally, jsdom simulates a browser environment in Node.js, since tests don't run in a real browser.
The Installation Command
npm install -D vitest @testing-library/react @testing-library/jest-dom jsdom
The -D flag saves these as devDependencies because they're only needed during development, not in production.
Why Each Dependency Matters
Think of it like assembling a home theater: Vitest is the receiver that coordinates everything, React Testing Library is the screen showing your components, jest-dom is the remote with convenient buttons for checking what's displayed, and jsdom is the room itself—the simulated environment where it all happens.
You also need @vitejs/plugin-react (likely already installed from your Vite setup) to transform JSX during tests, since test files run through Vite's transformation pipeline.
What's Next
After installation, you'll configure Vitest in your vite.config.js and set up the testing environment. The dependencies alone don't run anything—they're just the tools waiting to be configured.
Key Takeaway: Installing vitest, @testing-library/react, @testing-library/jest-dom, and jsdom gives you the complete modern React testing toolkit, each piece handling a specific part of the testing workflow.