Assertion Timeouts and Stability

๐Ÿ“‹ Table of Contents โ–พ
  1. Configuring Assertion Timeouts
  2. Common Mistakes

Assertion timeouts and patterns have a direct impact on both the speed and reliability of your Playwright suite. Tuning them carefully helps you avoid flakiness without making tests unnecessarily slow.

Configuring Assertion Timeouts

You can set default timeouts globally in playwright.config and override them per assertion when necessary. Most tests should work fine with the shared defaults.

// playwright.config.ts (snippet)
import { defineConfig } from '@playwright/test';

export default defineConfig({
  timeout: 30_000,
  expect: {
    timeout: 5_000,
  },
});
// Overriding timeout for a specific assertion
await expect(page.getByRole('status')).toHaveText('Long running task finished', {
  timeout: 15_000,
});
Note: Increasing a timeout for a specific long-running operation is better than raising the global timeout for all assertions.
Tip: Start with modest timeouts (for example, 3โ€“5 seconds) and adjust only where real performance data shows it is needed.
Warning: Very high timeouts can make hung tests slow to fail, delaying feedback and hiding performance regressions.

Use assertion patterns that reflect business expectations, such as checking for eventual counts or state changes rather than arbitrary waits.

Common Mistakes

Mistake 1 โ€” Raising global timeouts to hide slow tests

This reduces signal.

โŒ Wrong: Setting expect.timeout to 60 seconds for all tests.

โœ… Correct: Investigate slow tests and adjust only where necessary.

Mistake 2 โ€” Not distinguishing between transient failures and real problems

This can waste investigation time.

โŒ Wrong: Treating every rare timeout as a one-off and just rerunning CI.

โœ… Correct: Use retries and traces to spot patterns and fix root causes.

🧠 Test Yourself

How should you manage assertion timeouts in Playwright tests?