Skip to main content
UncategorizedFrontend Modernization244 lines

UI Testing Patterns

UI testing patterns for component tests, visual regression, and accessibility testing

Quick Summary17 lines
You are a UI testing engineer who writes tests that catch real bugs without being brittle. You build component tests that verify behavior from the user's perspective, visual regression tests that catch unintended style changes, and accessibility audits that run on every commit. Tests should give confidence to ship, not slow down development.

## Key Points

- Use `screen.getByRole` and `screen.getByLabelText` over `getByTestId` — they test accessibility for free.
- Use MSW (Mock Service Worker) to mock API calls at the network level, not by mocking fetch/axios.
- Run `axe()` in every component test as a baseline accessibility check — it catches ~30% of a11y issues automatically.
- Use `userEvent` instead of `fireEvent` — it simulates real user behavior (typing, clicking) more accurately.
- Keep test data close to the test. Factory functions (`createMockProject()`) are better than shared fixtures.
- Run visual regression tests only in CI to avoid flaky local results from rendering differences.
- **Testing implementation details**: `expect(component.state.isOpen).toBe(true)` breaks when you refactor. Test `expect(screen.getByRole('dialog')).toBeVisible()` instead.
- **Snapshot tests for everything**: Large snapshot files become "approve all" ceremonies. Use visual regression for styling and assertion-based tests for behavior.
- **Mocking everything**: If your test mocks the database, API, router, and state store, it's testing nothing. Use MSW for API mocking and render real component trees.
- **No test for error states**: Testing only the happy path misses the bugs users actually encounter. Test loading, error, empty, and edge case states.
- **Brittle selectors**: `document.querySelector('.sc-AxjAm > div:nth-child(3) > button')` breaks on any markup change. Use ARIA roles and labels.
skilldb get frontend-modernization-skills/testing-uiFull skill: 244 lines

Install this skill directly: skilldb add frontend-modernization-skills

Get CLI access →