Testing Architecture
Overview
Comprehensive testing strategy covering unit tests, component tests, integration tests, and API mocking with 478+ tests across the codebase.
Location: src/__tests__/
directory
Quick Navigation
- Test Structure & Organization - Directory layout and test file statistics
- Testing Patterns - Unit, component, integration, and accessibility testing patterns
- Setup & Configuration - Vitest, test utilities, and global setup
- Test Coverage Goals - Coverage metrics and reporting
- Running Tests - CLI commands and CI/CD integration
- Overview - This file
Testing Philosophy
Pyramid Strategy
E2E Tests (Few)
/ \
/ \
/ \
Integration Tests (Medium)
/ \
/ \
/ \
Unit Tests (Many)
Distribution:
- Unit Tests: 70% - Fast, isolated, high granularity
- Integration Tests: 20% - Realistic scenarios, moderate speed
- E2E Tests: 10% - Full workflows, slower but comprehensive
Test Categories
1. Unit Tests (320+ tests)
Test individual functions and classes in isolation.
Coverage:
- Services (ProductService, auth, etc.)
- Utility functions
- Hooks
- Pure components
- Type guards and validators
Speed: Fast (< 1ms each)
src/__tests__/
βββ api/
β βββ ProductService.test.ts (40+ tests)
β βββ auth.test.ts (20+ tests)
β βββ client.test.ts (30+ tests)
βββ services/
β βββ useProducts.test.ts (25+ tests)
β βββ useForm.test.ts (30+ tests)
β βββ useAuth.test.ts (20+ tests)
βββ utils/
βββ validation.test.ts (15+ tests)
βββ helpers.test.ts (15+ tests)
2. Component Tests (120+ tests)
Test React components with their props and interactions.
Coverage:
- Shared components (Button, Header, Sidebar, etc.)
- Page components
- Props validation
- Event handling
- Accessibility
Speed: Medium (1-10ms each)
src/__tests__/components/
βββ Buttons.test.tsx (15+ tests)
βββ Header.test.tsx (20+ tests)
βββ Sidebar.test.tsx (15+ tests)
βββ ErrorBoundary.test.tsx (10+ tests)
βββ pages/
βββ LoginPage.test.tsx (20+ tests)
βββ ListStockPage.test.tsx (25+ tests)
βββ AdminDashboard.test.tsx (20+ tests)
3. Integration Tests (30+ tests)
Test multiple components/services working together.
Coverage:
- Component + Service interactions
- Redux integration
- API flow workflows
- Form submission flows
Speed: Slower (10-100ms each)
src/__tests__/integration/
βββ productWorkflow.integration.test.ts
βββ authFlow.integration.test.ts
βββ dashboardIntegration.test.ts
4. API Mocking
Mock Service Worker (MSW) for HTTP mocking.
// src/__tests__/mocks/server.ts
import { setupServer } from 'msw/node';
import { rest } from 'msw';
export const server = setupServer(
rest.get('/api/products', (req, res, ctx) => {
return res(ctx.status(200), ctx.json([...]));
}),
rest.post('/api/products', (req, res, ctx) => {
return res(ctx.status(201), ctx.json({ id: '123', ... }));
})
);Quick Start
Running Tests
# Run all tests
npm run test
# Run tests in watch mode
npm run test:watch
# Run specific file
npm run test ProductService.test.ts
# Run with coverage
npm run test:coverage
# Run integration tests only
npm run test -- integrationTest Command Examples
# Verbose output
npm run test -- --reporter=verbose
# Only failed tests
npm run test -- --reporter=verbose --bail
# Update snapshots
npm run test -- -u
# Debug in Node inspector
node --inspect-brk ./node_modules/vitest/vitest.mjs runTest Statistics
Coverage Summary
Statements : 82% ( 1,234 / 1,505 )
Branches : 78% ( 456 / 584 )
Functions : 81% ( 203 / 251 )
Lines : 83% ( 1,199 / 1,443 )
Tests by Category
| Category | Count | Status |
|---|---|---|
| Unit Tests | 320+ | β |
| Component Tests | 120+ | β |
| Integration Tests | 30+ | β |
| API Tests | 8+ | β |
| Total | 478+ | β |
Test Duration
Total Time: 8.2 seconds
- Unit Tests: 2.1s (fastest)
- Component Tests: 4.5s
- Integration Tests: 1.6s
File Structure
Complete Test Directory
src/__tests__/
βββ mocks/
β βββ server.ts (MSW setup)
β βββ handlers.ts (HTTP handlers)
β βββ data.ts (Mock data factories)
β
βββ api/
β βββ ProductService.test.ts
β βββ auth.test.ts
β βββ client.test.ts
β βββ integration/
β βββ apiWorkflow.test.ts
β
βββ services/
β βββ hooks/
β β βββ useProducts.test.ts
β β βββ useForm.test.ts
β β βββ useAuth.test.ts
β β βββ useDebounce.test.ts
β β βββ useLocalStorage.test.ts
β βββ apiClient.test.ts
β
βββ components/
β βββ __snapshots__/
β β βββ Button.test.tsx.snap
β β βββ Header.test.tsx.snap
β βββ Buttons.test.tsx
β βββ Header.test.tsx
β βββ Sidebar.test.tsx
β βββ ErrorBoundary.test.tsx
β βββ SkeletonLoader.test.tsx
β βββ pages/
β βββ LoginPage.test.tsx
β βββ ListStockPage.test.tsx
β βββ AddProductPage.test.tsx
β βββ AdminDashboard.test.tsx
β βββ UserDashboard.test.tsx
β
βββ utils/
β βββ validation.test.ts
β βββ helpers.test.ts
β βββ formatters.test.ts
β
βββ integration/
β βββ productWorkflow.test.ts
β βββ authFlow.test.ts
β βββ dashboardIntegration.test.ts
β
βββ setup.ts (Global test setup)
Testing Best Practices
β DO:
- Test behavior, not implementation
- Use descriptive test names
- Keep tests isolated and independent
- Mock external dependencies
- Test happy paths and error cases
- Organize tests logically
- Clean up after tests (vi.clearAllMocks)
β DON'T:
- Test implementation details
- Make real API calls in tests
- Create interdependent tests
- Skip error scenarios
- Use vague test descriptions
- Mix concerns in single test
- Ignore cleanup
Related Documentation
- Structure - Test organization and statistics
- Patterns - Unit, component, integration patterns
- Setup - Configuration and utilities
- Coverage - Coverage metrics and goals
- Running Tests - Commands and CI/CD
Last Updated: November 2025