condition-based-waiting

Replace arbitrary timeouts with condition polling in tests.

4|Updated Dec 7, 2020
One-click install
npx skills add https://github.com/charly3pins/dotfiles --skill condition-based-waiting-charly3pins
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: condition-based-waiting
Source: https://github.com/charly3pins/dotfiles/tree/main/.config/opencode/skills/condition-based-waiting
Command: npx skills add https://github.com/charly3pins/dotfiles --skill condition-based-waiting-charly3pins

SYSTEM DOCUMENTATION & REQUIREMENTS

💡 This Skill includes scripts (resource) components.

What problem does it solve?

Arbitrary setTimeout or sleep calls in tests create race conditions, leading to flaky tests that pass inconsistently and fail under load or in CI, wasting debugging time.

Core Features & Use Cases

  • Condition Polling: Replaces fixed delays with dynamic waiting for actual state changes or events, ensuring reliability.
  • Generic Polling Function: Provides a reusable waitFor utility to poll for any condition with a configurable timeout.
  • Domain-Specific Helpers: Offers specialized functions like waitForEvent and waitForEventCount for common async scenarios.
  • Use Case: When a test intermittently fails due to timing issues, use this skill to replace its arbitrary setTimeout calls with condition-based waiting, ensuring reliability and faster execution.

Quick Start

I have a flaky test that uses setTimeout. Help me refactor it to use condition-based waiting.

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 timing issues and race conditions?

Replace arbitrary `setTimeout` calls with condition-based waiting that polls for actual state changes. This eliminates race conditions by waiting for events or conditions to occur rather than guessing how long they'll take, ensuring tests pass reliably in CI and parallel environments.

What's the difference between using setTimeout and condition polling in async tests?

Condition polling waits for a specific condition to become true before proceeding, adapting to actual execution speed. `setTimeout` waits a fixed duration regardless of whether the condition is met, creating race conditions and flaky failures under load or in CI.

How do I implement event-driven waiting in TypeScript tests?

Use a generic `waitFor` polling function with a condition predicate and configurable timeout. The function repeatedly checks your condition until it returns true or the timeout expires, with specialized helpers like `waitForEvent` for common async patterns.

Can I use condition-based waiting to handle asynchronous operations in parallel test execution?

Yes. Condition polling is designed for async operations and parallel CI environments. By waiting for actual state changes instead of fixed delays, tests execute faster and remain reliable regardless of execution order or system load.

What happens when a condition doesn't occur within the timeout?

The `waitFor` function throws a timeout error after the configurable timeout expires, providing clear failure messaging. This replaces silent test hangs or inconsistent passes, making debugging race condition failures faster.