Loading...
Loading...
Fixes flaky tests by replacing arbitrary timeouts with condition polling. Use when tests fail intermittently, have setTimeout delays, or involve async operations that need proper wait conditions.
npx skill4agent add rileyhilliard/claude-essentials condition-based-waitingwriting-testsfixing-flaky-testsTest has arbitrary delay (setTimeout/sleep)?
│
├─ Testing actual timing (debounce, throttle)?
│ └─ Yes → Keep timeout, document WHY
│
└─ No → Replace with condition-based waitingsetTimeoutsleeptime.sleep()fixing-flaky-tests// Bad: Guessing at timing
await new Promise((r) => setTimeout(r, 50));
const result = getResult();
expect(result).toBeDefined();
// Good: Waiting for condition (returns the result)
const result = await waitFor(() => getResult(), 'result to be available');
expect(result).toBeDefined();findBywaitForexpect(locator).toBeVisible()asyncio.wait_forasync function waitFor<T>(
condition: () => T | undefined | null | false,
description: string,
timeoutMs = 5000
): Promise<T> {
const startTime = Date.now();
while (true) {
const result = condition();
if (result) return result;
if (Date.now() - startTime > timeoutMs) {
throw new Error(`Timeout waiting for ${description} after ${timeoutMs}ms`);
}
await new Promise((r) => setTimeout(r, 50)); // Poll interval
}
}waitFor(() => events.find(e => e.type === 'DONE'), 'done event')waitFor(() => machine.state === 'ready', 'ready state')waitFor(() => items.length >= 5, '5+ items')| Stack | Reference |
|---|---|
| Python (pytest, asyncio, tenacity) | references/python.md |
| TypeScript (Jest, Testing Library, Playwright) | references/typescript.md |
| Mistake | Problem | Fix |
|---|---|---|
| Polling too fast | | Poll every 50ms |
| No timeout | Loop forever if condition never met | Always include timeout |
| Stale data | Caching state before loop | Call getter inside loop |
| No description | "Timeout" with no context | Include what you waited for |
// Tool ticks every 100ms - need 2 ticks to verify partial output
await waitForEvent(manager, "TOOL_STARTED"); // First: wait for condition
await new Promise((r) => setTimeout(r, 200)); // Then: wait for timed behavior
// 200ms = 2 ticks at 100ms intervals - documented and justified