How do you mitigate test flakiness caused by asynchronous client-side state hydration in single-page React/Next.js apps?
Asked by The SDET Playbook
Asked Sep 28, 2026Viewed 1 time
How do you mitigate test flakiness caused by asynchronous client-side state hydration in single-page React/Next.js apps?
Asked by The SDET Playbook
Sign in to answer and to vote.
With server rendering, the HTML arrives before React has attached its event handlers. A test can see the button and click it before hydration finishes, and the click does nothing. Playwright's actionability checks can't catch this, because the button is already visible, stable and enabled.
Fixes, from most to least preferable:
click(), wait for what the click should do: await expect(page.getByRole('dialog')).toBeVisible(). If hydration swallowed the click, the assertion fails clearly instead of a later step failing mysteriously.data-hydrated="true" on <body>, set in an effect), and the test waits for it with await expect(page.locator('body')).toHaveAttribute('data-hydrated', 'true'). This is a hook your team adds; neither React nor Next.js provides it.page.waitForResponse, set up before the action that triggers it) or, better, for the element that shows the loaded data.Avoid waitForTimeout and networkidle as fixes: both hide the race without removing it.
Sources: React: hydrateRoot, Playwright auto-waiting, Playwright best practices