flaky-test-analyzer
Diagnoses why tests pass inconsistently and suggests fixes for timing, ordering, and state isolation issues.
Codex 또는 Claude로 설치 이 Prompt를 복사해 Codex, Claude 또는 다른 어시스턴트에 붙여 넣으면 Skill 페이지를 검토하고 설치를 진행할 수 있습니다.
메뉴
Diagnoses why tests pass inconsistently and suggests fixes for timing, ordering, and state isolation issues.
Codex 또는 Claude로 설치 이 Prompt를 복사해 Codex, Claude 또는 다른 어시스턴트에 붙여 넣으면 Skill 페이지를 검토하고 설치를 진행할 수 있습니다.
SOC 직업 분류 기준
Runs a systematic checklist review on any code diff or file, covering correctness, security, performance, and readability.
Writes a high-quality CLAUDE.md, .cursorrules, or .windsurfrules file that gives a coding agent the right project context, conventions, and constraints to work effectively.
Designs an eval suite for an LLM agent or pipeline including success metrics, trajectory scoring, LLM-as-judge setup, and regression test cases.
Designs a hybrid retrieval pipeline combining dense vector search and BM25 sparse search with reciprocal rank fusion, and explains when to use each configuration.
Converts a workflow description into a LangGraph node/edge graph with typed state, conditional routing, and human-in-the-loop checkpoints.
Audits an AI application for unnecessary token spend and recommends prompt caching, model routing, and token reduction techniques to cut costs.
| name | Flaky Test Analyzer |
| description | Diagnoses why tests pass inconsistently and suggests fixes for timing, ordering, and state isolation issues. |
| category | coding |
| tags | ["testing","flaky-tests","ci","reliability"] |
| author | simplyutils |
This skill directs the agent to diagnose flaky tests — tests that sometimes pass and sometimes fail without any code changes. It examines the test code, the code under test, and the failure patterns to identify the root cause category (timing, shared state, ordering, network, randomness, etc.) and then suggests targeted fixes that make the test deterministic.
Use this when a test is unreliable in CI, when a test passes locally but fails on the CI server, or when a test fails intermittently with no obvious pattern.
Copy this file to .agents/skills/flaky-test-analyzer/SKILL.md in your project root.
Then ask:
tests/checkout.test.ts — it fails about 1 in 5 runs in CI."Provide:
Add the instructions below to your .cursorrules or paste them into the Cursor AI pane. Provide the test code and failure output.
Paste the test file, the failure message, and any relevant context. Ask Codex to follow the instructions below.
When asked to diagnose a flaky test, follow this process:
Before analyzing, ensure you have:
beforeEach, afterEach, beforeAll, and afterAll hooksIf any of these are missing, ask for them.
Check for each of these flakiness patterns in order:
Timing and async issues
setTimeout or setInterval with hardcoded delays that may not be long enoughawait on async operationsjest.useFakeTimers) mixed with real timersShared state and isolation
Test ordering dependencies
External dependencies
Race conditions in the code under test
Promise.all with side effects that interfereTest framework issues
After identifying which category applies, pinpoint the exact line(s) causing the flakiness. Explain:
Provide a concrete, specific fix. Common fixes include:
setTimeout(() => ..., 100) with waitFor(() => ...) or vi.runAllTimers()afterEach(() => { jest.clearAllMocks(); db.cleanup(); })jest.useFakeTimers() consistently and advance time explicitlyawait to async operations## Flaky Test Diagnosis
### Test
`[test name]` in `[file path]`
### Failure Pattern
[How often it fails, under what conditions]
### Root Cause Category
[Timing / Shared state / Test ordering / External dependency / Race condition / Other]
### Root Cause
[2–3 sentences explaining exactly why the test is flaky — what assumption it makes and why that assumption sometimes fails]
### The Problematic Code
[Quote the specific lines that cause the flakiness]
### Fix
[The specific change(s) to make the test deterministic, with code]
### Why This Fix Works
[1–2 sentences explaining why the fix eliminates the non-determinism]
### Prevention
[1 sentence on how to avoid this class of flakiness in future tests]
Input to Agent:
"Use the Flaky Test Analyzer skill. This test fails about 1 in 4 runs in CI:
it('sends a welcome email after registration', async () => { await registerUser({ email: 'test@example.com', password: 'abc123' }); setTimeout(() => { expect(emailService.send).toHaveBeenCalledWith({ to: 'test@example.com', subject: 'Welcome!' }); }, 200); }); ```"
Output from Agent:
Flaky Test Diagnosis
Test
sends a welcome email after registration—user.test.tsFailure Pattern
Fails ~25% of runs, more often in CI than locally.
Root Cause Category
Timing / async issue
Root Cause
The
expectinsidesetTimeoutis not awaited, so Jest considers the test done as soon asregisterUserresolves — the assertion runs 200ms later, after Jest has already moved on. When the event loop is busy (common in CI), the 200ms delay may not be enough, and even when it fires, Jest may no longer be tracking it.The Problematic Code
setTimeout(() => { expect(emailService.send).toHaveBeenCalledWith(...); }, 200);Fix
it('sends a welcome email after registration', async () => { await registerUser({ email: 'test@example.com', password: 'abc123' }); await waitFor(() => { expect(emailService.send).toHaveBeenCalledWith({ to: 'test@example.com', subject: 'Welcome!' }); }); });If
waitForisn't available (non-React project), usejest.runAllTimers()withjest.useFakeTimers()or poll manually with a small helper.Why This Fix Works
waitForrepeatedly retries the assertion until it passes or a timeout is reached, making the test resilient to variable execution timing.Prevention
Never put
expect()calls inside a rawsetTimeout— always await the operation that triggers the side effect, or usewaitFor.