Frontend Testing
This guide covers testing strategies and implementation for the Rhesis frontend application.
Testing Framework The frontend uses Jest as the test runner and React Testing Library for component testing, providing comprehensive unit testing capabilities.
Quick Start
Running Tests
Test Coverage Target coverage thresholds: 70% for branches, functions, lines, and statements
Testing Configuration
Jest Setup
The testing framework is configured in jest.config.js:
Test Environment Setup
The test environment is configured in jest.setup.js:
Writing Tests
Component Testing
Use React Testing Library to test component behavior and user interactions:
Hook Testing
Test custom hooks using @testing-library/react-hooks:
Utility Function Testing
Test utility functions with Jest:
Test Utilities
Custom Test Utils
Use custom test utilities for consistent test setup:
Mock Implementations
Create mock implementations for API clients:
Testing Best Practices
Test Structure
✅ Good Practices
// Test user behavior, not implementation
expect(screen.getByRole('button')).toBeInTheDocument();
// Use semantic queries
screen.getByRole('button', { name: /submit/i });
// Test error states
expect(screen.getByText(/error/i)).toBeInTheDocument();❌ Avoid These
// Don't test implementation details
expect(wrapper.find('.my-button')).toHaveLength(1);
// Avoid fragile queries
screen.getByClassName('button-submit');
// Don't test internal state
expect(component.state.isLoading).toBe(true);Test Organization
Coverage Guidelines
Unit Tests
Component and utility function testing
Target: 80%+Integration Tests
Hook and API integration testing
Target: 70%+Critical Paths
Authentication and core workflows
Target: 90%+Edge Cases
Error handling and boundary conditions
Target: 60%+Integration with CI/CD
Pre-commit Hooks
Tests run automatically before commits through the validation script:
GitHub Actions
Automated testing in CI/CD pipeline:
Troubleshooting
Common Issues
Environment Variables Missing
# Add to jest.setup.js
process.env.NEXT_PUBLIC_API_BASE_URL = 'http://localhost:8080/api/v1';Browser APIs Not Available
# Mock in jest.setup.js
global.matchMedia = jest.fn().mockImplementation(query => ({ ... }));
global.ResizeObserver = jest.fn();Async Operations Not Awaited
// Use act() for async operations
await act(async () => {
await new Promise(resolve => setTimeout(resolve, 0));
});