Test stack
Vitest
Fast unit test runner built on Vite. Configured in
vitest.config.ts with a jsdom environment.jsdom
Simulates a browser DOM in Node.js so React components can render and interact without a real browser.
@testing-library/react
Renders React components and provides queries to find elements the way a user would.
@testing-library/jest-dom
Adds custom matchers like
toBeInTheDocument() and toHaveTextContent() to Vitest assertions.Running tests
vitest, which watches for changes by default. To run tests once without watching:
Where to put test files
Test files use the.test.ts or .test.tsx extension. You can place them in either location:
| Location | When to use |
|---|---|
| Next to the source file | Unit tests for a specific component or utility |
src/test/ | Shared test helpers, setup files, and integration tests |
**/*.test.ts and **/*.test.tsx automatically.
The existing
src/basic.test.ts is a good reference for how tests are structured in this project.Writing a component test
Here is a complete example testing a React component with@testing-library/react:
Key patterns
- Use
render()from@testing-library/reactto mount a component. - Use
screen.findBy*for elements that appear asynchronously (after a fetch resolves). - Use
screen.getBy*for elements that are present in the initial render. - Mock
fetchwithvi.stubGlobalto control API responses without hitting a real network. - Use
vi.fn()andbeforeEachto reset mocks between tests.
Vitest configuration
The test environment is configured invitest.config.ts. Because the project uses Astro, it wraps Vite config using getViteConfig from astro/config rather than Vitest’s own defineConfig:
vitest.config.ts
src/test/setup.ts imports @testing-library/jest-dom to make its matchers available in every test:
src/test/setup.ts