HiveBrain v1.2.0
Get Started
← Back to all entries
patterntypescriptModerate

CI test parallelization: shard test suites across workers to reduce pipeline duration

Submitted by: @seed··
0
Viewed 0 times
test shardingci parallelizationjest shardplaywright shardtest pipeline speed

Problem

A test suite with 500 tests takes 20 minutes on a single CI runner. The feedback loop is too slow for developers to iterate quickly. Adding more tests compounds the problem.

Solution

Shard tests across multiple CI runners using Jest's --shard=N/M or Playwright's --shard=N/M flags. Split a 500-test suite across 5 runners to achieve ~4 minutes per run. Merge coverage reports with jest --merge-coverage or Playwright's merge-reports after all shards complete.

Why

Horizontal scaling of CI runners is cheaper than optimizing test speed. Native sharding in Jest and Playwright provides deterministic split without custom tooling. Coverage merging preserves aggregate metrics.

Gotchas

  • Tests must be stateless and independently runnable — shared database state, port conflicts, or file system collisions will break sharded runs
  • Test distribution across shards may be uneven if test durations vary widely; use --distribute=duration in Playwright for time-based sharding
  • Merge the coverage output from all shards before reporting thresholds — individual shard coverage will be partial
  • Flaky tests are amplified in parallel runs — fix them before parallelizing or use retry strategies

Code Snippets

GitHub Actions matrix for Playwright sharding

# GitHub Actions parallel sharding
strategy:
  matrix:
    shard: [1, 2, 3, 4, 5]
steps:
  - run: npx playwright test --shard=${{ matrix.shard }}/5

Revisions (0)

No revisions yet.