Akusara Digital
Developer Tools2026-08-20•5 min read

Eliminating Flaky Tests in CI: Deterministic Wait Patterns and DOM State Guards

Why waitForTimeout is an anti-pattern. How web-first assertions, auto-waiting locators, and isolated test workers cut CI false negatives to zero.

A

Akusara Studio Labs

QA & Test Engineering

#Playwright#CI/CD#E2E Testing#Quality Assurance
Eliminating Flaky Tests in CI: Deterministic Wait Patterns and DOM State Guards
Key Takeaway

Why waitForTimeout is an anti-pattern. How web-first assertions, auto-waiting locators, and isolated test workers cut CI false negatives to zero.

Why Flaky Tests Erode Trust

When continuous integration pipelines fail intermittently on unrelated pull requests, developers stop trusting test results. They hit "Re-run failed jobs" until it turns green by chance, and critical regressions slip into production.

The root cause of flakiness is almost always non-deterministic synchronization between asynchronous JavaScript rendering and test assertions.

1. Ban page.waitForTimeout()

Hardcoded sleeps like page.waitForTimeout(2000) fail because CI runner CPU allocation varies. On a busy shared CI runner, 2000ms is not enough; on a local laptop, it wastes time.

Instead, rely on Playwright's web-first assertions, which automatically retry until the condition is met or the timeout expires:

typescript
// Anti-pattern: arbitrary sleep
await page.click('#submit-btn');
await page.waitForTimeout(3000);
expect(await page.isVisible('.success-badge')).toBeTruthy();

// Deterministic pattern: auto-retrying assertion
await page.getByRole('button', { name: 'Save Changes' }).click();
await expect(page.getByText('Settings updated successfully')).toBeVisible();

2. Waiting for Network Idleness Is Risky

Using page.waitForLoadState('networkidle') frequently times out on applications that maintain active WebSocket heartbeats, telemetry beacons, or polling loops.

Instead, wait for the specific response payload your test depends on:

typescript
// Wait deterministically for the specific API endpoint
const [response] = await Promise.all([
  page.waitForResponse((res) => res.url().includes('/api/projects') && res.status() === 200),
  page.getByRole('button', { name: 'Create Project' }).click(),
]);

const data = await response.json();
expect(data.id).toBeDefined();

3. Database Isolation Per Test Run

Flakiness also occurs when tests read and write to shared database rows simultaneously. In our CI pipelines, each parallel test worker connects to an isolated tenant schema or runs inside a dedicated lightweight test database spun up via Docker compose.

When tests run against independent data states, CI failure rates drop to genuine regression alerts.

Tags:#Playwright#CI/CD#E2E Testing#Quality Assurance
A

Published by Akusara Studio Labs

QA & Test Engineering

Akusara Digital is an engineering and product studio building mission-critical software, custom SaaS platforms, and developer tooling.

Read Next from Akusara

All Articles