Example result: Fix a flaky test that passes and fails at random
AI-generated example · Debug · Flaky Test
Updated

Fix a flaky test that passes and fails at random

A flaky test passes and fails with no code change. This prompt looks for the usual causes, such as async timing, shared state and dates, and rewrites the test so it is reliable without adding sleeps or retries.

text-code-flaky-test-fix — claude — 85 words

Act as a test reliability expert. This test fails about [HOW OFTEN, e.g. 1 in 5 runs] with no code change.

Test code:
[PASTE THE TEST]

Code under test:
[PASTE]

Failure output:
[PASTE A FAILING RUN]

Find the source of the flakiness: timing and async waits, shared state between tests, test order, random data, dates and timezones, network calls or leftover files. Explain the cause in plain words, then rewrite the test so it is deterministic. Do not fix it by adding sleeps or retries.

If you can, run the test 20 times in a loop and paste two different failures. Patterns between them point to the cause.

Get a better result

What you will get

A plain-words explanation of why the test fails sometimes, covering timing, shared state, order, random data, dates or the network, and a rewrite that makes it deterministic without sleeps or retries.

Common mistake

The fix is a longer wait that hides the problem.

Fix: Keep “no sleeps or retries” and ask for the test to wait for a specific condition or event instead.

Also watch for
  • The AI guesses without seeing a failure. Paste two different failing outputs and the code under test. Differences between failures point to the real cause.
  • The test passes alone but fails in the full suite. Mention it, and ask the AI to check for shared state, leftover data and test order.
Try a variation: freeze time and randomness

Ask for “a version that fixes the clock and the random seed”. Tests that read today’s date or random values are a common cause of failures.

Still not right? Reply with this

List the three most likely causes in order, with the line that points to each, and give me a quick way to confirm each one.

Guides that use this prompt

More chat & writing prompts

See all 465 →