Skip to content
Latest stable: v0.8.0.

Reproduce a Flaky Failure

Use this loop when a test fails only sometimes:

  1. Log the chaos seed only on failure.
  2. Put that seed into the config.
  3. Re-run the same user actions.
  4. Assert the log shape so the replay proves the same chaos path.
import { test, expect } from '@playwright/test';
import {
formatSeedReproduction,
getChaosLog,
getChaosSeed,
injectChaos,
} from '@chaos-maker/playwright';
test.afterEach(async ({ page }, testInfo) => {
if (testInfo.status !== testInfo.expectedStatus) {
console.error(formatSeedReproduction(await getChaosSeed(page)));
}
});
test('replays checkout failure', async ({ page }) => {
await injectChaos(page, {
seed: 1234,
network: {
failures: [{ urlPattern: '/api/payments', statusCode: 503, probability: 0.3 }],
},
});
await page.goto('/checkout');
await page.click('#pay-now');
await expect(page.locator('[data-testid="payment-error"]')).toBeVisible();
const log = await getChaosLog(page);
expect(log.some((event) => event.type === 'network:failure' && event.applied)).toBe(true);
});