# Diagnose and fix a flaky test

Works out why a test passes and fails intermittently, how to reproduce it, and how to fix the cause rather than adding retries.

- Topic: Software Development
- Framework: Few-Shot
- Level: Standard
- Tags: Coding, Analyze, Code, Fill-in Template
- Source: https://www.thinkwithprompt.com/prompts/diagnose-and-fix-a-flaky-test

## Prompt

```text
Help me find out why a test is flaky and fix it properly.

**Test code:** [PASTE THE TEST]
**Code under test:** [PASTE THE RELEVANT PARTS]
**Failure output:** [PASTE ONE OR MORE FAILING RUNS]
**How often it fails:** [e.g. "1 in 20 runs", "only in CI", "only when run with the full suite"]
**Test environment:** [FRAMEWORK, CI RUNNER, PARALLELISM, DATABASE OR CONTAINERS USED]

Work through the common causes and say which fit my evidence:
- Timing: sleeps, timeouts, async work not awaited, animations, polling.
- Order dependence: shared state, leftover database rows, global mocks, caches between tests.
- Concurrency: parallel tests sharing ports, files, or records.
- Time and randomness: current date, time zones, daylight saving, unseeded random data, unordered collections.
- Environment: network calls, external services, resource limits in CI.

Return:
1. The most likely cause(s), ranked, with the evidence for each.
2. How to reproduce it reliably (e.g. run in a loop, fixed seed, specific test order, throttled CPU).
3. The fix, as code. Prefer fixing the root cause: deterministic waits, isolated data, injected clocks, seeded randomness.
4. If the real bug is in the production code rather than the test, say so clearly.
5. What not to do: explain why a blanket retry or longer sleep would only hide the problem.
```

## What to fill in

- PASTE THE TEST
- PASTE THE RELEVANT PARTS
- PASTE ONE OR MORE FAILING RUNS

Free to copy and use. From ThinkWithPrompt (https://www.thinkwithprompt.com/), a library of 2,663 curated AI prompts.
