Key idea
Tests are executable documentation.
Why we test
- Catch bugs early.
- Refactor with confidence.
- Ship without fear.
Tools
- Vitest
- React Testing Library
Install (dev dependencies)
npm i -D vitest jsdom @testing-library/react @testing-library/user-event @testing-library/jest-dom @vitest/coverage-v8
What each package is for
- vitest: test runner (like Jest). Runs your unit/integration tests.
- jsdom: browser-like DOM environment for tests so React components can render in Node.
- @testing-library/react: utilities to render React components and query the UI the way a user would.
- @testing-library/user-event: higher-level user interactions (click, type, tab) that behave more like real users than
fireEvent.
- @testing-library/jest-dom: extra DOM matchers (e.g.
toBeInTheDocument, toHaveTextContent). Works in Vitest via setup file.
- @vitest/coverage-v8: code coverage reporting using V8 (produces coverage you can view in terminal and HTML reports).
Basic setup (Vite + React)