plugins/ce/skills/condition-based-waiting/SKILL.md
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 skillsauth add rileyhilliard/claude-essentials condition-based-waitingInstall this skill globally with one command. Works with Claude Code, Cursor, and Windsurf.
3 of 9 scanners reported clean
Some scanners were skipped, did not run, or reported a non-clean status. Review each row below.
Use with: writing-tests skill for overall test guidance. This skill focuses on timing-based flakiness.
Related: If tests pass alone but fail concurrently, the problem may be shared state, not timing. See fixing-flaky-tests skill for diagnosis.
Flaky tests often guess at timing with arbitrary delays. This creates race conditions where tests pass on fast machines but fail under load or in CI.
Core principle: Wait for the actual condition you care about, not a guess about how long it takes.
Test has arbitrary delay (setTimeout/sleep)?
│
├─ Testing actual timing (debounce, throttle)?
│ └─ Yes → Keep timeout, document WHY
│
└─ No → Replace with condition-based waiting
Use when:
setTimeout, sleep, time.sleep())Don't use when:
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();
Prefer framework built-ins when available:
findBy queries, waitForexpect(locator).toBeVisible()asyncio.wait_for, tenacityCustom polling fallback when built-ins aren't enough:
async 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
}
}
Common use cases:
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 | setTimeout(check, 1) wastes CPU | 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
Requirements:
development
Selects and applies professional journalistic story structures (WSJ Formula, Inverted Pyramid, Hourglass, Tick-Tock, etc.) based on the content being written. Use when writing articles, blog posts, features, essays, long-form content, news stories, trend pieces, investigative reports, profiles, or any narrative prose longer than a few paragraphs. Also use when the user asks for help structuring a piece, choosing a story framework, organizing a draft, outlining an article, or wants to know which article format fits their content. Trigger on requests like "help me structure this," "what format should I use," "write a feature about," "draft a blog post on," or any mention of story structure, article architecture, or narrative frameworks. Complements the writer skill (which handles tone and anti-AI rhetoric) by providing the structural blueprint.
testing
Writing style and tone guide for human-sounding content. Use when writing documentation, READMEs, commit messages, PR descriptions, blog posts, LinkedIn posts, social media content, or any user-facing content.
data-ai
Create implementation plans with tasks grouped by subsystem. Related tasks share agent context; groups parallelize across subsystems.
development
Debugging framework that finds root causes before proposing fixes. Use when investigating bugs, errors, unexpected behavior, failed tests, or when previous fixes haven't worked.