Part of the
investigating-bugs skill. The skill links here when your agent needs the detail.Overview
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.When to Use
- Tests have arbitrary delays (
setTimeout,sleep,time.sleep()) - Tests are flaky (pass sometimes, fail under load)
- Tests timeout when run in parallel
- Waiting for async operations to complete
- Testing actual timing behavior (debounce, throttle intervals)
- Always document WHY if using arbitrary timeout
Core Pattern
Quick Patterns
Implementation
Generic polling function:Common Mistakes
Bad: Polling too fast:setTimeout(check, 1) - wastes CPU
Fix: Poll every 10ms
No timeout: Loop forever if condition never met
Fix: Always include timeout with clear error
Stale data: Cache state before loop
Fix: Call getter inside loop for fresh data
When Arbitrary Timeout IS Correct
- First wait for triggering condition
- Based on known timing (not guessing)
- Comment explaining WHY
Source:
library/skills/investigating-bugs/condition-based-waiting.md, rendered from summer-engine@4.3.0 on npm.
