Performance: Testing client components
MEDIUM IMPACT
Testing client components affects development speed and can indirectly impact user experience by ensuring performant, bug-free UI rendering.
import { render, screen, waitFor } from '@testing-library/react'; import userEvent from '@testing-library/user-event'; import MyComponent from './MyComponent'; test('renders and updates after user interaction', async () => { render(<MyComponent />); expect(screen.getByText('Loading...')).toBeInTheDocument(); await waitFor(() => expect(screen.getByText('Data loaded')).toBeInTheDocument()); await userEvent.click(screen.getByRole('button', { name: /refresh/i })); await waitFor(() => expect(screen.getByText('Updated data')).toBeInTheDocument()); });
import { render } from '@testing-library/react'; import MyComponent from './MyComponent'; test('renders and updates', () => { const { getByText } = render(<MyComponent />); expect(getByText('Loading...')).toBeInTheDocument(); // No wait for async updates or user events });
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Basic render without async wait | Minimal | 0 during test | 0 during test | [X] Bad - flaky and incomplete |
| Render with async wait and user events | Simulated DOM ops | 0 during test | 0 during test | [OK] Good - reliable and meaningful |