What problem does it solve?
This Skill replaces unreliable arbitrary timeouts in tests with robust condition-based polling, eliminating flaky tests caused by race conditions and timing dependencies, ensuring consistent and reliable test results.
Core Features & Use Cases
- Reliable Async Waiting: Provides a generic
waitFor function to poll for a condition to become true, preventing tests from failing due to timing guesses.
- Domain-Specific Helpers: Includes examples for waiting for specific events, event counts, or custom predicates, adaptable to various async scenarios.
- Flaky Test Prevention: Ensures tests wait for actual state changes, not just a guessed duration, making your test suite more robust.
- Use Case: When a test intermittently fails due to an
await new Promise(r => setTimeout(r, 50)); line, use this skill to replace it with await waitFor(() => getResult() !== undefined);, making the test reliable across different environments and loads.
Quick Start
Generic polling function
async function waitFor(condition, description, timeoutMs = 5000) {
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});
}
await new Promise(r => setTimeout(r, 10)); # Poll every 10ms
}
}
Example: Wait for a specific event
await waitForEvent(threadManager, agentThreadId, 'TOOL_RESULT');