- Add comprehensive database migrations (001-024) for schema evolution - Enhance API schema with expanded type definitions and resolvers - Add new middleware: audit logging, rate limiting, MFA enforcement, security, tenant auth - Implement new services: AI optimization, billing, blockchain, compliance, marketplace - Add adapter layer for cloud integrations (Cloudflare, Kubernetes, Proxmox, storage) - Update Crossplane provider with enhanced VM management capabilities - Add comprehensive test suite for API endpoints and services - Update frontend components with improved GraphQL subscriptions and real-time updates - Enhance security configurations and headers (CSP, CORS, etc.) - Update documentation and configuration files - Add new CI/CD workflows and validation scripts - Implement design system improvements and UI enhancements
4.9 KiB
4.9 KiB
Testing Documentation
Overview
The Infrastructure Dashboard has comprehensive test coverage including unit tests, integration tests, and end-to-end (E2E) tests.
Test Structure
src/
├── components/
│ └── infrastructure/
│ └── __tests__/ # Component tests
├── lib/
│ ├── hooks/
│ │ └── __tests__/ # Hook tests
│ ├── services/
│ │ └── __tests__/ # Service tests
│ └── validation/
│ └── __tests__/ # Validation tests
└── test-utils.tsx # Test utilities
e2e/ # E2E tests
├── infrastructure-dashboard.spec.ts
└── ...
Running Tests
Unit and Integration Tests
# Run all tests
npm run test
# Run with UI
npm run test:ui
# Run with coverage
npm run test:coverage
# Watch mode
npm run test -- --watch
E2E Tests
# Run E2E tests
npm run test:e2e
# Run with UI
npm run test:e2e:ui
# Run specific browser
npx playwright test --project=chromium
All Tests
# Run all tests (unit + E2E)
npm run test:all
Test Coverage
Current Coverage Targets
- Lines: 90%
- Functions: 90%
- Branches: 85%
- Statements: 90%
Viewing Coverage
After running npm run test:coverage, open coverage/index.html in your browser to view detailed coverage reports.
Test Types
Unit Tests
Test individual components, hooks, and services in isolation.
Example:
import { describe, it, expect } from 'vitest'
import { render, screen } from '@testing-library/react'
import { EmptyState } from '../EmptyState'
describe('EmptyState', () => {
it('should render with title', () => {
render(<EmptyState title="Test" description="Description" />)
expect(screen.getByText('Test')).toBeInTheDocument()
})
})
Integration Tests
Test component interactions and data flow.
Example:
import { renderWithProviders } from '@/lib/test-utils'
import { ComplianceMapping } from '../ComplianceMapping'
it('should render and display data', async () => {
renderWithProviders(<ComplianceMapping />)
await waitFor(() => {
expect(screen.getByText('Italy')).toBeInTheDocument()
})
})
E2E Tests
Test complete user workflows in a real browser.
Example:
import { test, expect } from '@playwright/test'
test('should navigate to topology page', async ({ page }) => {
await page.goto('/infrastructure/docs')
await page.getByRole('link', { name: /network topology/i }).click()
await expect(page).toHaveURL(/.*\/topology/)
})
Test Utilities
renderWithProviders
Custom render function that includes all necessary providers:
import { renderWithProviders } from '@/lib/test-utils'
renderWithProviders(<MyComponent />)
Mocking
Mock API Calls
global.fetch = vi.fn().mockResolvedValue({
ok: true,
json: async () => mockData,
})
Mock Next.js Router
Already set up in vitest.setup.ts:
vi.mock('next/navigation', () => ({
useRouter: () => ({
push: vi.fn(),
replace: vi.fn(),
}),
}))
Writing Tests
Component Tests
- Test rendering
- Test user interactions
- Test state changes
- Test edge cases
Hook Tests
- Test return values
- Test side effects
- Test error handling
- Test loading states
Service Tests
- Test core functionality
- Test edge cases
- Test error handling
- Test data transformations
E2E Tests
- Test critical user flows
- Test navigation
- Test form submissions
- Test error scenarios
Best Practices
- Arrange-Act-Assert: Structure tests clearly
- Test Behavior: Test what users see/do, not implementation
- Isolation: Each test should be independent
- Descriptive Names: Test names should describe what they test
- Coverage: Aim for high coverage but focus on critical paths
- Maintainability: Keep tests simple and readable
Continuous Integration
Tests run automatically on:
- Pull requests
- Commits to main branch
- Scheduled runs
Debugging Tests
Unit Tests
# Run specific test file
npm run test -- src/components/infrastructure/__tests__/EmptyState.test.tsx
# Run with debug output
npm run test -- --reporter=verbose
E2E Tests
# Run in headed mode
npx playwright test --headed
# Debug mode
npx playwright test --debug
# Show browser
npx playwright test --ui
Troubleshooting
Common Issues
- Tests timing out: Increase timeout or check async operations
- Mock not working: Ensure mocks are set up before imports
- Coverage not updating: Clear cache and rerun
- E2E tests failing: Check if dev server is running