Sub topic 5
Using your test runner's shard option
This chapter assumes you can run your test suite from the command line and read a CI YAML file. It also assumes you know the split strategies from the previous chapter. Here you will use the splitting built into your runner, so you don't have to write your own.
What a shard option does
A shard option takes two numbers, index/total. The runner builds the full list of tests, divides it into total parts, and runs only part number index. Every job runs the same command with a different index.
Three rules hold in most runners:
- The index starts at 1.
--shard=1/4is the first part and--shard=4/4is the last.--shard=0/4is an error. - The total must be the same in every job. If one job says
/4and another says/5, the parts overlap and some tests never run. - Each test runs in exactly one shard, as long as the test list is identical across jobs.
Runners that support it
Playwright, Jest (version 28 and later) and Vitest all accept the same form.
# Playwright: run the second of four shards
npx playwright test --shard=2/4
# Jest: the same form
npx jest --shard=2/4
# Vitest: the same form
npx vitest run --shard=2/4Check your runner's documentation before relying on this. Mocha, for example, has no built-in shard option, so you would need the hash or timing splitter from earlier chapters.
What gets divided
Runners split by file by default. Playwright splits whole test files across shards unless you set fullyParallel: true in its config. With that setting it can split individual tests, which gives a more even spread. Jest splits test files only. This matters when one file holds most of your run time, because a single large file always lands in one shard. Shard options balance the number of items, not their duration. Timing-based balancing is a separate step.
Checking that nothing is missed
Before wiring up CI, confirm that the shards together cover the whole suite. Playwright's --list flag prints test names without running them.
total=$(npx playwright test --list | grep -c "›")
sharded=0
for i in 1 2 3 4; do
n=$(npx playwright test --list --shard=$i/4 | grep -c "›")
echo "shard $i: $n tests"
sharded=$((sharded + n))
done
echo "all: $total, sum of shards: $sharded"The two totals should match. If they don't, the test list differs between runs. A common cause is a test file that is generated at run time.
Using it in CI
In the CI matrix, the shard number becomes a matrix value and the total stays a constant. The key line from the earlier example is:
- run: npx playwright test --shard=${{ matrix.shard }}/4To avoid a mismatch, keep the matrix list and the /4 in step. Some teams set the total in one environment variable and generate the list from it.
Common errors
| Symptom | Likely cause |
|---|---|
| Runner rejects the option | Index is 0, or index is larger than total |
| Some tests never run | Jobs use different totals, or a matrix entry was removed |
| A test runs twice | Two matrix entries share the same index |
| A shard finishes in seconds | More shards than files, or one huge file holds most of the work |
| Shards disagree on the test list | Different config, environment or generated files between jobs |
Exercise
Your matrix is shard: [1, 2, 3, 4] but the command says --shard=${{ matrix.shard }}/3. Which tests run, and what is the fix?
Show answer
Jobs 1, 2 and 3 run the three parts of a 3-way split, so those tests all run. Job 4 uses an index larger than the total, so the runner rejects it (or runs nothing). The suite is still covered, but the fourth job is wasted and likely fails. Fix it by making the total match the matrix: change the command to /4, or remove entry 4 from the matrix.