# ๐Ÿงช Automated Testing Guide This guide explains how to run automated tests for critical paths, hydration, emails, and more. ## ๐Ÿ“‹ Test Types ### 1. Unit Tests (Jest) Tests individual components and functions in isolation. ```bash npm run test # Run all unit tests npm run test:watch # Watch mode npm run test:coverage # With coverage report ``` ### 2. E2E Tests (Playwright) Tests complete user flows in a real browser. ```bash npm run test:e2e # Run all E2E tests npm run test:e2e:ui # Run with UI mode (visual) npm run test:e2e:headed # Run with visible browser npm run test:e2e:debug # Debug mode ``` ### 3. Critical Path Tests Tests the most important user flows. ```bash npm run test:critical # Run critical path tests only ``` ### 4. Hydration Tests Ensures React hydration works without errors. ```bash npm run test:hydration # Run hydration tests only ``` ### 5. Email Tests Tests email API endpoints. ```bash npm run test:email # Run email tests only ``` ### 6. Performance Tests Checks page load times and performance. ```bash npm run test:performance # Run performance tests ``` ### 7. Accessibility Tests Basic accessibility checks. ```bash npm run test:accessibility # Run accessibility tests ``` ## ๐Ÿš€ Running All Tests ### Quick Test (Recommended) ```bash npm run test:all ``` This runs: - โœ… TypeScript check - โœ… ESLint - โœ… Build - โœ… Unit tests - โœ… Critical paths - โœ… Hydration tests - โœ… Email tests - โœ… Performance tests - โœ… Accessibility tests ### Individual Test Suites ```bash # Unit tests only npm run test # E2E tests only npm run test:e2e # Both npm run test && npm run test:e2e ``` ## ๐Ÿ“ What Gets Tested ### Critical Paths - โœ… Home page loads correctly - โœ… Projects page displays projects - โœ… Individual project pages work - โœ… Admin dashboard is accessible - โœ… API health endpoint - โœ… API projects endpoint ### Hydration - โœ… No hydration errors in console - โœ… No duplicate React key warnings - โœ… Client-side navigation works - โœ… Server and client HTML match - โœ… Interactive elements work after hydration ### Email - โœ… Email API accepts requests - โœ… Required field validation - โœ… Email format validation - โœ… Rate limiting (if implemented) - โœ… Email respond endpoint ### Performance - โœ… Page load times (< 5s) - โœ… No large layout shifts - โœ… Images are optimized - โœ… API response times (< 1s) ### Accessibility - โœ… Proper heading structure - โœ… Images have alt text - โœ… Links have descriptive text - โœ… Forms have labels ## ๐ŸŽฏ Pre-Push Testing Before pushing to main, run: ```bash # Full test suite npm run test:all # Or manually: npm run build npm run lint npx tsc --noEmit npm run test npm run test:critical npm run test:hydration ``` ## ๐Ÿ”ง Configuration ### Playwright Config Located in `playwright.config.ts` - **Base URL**: `http://localhost:3000` (or set `PLAYWRIGHT_TEST_BASE_URL`) - **Browsers**: Chromium, Firefox, WebKit, Mobile Chrome, Mobile Safari - **Retries**: 2 retries in CI, 0 locally - **Screenshots**: On failure - **Videos**: On failure ### Jest Config Located in `jest.config.ts` - **Environment**: jsdom - **Coverage**: v8 provider - **Setup**: `jest.setup.ts` ## ๐Ÿ› Debugging Tests ### Playwright Debug Mode ```bash npm run test:e2e:debug ``` This opens Playwright Inspector where you can: - Step through tests - Inspect elements - View console logs - See network requests ### UI Mode (Visual) ```bash npm run test:e2e:ui ``` Shows a visual interface to: - See all tests - Run specific tests - Watch tests execute - View results ### Headed Mode ```bash npm run test:e2e:headed ``` Runs tests with visible browser (useful for debugging). ## ๐Ÿ“Š Test Reports ### Playwright HTML Report After running E2E tests: ```bash npx playwright show-report ``` Shows: - Test results - Screenshots on failure - Videos on failure - Timeline of test execution ### Jest Coverage Report ```bash npm run test:coverage ``` Generates coverage report in `coverage/` directory. ## ๐Ÿšจ Common Issues ### Tests Fail Locally But Pass in CI - Check environment variables - Ensure database is set up - Check for port conflicts ### Hydration Errors - Check for server/client mismatches - Ensure no conditional rendering based on `window` - Check for date/time differences ### Email Tests Fail - Email service might not be configured - Check environment variables - Tests are designed to handle missing email service ### Performance Tests Fail - Network might be slow - Adjust thresholds in test file - Check for heavy resources loading ## ๐Ÿ“ Writing New Tests ### E2E Test Example ```typescript import { test, expect } from '@playwright/test'; test('My new feature works', async ({ page }) => { await page.goto('/my-page'); await expect(page.locator('h1')).toContainText('Expected Text'); }); ``` ### Unit Test Example ```typescript import { render, screen } from '@testing-library/react'; import MyComponent from './MyComponent'; test('renders correctly', () => { render(); expect(screen.getByText('Hello')).toBeInTheDocument(); }); ``` ## ๐ŸŽฏ CI/CD Integration ### GitHub Actions Example ```yaml - name: Run tests run: | npm install npm run test:all ``` ### Pre-Push Hook Add to `.git/hooks/pre-push`: ```bash #!/bin/bash npm run test:all ``` ## ๐Ÿ“š Resources - [Playwright Docs](https://playwright.dev) - [Jest Docs](https://jestjs.io) - [Testing Library](https://testing-library.com) --- **Remember**: Tests should be fast, reliable, and easy to understand! ๐Ÿš€