# Isolated retries turned my flaky suite green. A test lock fixed it.

- Published: 2026-11-04
- Updated: 2026-11-26
- Tags: Testing, Tooling
- URL: https://devtrillo.com/writing/playwright-flaky-suites/

I turned on Playwright's isolated retries and my suite went green five runs in a row. Every one of those runs also had at least three flaky tests. The setting didn't fix anything. It moved the retries somewhere the flake couldn't reach them.

`retryStrategy: 'isolated'` arrived in [Playwright 1.62](https://github.com/microsoft/playwright/releases/tag/v1.62.0). Two releases later there are better tools for the job: [test locks](https://playwright.dev/docs/test-parallel#test-locks) in 1.63 and `--shuffle` in 1.64. I built a small suite with the two classic flakes, shared state and hidden test order, and ran it on 1.64.1. Everything below is as of 1.64.1. Releases have been landing every four to five weeks, so 1.65 may be out by the time you read this.

_Already know this? Jump to [What I'd set up](https://devtrillo.com/writing/playwright-flaky-suites/#what-id-set-up)._

## A suite that fights over one setting

Six tests save a theme to one shared account setting, wait for a fake 400 ms round trip, then check that they read back their own value. Each one is correct on its own. Twelve unrelated tests run next to them, and every third one resets the setting, the way a "reset preferences" test would.

```ts title="tests/settings.spec.ts"
for (const theme of ["dark", "light", "sepia", "contrast", "solar", "mono"]) {
  test(`saves theme ${theme}`, async ({ page }) => {
    writeTheme(theme);
    await page.setContent(`<button>Save</button>`);
    await page.waitForTimeout(400); // "server round trip"
    expect(readTheme()).toBe(theme);
  });
}
```

The config is the usual CI shape: `fullyParallel`, four workers, two retries. I ran it five times with each retry strategy.

| run | `immediate`                  | `isolated`         |
| --- | ---------------------------- | ------------------ |
| 1   | 3 flaky, 15 passed           | 3 flaky, 15 passed |
| 2   | 3 flaky, 15 passed           | 3 flaky, 15 passed |
| 3   | 1 failed, 2 flaky, 15 passed | 3 flaky, 15 passed |
| 4   | 3 flaky, 15 passed           | 5 flaky, 13 passed |
| 5   | 2 failed, 1 flaky, 15 passed | 3 flaky, 15 passed |

With the default, `immediate`, the build went red twice. With `isolated`, it never did. The flaky count never went down either.

## Isolated retries move the retry, not the bug

`immediate` retries a failed test right away, in the same crowded pool. If the test failed because a neighbour was writing the same setting, the retry often lands next to another writer and fails again. `isolated` holds every retry until the rest of the run is done, then runs them one by one in a single worker. Alone, the test passes.

```ts title="playwright.config.ts"
export default defineConfig({
  fullyParallel: true,
  workers: 4,
  retries: 2,
  retryStrategy: "isolated", // 1.62+, default "immediate"
});
```

Flip between the three settings to see where each retry runs. This is a hand-drawn schedule shaped like run 3, not a trace of it:

**Where Playwright runs a retry, and what a test lock changes.** Each grid shows four workers running tests over time; a cell either writes the shared setting (✓ passed, ✘ failed, ↻ retry), holds an unrelated test, or is empty. Columns after 4 come after the end of the main run. Three `retryStrategy` settings:

*immediate* — **1 failed, 2 flaky: red.** Retries go straight back into the crowd. The dark retry lands next to sepia, they overwrite each other, and dark runs out of retries.

| Lane | 1 | 2 | 3 | 4 | 5 | 6 |
| --- | --- | --- | --- | --- | --- | --- |
| worker 1 | dark ✘ | unrelated test | ↻ dark ✘ | unrelated test | · | · |
| worker 2 | light ✘ | unrelated test | sepia ✘ | ↻ sepia ✓ | · | · |
| worker 3 | unrelated test | ↻ light ✓ | unrelated test | unrelated test | · | · |
| worker 4 | unrelated test | unrelated test | unrelated test | ↻ dark ✘ | · | · |

*isolated* — **0 failed, 2 flaky: green.** Retries wait until everything else is done, then run one at a time. They pass. The collision in the first column still happened.

| Lane | 1 | 2 | 3 | 4 | 5 | 6 |
| --- | --- | --- | --- | --- | --- | --- |
| worker 1 | dark ✘ | unrelated test | · | · | · | · |
| worker 2 | light ✘ | unrelated test | sepia ✓ | · | · | · |
| worker 3 | unrelated test | unrelated test | unrelated test | · | · | · |
| worker 4 | unrelated test | unrelated test | unrelated test | · | · | · |
| retries | · | · | · | · | ↻ dark ✓ | ↻ light ✓ |

*locked, no retries* — **0 failed, 0 flaky: green.** Every test that writes the setting holds the same lock, so no two of them share a column. Nothing to retry.

| Lane | 1 | 2 | 3 | 4 | 5 | 6 |
| --- | --- | --- | --- | --- | --- | --- |
| worker 1 | dark ✓ | unrelated test | unrelated test | · | · | · |
| worker 2 | unrelated test | light ✓ | unrelated test | · | · | · |
| worker 3 | unrelated test | unrelated test | sepia ✓ | · | · | · |
| worker 4 | unrelated test | unrelated test | · | · | · | · |

So "flaky under isolation" is a diagnosis, not a fix. A test that fails in the crowd and passes alone is fighting with something running next to it. The [type docs](https://playwright.dev/docs/api/class-testconfig#test-config-retry-strategy) say what it costs, too: retries run "at the expense of the total run time", serially, at the end.

**Quiz:** With retryStrategy: 'isolated', a test fails in the main run and fails again on both isolated retries. What does that point at?

- Contention with a parallel test
- A real bug in the test or the app
- Too few retries

**Answer:** A real bug in the test or the app — The isolated retries ran alone, with nothing to collide with. A test that fails by itself is broken by itself.

## A test lock fixes the collision

[Playwright 1.63](https://github.com/microsoft/playwright/releases/tag/v1.63.0) added locks. Tests that share a lock name never run at the same time, across files, workers and projects. It's one option on the test:

```ts title="tests/settings.spec.ts" ins={3}
test(`saves theme ${theme}`, { lock: "account-settings" }, async ({ page }) => {
  // same body as before
});
```

`test.describe` takes the same `lock` option, and 1.64 added `test.describe.configure({ lock })` to lock a whole file. With the lock and `--retries=0`, the suite went 18 passed, 0 flaky in 8 of 8 runs:

```text title="LOCK=1, --retries=0"
[17/18] tests/settings.spec.ts:7:7 › saves theme solar
[18/18] tests/settings.spec.ts:7:7 › saves theme mono
  18 passed
```

> **Note:** A lock only protects the tests that declare it. The four filler tests that
> reset the setting didn't hold the lock. They didn't collide in eight runs, but
> nothing stops them. Every test that writes the resource needs the lock.

## `--shuffle` finds the tests that need an order

The second flake hides better. "add to cart" writes a cart file, and "checkout" assumes it exists. In the order they're written, both pass every time, so nobody notices.

```ts title="tests/order.spec.ts"
test("add to cart", async () => {
  fs.writeFileSync(DB, '["sock"]');
});
test("checkout", async () => {
  const cart = fs.existsSync(DB) ? JSON.parse(fs.readFileSync(DB, "utf8")) : [];
  expect(cart).toContain("sock");
});
```

[1.64](https://github.com/microsoft/playwright/releases/tag/v1.64.0) added `--shuffle`, which runs test files, and tests in parallel mode, in a random order. On one worker with retries off, "checkout" failed in 4 of 6 shuffled runs. The header prints the seed:

```text title="npx playwright test order --shuffle --workers=1 --retries=0"
Running 2 tests using 1 worker, shuffle seed 245155859

  ✘  1 tests/order.spec.ts:7:5 › checkout
  ✓  2 tests/order.spec.ts:6:5 › add to cart
```

**Quiz:** A shuffled CI run fails. You rerun the job and it passes. What's the fastest way to see the failure again?

- Rerun with --shuffle until it fails
- Copy the seed from the failed run's header and pass it to --shuffle
- Run with more workers

**Answer:** Copy the seed from the failed run's header and pass it to --shuffle — A seed is an order. --shuffle 245155859 reproduced the failing order on both reruns I tried, so the bug stops being random.

`--shuffle 245155859` reproduced it 2 of 2 times. A failure that only shows up under one order is a test leaning on state another test left behind. Make "checkout" create its own cart.

## Quarantine what you can't fix today

Some flakes take longer than an afternoon. [`Reporter.preprocess()`](https://playwright.dev/docs/api/class-reporter#reporter-preprocess), new in 1.62, runs after the config resolves and before any test starts. It gets a [`TestRun`](https://playwright.dev/docs/api/class-testrun) with `exclude`, `fail`, `fixme`, `skip` and `skipSharding`, so a reporter can sideline known-flaky tests by title:

```ts title="quarantine-reporter.ts"
import type {
  Reporter,
  FullConfig,
  Suite,
  TestRun,
} from "@playwright/test/reporter";
import fs from "node:fs";

const quarantined: string[] = JSON.parse(
  fs.readFileSync(__dirname + "/quarantine.json", "utf8"),
);

export default class QuarantineReporter implements Reporter {
  async preprocess({
    suite,
    testRun,
  }: {
    config: FullConfig;
    suite: Suite;
    testRun: TestRun;
  }) {
    for (const t of suite.allTests())
      if (quarantined.includes(t.title))
        testRun.fixme(t, "quarantined: see flake tracker");
  }
}
```

It sees the full suite before sharding, and it can't change setup or teardown projects. With `["checkout"]` in `quarantine.json` and the failing seed, "checkout" was skipped and the build went green. That's the "quarantine it or fix it" rule from [the testing strategy post](/writing/continuous-delivery-3-testing-strategy/#a-flaky-suite-is-worse-than-none), written down where the runner can enforce it.

**Quiz:** The quarantine reporter is set in playwright.config.ts. A teammate runs npx playwright test --reporter=dot. What happens to the quarantined tests?

- They stay skipped
- They run and can fail the build
- Playwright errors on the conflicting reporters

**Answer:** They run and can fail the build — --reporter replaces the configured reporters, so preprocess() never runs. --add-reporter (1.63+) stacks a reporter on top instead.

Same seed, same config, three ways of picking the reporter:

| command                                  | result              |
| ---------------------------------------- | ------------------- |
| `npx playwright test`                    | 1 skipped, 1 passed |
| `npx playwright test --reporter=dot`     | 1 failed, 1 passed  |
| `npx playwright test --add-reporter=dot` | 1 skipped, 1 passed |

No warning on the second one. If a CI step passes `--reporter`, switch it to `--add-reporter`.

## Cancel hung work with a signal

1.62 also lets you pass an `AbortSignal` to most actions and web-first assertions. I aborted a `toBeVisible()` after 500 ms. It rejected after 506 ms with `expect(locator).toBeVisible() failed`, instead of waiting out the 5 s default:

```ts
const controller = new AbortController();
setTimeout(() => controller.abort(), 500);
await expect(page.getByText("paid")).toBeVisible({ signal: controller.signal });
```

Two details. The signal doesn't switch the timeout off, so pass `timeout: 0` if you want only the signal. And I aborted with `new Error('payment gateway cancelled')`, and that reason didn't show up in the first line of the error, so don't count on it.

`locator.waitForFunction()`, also new in 1.62, waits on a condition about one element. This one resolved once the bar filled:

```ts
await page.locator("progress").waitForFunction((el) => el.value >= 100);
```

## WebP artifacts: measure first

1.62 added WebP screenshots, and 1.64 lets you make every unnamed screenshot WebP from the config:

```ts title="playwright.config.ts"
export default defineConfig({
  expect: { toHaveScreenshot: { type: "webp" } },
});
```

The pitch is smaller CI artifacts. For goldens, that depends on the page. `toHaveScreenshot` goldens are lossless, and on a flat page Playwright's WebP was 8 times bigger than the PNG. I took these at 1280x720:

| subject               | PNG       | WebP lossless | WebP `quality: 50` |
| --------------------- | --------- | ------------- | ------------------ |
| flat page, one `<h1>` | 8,162 B   | 67,146 B      | n/a                |
| playwright.dev        | 318,715 B | 258,980 B     | 28,650 B           |

On a busy real page the lossless WebP was 19% smaller. Lossy WebP is about 11 times smaller, but only `page.screenshot({ quality })` takes it, so it's for debug screenshots, not goldens. For reference, `cwebp -lossless -z 9` turned the same two PNGs into 1,474 B and 129,134 B, so Playwright seems to use a fast, low-effort encoder. Try it on your own suite before you flip the config.

If CI creates missing goldens, 1.64 changed that too: `--update-snapshots=missing` now passes when it only writes missing snapshots. My run that wrote a new WebP golden reported `1 passed`.

## Upgrading can add flakes too

Pin 1.64.1, not 1.64.0. The patch fixes [#43242](https://github.com/microsoft/playwright/issues/43242), where Chromium 155+ aborted preflighted cross-origin redirects whenever any route was registered, plus a flaky Electron first window.

Two other 1.64 changes can turn a stable test red after the upgrade. Device descriptors now forward `screen`, and elements inside hidden iframes count as hidden. Run the suite a few times on the new version before you blame the tests.

1.62 also bundled `npx playwright mcp` and `npx playwright cli` for agent-driven debugging. I didn't use either for any of this.

## What I'd set up

If you followed [the SvelteKit series](/writing/sveltekit-cloudflare-1-setup/), you already have a `playwright.config.ts`. In this order:

1. Pin `@playwright/test` to 1.64.1.
2. Set `retryStrategy: 'isolated'` and read the flaky list as "fights with something in parallel".
3. Put a `lock` on every test that touches the shared thing, then drop retries for those tests and watch it stay green.
4. Run `--shuffle` on a schedule, and save the seed from any red run.
5. Quarantine the rest in `preprocess()`, and use `--add-reporter` everywhere CI adds a reporter.

> **Tip:** One check I haven't run on this suite: take the lock off a single writer and
> run it a few times. If nothing goes flaky, the lock isn't protecting anything
> you can measure.

Isolated retries tell you which tests are fighting. A lock stops the fight.
