- Install Jest for unit testing with React Testing Library - Install Playwright for end-to-end testing - Configure Jest with proper TypeScript support and module mapping - Create test setup files and utilities for both unit and e2e tests Components: * Jest configuration with coverage thresholds * Playwright configuration with browser automation * Unit tests for LoginForm, AuthContext, and useSocketIO hook * E2E tests for authentication, dashboard, and agents workflows * GitHub Actions workflow for automated testing * Mock data and API utilities for consistent testing * Test documentation with best practices Testing features: - Unit tests with 70% coverage threshold - E2E tests with API mocking and user journey testing - CI/CD integration for automated test runs - Cross-browser testing support with Playwright - Authentication system testing end-to-end 🚀 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com>
4.4 KiB
4.4 KiB
Frontend Testing Infrastructure
This document describes the testing setup for the Hive frontend application.
Overview
The testing infrastructure includes:
- Unit Tests: Jest + React Testing Library for component and hook testing
- End-to-End Tests: Playwright for full user journey testing
- Type Checking: TypeScript compiler for type safety
- Linting: ESLint for code quality
Test Commands
Unit Tests
# Run all unit tests
npm run test
# Run tests in watch mode
npm run test:watch
# Run tests with coverage report
npm run test:coverage
End-to-End Tests
# Run e2e tests headlessly
npm run test:e2e
# Run e2e tests with UI
npm run test:e2e:ui
# Debug e2e tests
npm run test:e2e:debug
All Tests
# Run both unit and e2e tests
npm run test:all
Test Structure
Unit Tests
- Location:
src/**/__tests__/orsrc/**/*.test.tsx - Framework: Jest + React Testing Library
- Configuration:
jest.config.js - Setup:
src/test/setup.ts - Utilities:
src/test/utils.tsx
Example test file structure:
src/
├── components/
│ └── auth/
│ ├── LoginForm.tsx
│ └── __tests__/
│ └── LoginForm.test.tsx
├── hooks/
│ └── __tests__/
│ └── useSocketIO.test.ts
└── test/
├── setup.ts
└── utils.tsx
End-to-End Tests
- Location:
e2e/ - Framework: Playwright
- Configuration:
playwright.config.ts - Global Setup:
e2e/global-setup.ts
Example e2e test structure:
e2e/
├── auth.spec.ts
├── dashboard.spec.ts
├── agents.spec.ts
└── global-setup.ts
Testing Best Practices
Unit Testing
- Test user interactions, not implementation details
- Use semantic queries (getByRole, getByLabelText)
- Mock external dependencies (APIs, WebSocket)
- Test error states and loading states
- Maintain high coverage (70%+ threshold)
E2E Testing
- Test critical user journeys
- Mock API responses for consistent testing
- Use Page Object Model for complex interactions
- Test responsive design across viewports
- Include accessibility checks
Mocking Strategies
API Mocking (Unit Tests)
jest.mock('../../api/auth');
const mockAuthApi = authApi as jest.Mocked<typeof authApi>;
mockAuthApi.login.mockResolvedValue(mockApiResponses.auth.login);
API Mocking (E2E Tests)
await page.route('**/api/auth/login', async route => {
await route.fulfill({
status: 200,
contentType: 'application/json',
body: JSON.stringify(mockResponse),
});
});
Coverage Requirements
- Statements: 70%
- Branches: 70%
- Functions: 70%
- Lines: 70%
CI/CD Integration
Tests run automatically on:
- Push to main branches
- Pull requests
- Frontend code changes
GitHub Actions workflow: .github/workflows/frontend-tests.yml
Debugging Tests
Unit Tests
# Debug specific test
npm run test -- --testNamePattern="LoginForm"
# Debug with Node.js inspector
node --inspect-brk node_modules/.bin/jest --runInBand
E2E Tests
# Run in headed mode
npm run test:e2e:debug
# Run specific test
npx playwright test auth.spec.ts
# Open test results
npx playwright show-report
Test Data
Mock data is centralized in src/test/utils.tsx:
mockUser: Test user datamockAgent: Test agent datamockTask: Test task datamockApiResponses: API response templates
Environment Setup
Prerequisites
- Node.js 18+
- Chrome/Chromium (for Playwright)
Installation
cd frontend
npm install
npx playwright install chromium
Configuration Files
jest.config.js: Jest configurationplaywright.config.ts: Playwright configurationsrc/test/setup.ts: Jest test setupe2e/global-setup.ts: Playwright global setup
Common Issues
Jest Issues
- Module resolution: Check
moduleNameMappingin jest.config.js - Async tests: Use
act()for async operations - React warnings: Mock console methods in setup
Playwright Issues
- Timeouts: Increase timeout in playwright.config.ts
- Flaky tests: Add proper wait conditions
- Browser not found: Run
npx playwright install
Contributing
When adding new features:
- Write unit tests for components/hooks
- Add e2e tests for new user flows
- Update mock data if needed
- Ensure coverage thresholds are met
- Test both success and error scenarios