How to shard Jest tests in GitHub Actions


This article goes over how to shard Jest tests in GitHub Actions.

Workflow

Shard Jest test suite using GitHub Actions matrix:

# .github/workflows/jest-shard.yml
on: push
jobs:
  jest-shard:
    runs-on: ubuntu-latest
    strategy:
      matrix:
        shard: [1/2, 2/2]
    steps:
      - npx jest --shard=${{ matrix.shard }}

To increase to 3 shards:

# .github/workflows/jest-shard.yml
on: push
jobs:
  jest-shard:
    runs-on: ubuntu-latest
    strategy:
      matrix:
        shard: [1/3, 2/3, 3/3]
    steps:
      - npx jest --shard=${{ matrix.shard }}

Sharding will speed up the time it takes for tests to run since they run in parallel, but it will also increase the total duration, which can increase the billable time.



Please support this site and join our Discord!