Diagnose and fix flaky tests. Use when a test passes sometimes and fails other times.
Scanned 5/27/2026
Install via CLI
openskills install girijashankarj/cursor-handbook---
name: flaky-test
description: Diagnose and fix flaky tests. Use when a test passes sometimes and fails other times.
---
# Skill: Fix Flaky Tests
## Trigger
When a test is flaky — passes intermittently, fails on CI but not locally, or depends on timing/order.
## Steps
### Step 1: Reproduce
- [ ] Run the test 10+ times: `{{CONFIG.testing.testCommand}} -- --testPathPattern={file} --runInBand`
- [ ] Note failure rate and any pattern (e.g., fails when run with others)
- [ ] Run in isolation vs with full suite to check order dependency
### Step 2: Categorize
Common causes:
- **Timing** — Async not awaited; `setTimeout`/`setInterval`; race conditions
- **Shared state** — Mocks, globals, or DB state leaking between tests
- **Randomness** — `Date.now()`, `Math.random()`, `uuid` without seed
- **Order dependency** — Tests assume execution order; shared fixtures
- **Environment** — CI vs local (timezone, env vars, network)
### Step 3: Fix
#### Timing
- Add `await` for all async operations
- Use `waitFor` or `act` for React updates
- Replace `setTimeout` with fake timers: `jest.useFakeTimers()`
- Increase timeout only as last resort; prefer fixing the race
#### Shared State
- Reset mocks in `beforeEach`: `jest.clearAllMocks()` or `mockReset()`
- Use `beforeEach` to create fresh fixtures; avoid module-level state
- Run tests with `--runInBand` to isolate; fix root cause rather than rely on it
#### Randomness
- Mock `Date.now()`, `Math.random()`, or ID generators
- Use `jest.setSystemTime()` for date-dependent tests
- Seed random if deterministic output is needed
#### Order Dependency
- Make each test self-contained; no assumptions about other tests
- Use `beforeEach` for setup; avoid `afterAll` that affects later tests
### Step 4: Verify
- [ ] Run test 20+ times: `for i in {1..20}; do {{CONFIG.testing.testCommand}} -- --testPathPattern={file}; done`
- [ ] Run with full suite to ensure no regression
- [ ] Check CI passes on multiple runs
## Completion
- [ ] Test passes consistently (20/20 runs)
- [ ] No new flakiness introduced
- [ ] Root cause fixed, not masked (e.g., no arbitrary `sleep`)
No comments yet. Be the first to comment!
Use this skill when developing or maintaining browser extension code in the `browser/` directory, including Chrome/Firefox/Edge compatibility, content scripts, background scripts, or i18n updates.
SEO optimization with keyword analysis, readability assessment, technical validation, content quality. Use for search rankings, blog posts, content audits, or encountering keyword density, readability scores, meta tags, schema markup errors.
Python backend development expertise for FastAPI, security patterns, database operations, Upstash integrations, and code quality. Use when: (1) Building REST APIs with FastAPI, (2) Implementing JWT/OAuth2 authentication, (3) Setting up SQLAlchemy/async databases, (4) Integrating Redis/Upstash caching, (5) Refactoring AI-generated Python code (deslopification), (6) Designing API patterns, or (7) Optimizing backend performance.
Drive the full internationalization journey for a project — detect the stack, recommend a library, set up the chosen library, wrap existing strings, and optionally connect a translation platform. Use when the user asks to add or configure i18n, internationalization, localization, multi-language support, or translations — including when they explicitly mention LinguiJS, Lingui, next-intl, "wrap strings", "find hardcoded text", "make my app translatable", or "set up translations". Triggers on g...
PTES-aligned adversarial security audit for backend, frontend, and mobile applications. Produces a CVSS-scored Hacker Report with verified PoCs and phased remediation.