Flaky Test Hunter
This loop automatically identifies and removes flaky tests in your Node.js project. A flaky test is one that passes and fails intermittently without any code changes. The agent will run your test suite multiple times, detect inconsistent test results, and attempt to fix or remove the flaky tests to stabilize your CI pipeline.
Goal
Remove flaky tests
How to Run
Run this loop in your Node.js project to automatically detect and eliminate flaky tests by repeatedly executing your test suite and making targeted fixes.
- 01
Project Setup
Ensure your Node.js project has npm test configured in package.json and all necessary dependencies installed.
- 02
Launch Agent
Start the Flaky Test Hunter loop in your preferred coding agent environment (Cursor, Claude Code, etc.).
- 03
Monitor Progress
The agent will run npm test up to 20 times consecutively. If tests pass consistently, the loop exits. If failures occur intermittently, the agent works to fix them.
- 04
Review Changes
After completion, review any test modifications, deletions, or configuration changes made by the agent to ensure correctness.
Workflow Steps
- 01
Run npm test once and capture the full output including exit code
- 02
Analyze test output to identify any failed tests
- 03
If tests pass, increment consecutive pass counter and run again until reaching 20 passes
- 04
If tests fail, check if the same test failed in previous runs - if yes, it's consistently failing (not flaky)
- 05
If different tests fail across runs or the same test sometimes passes/sometimes fails, mark as flaky
- 06
Investigate flaky tests by examining their implementation and dependencies
- 07
Fix or remove flaky tests using best practices: stabilize async operations, remove shared state dependencies, add proper assertions
- 08
Re-run tests to verify the fix eliminated the flakiness without introducing new issues
- 09
Repeat the process until 20 consecutive successful test runs are achieved or max iterations reached
Kickoff Prompt
Start the "Flaky Test Hunter" loop. Goal: Remove flaky tests Max iterations: 10 Between iterations run: npm test Exit when: 20 consecutive runs pass You are an autonomous agent specialized in eliminating flaky tests. Begin by running 'npm test' to establish a baseline. Then execute the test suite repeatedly to identify tests that pass and fail inconsistently. For each flaky test detected, analyze its code to find the root cause (race conditions, shared state, timing issues, etc.) and implement a proper fix that maintains test coverage while ensuring consistent behavior. Continue this process until you achieve 20 consecutive successful test runs or reach the maximum iteration limit. Self-pace this loop. After each iteration, run `npm test` and evaluate the output, and only continue if the exit condition is not met (20 consecutive runs pass). Stop when the exit condition passes or 10 iterations are reached. Give a short status update each pass.
Guardrails
hardcoded- ·Only modify test files related to detected flakiness; avoid changing unrelated application code
- ·Focus on root cause analysis rather than adding random retries or timeouts
- ·Preserve test intent and coverage when fixing or removing flaky tests
- ·Run each fix through code review before committing
- ·Use isolated debugging techniques to understand flakiness patterns
- ·Do not suppress legitimate test failures as flaky behavior
Flow Diagram
Related loops — CI
CI
Dependency Cache Optimizer
Automatically analyzes cache metrics, identifies dependency-related bottlenecks, and optimizes caching strategy to improve cache hit rates in CI environments.
CI
Parallelize CI Jobs
This loop optimizes CI pipeline efficiency by identifying and parallelizing independent jobs to reduce overall runtime. The agent iteratively analyzes the current pipeline structure, implements parallelization strategies, and validates improvements through metrics.
CI
CI Until Green
This loop continuously monitors and fixes CI issues until all checks pass. It runs the 'gh pr checks' command iteratively, analyzes failures, and applies fixes until the exit condition is met.