Mocha's --retries Flag: A Pragmatic Band-Aid for Flaky Tests
Mocha's --retries flag automatically reruns failed tests, but it masks root causes. Use it surgically on flaky integration tests while you fix the underlying shared state or timing issues.
14 Aug 2026, 07:02 UTC

The Problem: Flaky Tests Erode Trust
Every team hits it eventually: a test that passes locally but fails once in twenty runs on CI. The failure vanishes on rerun. The culprit is usually shared state, timing sensitivity, or an external dependency — not the code under test. Mocha's --retries flag lets you automatically rerun failed tests up to N times before marking the suite red. It's a practical engineering decision: use it to keep the pipeline moving while you hunt the root cause, but don't let it become permanent technical debt.
How --retries Works
When you pass --retries 2, Mocha will re-execute any failing test up to two additional times. Each retry runs the full test body and its surrounding beforeEach/afterEach hooks. This matters: if your flakiness stems from leaked state between tests, the retry may pass simply because the previous attempt's cleanup ran. The reporter (spec, dot, or JSON) shows each attempt, so you can see which tests needed retries.
You can also set retries programmatically inside a suite or test:
describe('payment gateway', function() {
this.retries(3); // applies to all tests in this suite
it('handles network timeout', async function() {
// test that occasionally times out
});
});
Programmatic retries are useful when only a subset of tests — say, integration tests hitting a sandbox environment — need the safety net.
Worked Example: Stabilizing a CI Pipeline
Suppose your suite has an integration test that calls a third-party API. Network blips cause ~5% failure rate. You decide to allow two retries for this test file only, keeping the rest of the suite strict.
# package.json
{
"scripts": {
"test": "mocha",
"test:ci": "mocha --retries 0",
"test:integration": "mocha test/integration/**/*.spec.js --retries 2"
}
}
Run npm run test:integration locally to verify the retry behavior. In CI, you might run the strict suite first, then the integration suite with retries. The spec reporter output will show something like:
payment gateway
✓ handles network timeout (2 retries)
✓ processes refund
2 passing (3 retries total)
This visibility lets you track whether retries are increasing over time — a signal that the underlying flakiness is worsening.
Trade-offs and Limitations
- Masking root causes: Retries hide shared-state bugs, race conditions, and external flakiness. The test eventually passes, so the ticket gets closed, but the defect remains.
- Suite time inflation: A 30-second integration test with
--retries 2can consume 90 seconds on a flaky run. In a large suite, this adds up fast and delays feedback. - Hook side effects: Since
beforeEach/afterEachrerun on each attempt, tests that depend on hook execution count (e.g., counting DB inserts) will behave differently under retry. - Version differences: Mocha 10+ changed default timeout behavior; always verify
npx mocha --versionmatches your expectations before relying on retry semantics.
Actionable Closing: Use Retries as a Diagnostic, Not a Default
Add --retries to a specific test file or suite via a dedicated npm script. Monitor the retry count in CI logs weekly. If a test consistently needs retries, treat it as a bug: investigate the state leak, add proper waiting, or mock the external dependency. Remove the retry once the root cause is fixed. The flag is a tool for buying time — not a substitute for deterministic tests.