condition-based-waiting

Replace fixed timeouts with configurable polling intervals in flaky tests.

4|1|Updated Oct 29, 2025
One-click install
npx skills add https://github.com/samjhecht/wrangler --skill condition-based-waiting-samjhecht
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: condition-based-waiting
Source: https://github.com/samjhecht/wrangler/tree/main/skills/condition-based-waiting
Command: npx skills add https://github.com/samjhecht/wrangler --skill condition-based-waiting-samjhecht

SYSTEM DOCUMENTATION & REQUIREMENTS

💡 This Skill includes references (resource) components.

What problem does it solve?

Tests with arbitrary setTimeout or sleep calls are prone to race conditions and flakiness, passing inconsistently across different environments or under varying loads, leading to unreliable test suites.

Core Features & Use Cases

  • Dynamic Polling: Replaces fixed delays with dynamic polling for actual state changes, waiting for the precise condition you care about.
  • Generic waitFor Function: Provides a versatile polling utility to wait for any condition to become true within a specified timeout.
  • Domain-Specific Helpers: Includes helpers like waitForEvent, waitForEventCount, and waitForEventMatch for common asynchronous testing scenarios.
  • Use Case: When your tests exhibit race conditions, timing dependencies, or inconsistent pass/fail behavior due to guessing at asynchronous operation completion, making them flaky.

Quick Start

I have a flaky test that uses setTimeout(..., 500). Help me refactor it to use condition-based waiting for a 'DONE' event.

Frequently Asked Questions about condition-based-waiting

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I fix flaky tests caused by race conditions and timing issues?

Replace arbitrary timeouts with condition-based waiting that polls for actual state changes. Instead of fixed `setTimeout` or `sleep` calls, use dynamic polling to wait for the precise condition you need, eliminating inconsistent pass/fail behavior across environments and loads.

What's the difference between setTimeout delays and condition-based waiting in tests?

Condition-based waiting polls for real conditions rather than guessing at completion time. Fixed delays can miss asynchronous operations or wait longer than necessary, while polling detects the exact moment your condition is true, removing race conditions and flakiness.

How do I wait for events in async tests without arbitrary timeouts?

Use event-based waiting helpers like `waitForEvent`, `waitForEventCount`, or `waitForEventMatch` to poll for specific events within a timeout. These specialized variants replace sleep calls and detect when events occur, making tests reliable across varying system loads.

Can I use condition polling for testing timing-dependent code?

Yes. Condition polling with short intervals (default 10ms) and backoff strategies works for any test with timing dependencies or asynchronous behavior. It replaces guesswork about completion time with actual state verification, preventing flaky results.

What should I do if my test still fails even after adding wait conditions?

Check that your condition accurately reflects the state you need. When events are available, prefer native async patterns or event-based waiting over polling. Verify your timeout is sufficient and your polling condition will eventually become true under normal operation.