Overview
The Pizza Chef Frontend uses Vitest for fast, modern testing with React Testing Library for component testing. This guide covers test configuration, writing tests, and running the test suite.Testing Stack
The project uses the following testing tools:- Vitest (^4.0.18) - Fast unit test framework with Vite integration
- @vitest/ui (^4.0.18) - Interactive UI for test exploration
- @testing-library/react (^16.3.2) - React component testing utilities
- @testing-library/jest-dom (^6.9.1) - Custom DOM matchers
- @testing-library/user-event (^14.6.1) - User interaction simulation
- jsdom (^28.1.0) - DOM implementation for Node.js
Test Configuration
Vitest Configuration
Vitest is configured invite.config.ts:
vite.config.ts
globals: true: Enables global test APIs (describe, it, expect) without importsenvironment: 'jsdom': Uses jsdom to simulate a browser environmentsetupFiles: Runs setup file before each test file
Setup File
Thesrc/setupTests.ts file configures testing utilities:
src/setupTests.ts
toBeInTheDocument()toHaveTextContent()toBeVisible()toBeDisabled()
Running Tests
Run All Tests
Execute the entire test suite:Interactive UI Mode
Launch the Vitest UI for visual test exploration:- Test file explorer
- Individual test execution
- Code coverage visualization
- Test performance metrics
- Filtering and search capabilities
Run Tests Once (CI Mode)
For continuous integration, run tests without watch mode:Run Specific Test Files
Run tests matching a pattern:Writing Component Tests
Test File Structure
Component tests are located in__tests__ directories next to the components:
Component Test Example
Here’s a complete example fromsrc/components/pizza/__tests__/PizzaCard.test.tsx:
PizzaCard.test.tsx
- Mock Data: Define test fixtures that match TypeScript interfaces
- renderWithProviders: Helper function to wrap components with Redux and Router
- Screen Queries: Use
screen.getByText(),screen.getByRole()for querying - User Interactions: Use
fireEventor@testing-library/user-event - Assertions: Use jest-dom matchers for readable expectations
Writing Redux Tests
Store Test Structure
Redux slice tests are located insrc/store/__tests__/:
Redux Slice Test Example
Here’s the complete test suite fromsrc/store/__tests__/orderSlice.test.ts:
orderSlice.test.ts
- Mock Dependencies: Use
vi.stubGlobal()to mock browser APIs like localStorage - beforeEach: Reset mocks and state before each test
- Test Actions: Dispatch actions and verify state changes
- Business Logic: Test discount calculations, quantity updates, etc.
- State Transitions: Verify state changes through multiple actions
Testing Best Practices
Query Priority
Use React Testing Library queries in this order:getByRole: Most accessible (buttons, links, inputs)getByLabelText: Form fields with labelsgetByPlaceholderText: Inputs with placeholdersgetByText: Non-interactive text contentgetByTestId: Last resort for dynamic content
Avoid Implementation Details
Test user behavior, not implementation:Use User Event for Interactions
For complex interactions, prefer@testing-library/user-event:
Test Accessibility
Ensure components are accessible:Coverage Reporting
Generate coverage reports with Vitest:@vitest/coverage-v8 or @vitest/coverage-istanbul.
Debugging Tests
Debug in VS Code
Add breakpoints in your test files and use VS Code’s debugger with this configuration:.vscode/launch.json
Use screen.debug()
Inspect the rendered output:Common Patterns
Testing Redux Dispatch
Testing Async Operations
Testing Router Navigation
Next Steps
- Review the Build Process for production deployment
- Check out Vitest documentation for advanced features
- Explore React Testing Library best practices