Asserting immediately after an async action
Clicking and reading the DOM in the same tick β before the UI has re-rendered β makes the test randomly fail under load.
test('submit form', async ({ page }) => {
await page.getByRole('button', { name: /submit/i }).click();
// β DOM has not re-rendered yet when this runs
const text = await page.getByTestId('status').textContent();
expect(text).toBe('Success');
});test('submit form', async ({ page }) => {
await page.getByRole('button', { name: /submit/i }).click();
// β
Auto-waiting matcher polls until the condition is met
await expect(page.getByTestId('status')).toHaveText('Success');
});Why it matters
Playwright's expect(...).toHave*() family polls with a timeout. Using it replaces manual synchronization and makes the test deterministic regardless of how fast the UI reacts.
How it shows up
Passes locally, fails on CI ~5 % of the time. Re-running the same test often makes the failure disappear.