Sub topic 11
Fail-fast, Retries and Flaky Tests Across Shards
This chapter assumes you can read a CI matrix configuration and that you have already split a suite into shards with a runner's --shard=index/total option. Here you will decide what happens when one shard fails, and how to keep flaky tests from hiding inside the split.
What happens when one shard fails
A matrix runs several jobs at once. When one job fails, the CI system must decide whether to cancel the others. GitHub Actions calls this setting fail-fast. Its default is true, so the first failed shard cancels the rest.
| Setting | Behavior | Best for |
|---|---|---|
| `fail-fast: true` | Cancels running shards after the first failure | Saving runner minutes on pull requests |
| `fail-fast: false` | Lets every shard finish | Seeing all failures in one run |
With fail-fast: true, you fix one failure, push, and discover a second one in a shard that was cancelled. With fail-fast: false, you pay for every shard but get the full list of failures at once. Many teams cancel early on pull requests and run everything on the main branch.
Retries
A retry runs a failed test again before reporting it as failed. Runners support this directly. Playwright has a retries option, and Jest has jest.retryTimes(). A retry works per test inside a shard, so it does not need any coordination between jobs.
# Retry each failing Playwright test up to 2 more times
npx playwright test --shard=2/4 --retries=2Retrying a single test is cheaper than rerunning a whole shard. A test that fails and then passes is called flaky. Playwright marks such a test as "flaky" in its report instead of "passed", so you can still see it.
Why flakiness is harder with shards
Sharding can expose flaky tests that a single run hid. Three causes are common:
- Hidden order dependence. Test B only passes if test A ran first on the same machine. After sharding, A and B may land in different jobs.
- Shared resources. Shards that use the same database, bucket, or test account can overwrite each other's data. Give each shard its own namespace, for example by including the shard index in a database name.
- Shifting assignment. With count-based splitting, adding one file moves many files to other shards. A failure that appears only in one combination looks random.
A failed shard is not always a real failure. Before you blame the code, rerun only the failed job. GitHub Actions offers "Re-run failed jobs", which repeats the failed shards and keeps the passing ones.
Keeping flaky tests visible
Retries make a pipeline green, but they can also hide a decaying suite. Keep two habits:
- Record every test that passed only on a retry, and count them per week.
- Quarantine a repeat offender. Move it to a separate, non-blocking job so it stops blocking merges while someone fixes it.
jobs:
test:
runs-on: ubuntu-latest
strategy:
fail-fast: false
matrix:
shard: [1, 2, 3, 4]
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: 20
- run: npm ci
- run: npx playwright install --with-deps
- run: npx playwright test --shard=${{ matrix.shard }}/4 --retries=2
env:
DB_NAME: app_test_${{ matrix.shard }}Here DB_NAME gives each shard its own database, and --retries=2 handles single-test flakiness.
Exercise
Your four-shard pull request run uses fail-fast: true. Shard 3 fails and shards 1, 2 and 4 are cancelled. You fix the bug, push, and shard 1 now fails. What setting change would have shown you both failures in the first run, and what is the cost?
Show answer
Set fail-fast: false. Every shard then runs to completion and reports its failures, so you would see the shard 3 and shard 1 failures together. The cost is that the cancelled shards now use full runner minutes on every failed run.
Exercise
A test passes alone but fails only when it lands in shard 2. Name two likely causes and one way to check each.
Show answer
Order dependence: it relies on state from a test that runs in another shard. Check by running shard 2 locally with the same --shard value, or running the test with the files in shard 2 only. Shared resources: it collides with another shard's data. Check by giving each shard a separate database or namespace and seeing if the failure disappears.