How to split a test suite across machines
Shard a test suite by file, count or recorded duration, with the real flags for Vitest, Jest, Playwright, pytest and Go, plus measured shard timings.
Riley Hilliard9 min read
testingcli
To split a test suite across machines, give each machine a fixed slice of the test files, run the same command everywhere with a different slice number, and fail the build if any slice fails. Most runners cut the slices for you: vitest run --shard=2/3 runs the second third of the suite. Total time becomes the time of the slowest slice plus the setup every machine repeats, so the evenness of the cut sets your speedup, not the number of machines.
How do you decide which tests go on which machine?
There are three common ways to cut a suite, and they differ in how well they balance the load.
By directory or package: backend on one machine, frontend on another. It needs no tooling and the slices make sense to a human, but the slices are only as even as your project layout, and a new package means editing the CI config.
By count or hash: sort the test files, give every Nth file to machine N (or hash each path and take it modulo the shard count). It balances the number of files, and it keeps working as files come and go. It knows nothing about how long each file takes.
By recorded duration: store how long each test took on a previous run, then pack tests into slices so the totals match. This is the only method that balances time rather than file counts, and it costs you a timing file that goes stale as the suite changes.
The reason balance matters is that the machines run at the same time, so the run finishes when the last one does. Three shards of 10, 10 and 40 seconds take 40 seconds, the same as one machine doing the 40-second shard alone. The other 20 seconds of work saved you nothing.
What flag does each test runner use?
Vitest, Jest and Playwright have a built-in flag, pytest has one through the pytest-split plugin, and Go has none. The “splits on” column is what each project’s own documentation says, because that decides how even your shards will be.
| Runner | Command | Splits on, per its docs |
|---|---|---|
| Vitest | vitest run --shard=1/3 |
Test files, not test cases. It can’t be combined with --watch. |
| Jest | jest --shard=1/3 |
“One third of the tests” is all the docs say. The configured testSequencer has to implement a shard method. |
| Playwright | playwright test --shard=1/3 |
Test files by default, individual tests when fullyParallel: true is set, which its docs recommend for balanced shards. |
| pytest-split | pytest --splits 3 --group 1 |
Tests, using durations stored by pytest --store-durations in .test_durations. |
| go test | go test ./pkg/a ./pkg/b |
Whatever packages you list. There’s no shard flag. |
Two notes on that table.
pytest-xdist is the usual answer to “run pytest in parallel”, but its distribution docs describe sending tests to multiple CPUs with -n, and say nothing about other machines. For several machines, use pytest-split to pick the slice and xdist inside each machine if you want to use its cores. pytest-split’s default algorithm is duration_based_chunks; its least_duration algorithm is documented as giving more balanced groups. For a test with no stored duration, it assumes the average of the ones it has.
For Go, you build the slices yourself. I ran this in a throwaway module with five packages, and it ran packages a and d (every third line of go list, starting at the first):
go test $(go list ./... | awk 'NR % 3 == 1')
Change 1 to 2 or 0 for the other two machines.
That’s count-based splitting by package. It balances only as well as your packages are the same size.
How evenly does count-based splitting divide a real suite?
Badly, in the one suite I measured. I ran this site’s own Vitest suite (44 test files, 628 tests) on a MacBook with an M3 Max and 16 cores, Vitest 5.0.5 on Node 24.21. Other work was running on the machine at the time (load average about 9), so treat the absolute numbers loosely. Each row is three separate runs, in non-watch mode with a timeout, and the times are wall-clock for the whole command including Vitest’s startup.
| Run | Test files | Wall time, 3 runs |
|---|---|---|
| Whole suite | 44 | 9.75 s, 9.62 s, 9.89 s |
--shard=1/3 |
15 | 9.07 s, 9.17 s, 9.15 s |
--shard=2/3 |
15 | 1.85 s, 2.02 s, 1.94 s |
--shard=3/3 |
14 | 2.42 s, 2.66 s, 2.50 s |
The files were split almost perfectly evenly, 15, 15 and 14. The time wasn’t. Shard 1 took about 9.1 seconds, which is about 0.6 seconds less than running everything in one process. Shards 2 and 3 were done in 2 to 3 seconds.
I asked Vitest’s JSON reporter which files made shard 1 slow, in a single run. One file, src/lib/night-drive/session.test.ts, took about 8.5 seconds, and boss-skill.test.ts took about 3.7 seconds. Both test the rules of this site’s hero game, and the next-slowest file in that shard took 0.4 seconds. The slowest file sets a floor: no way of dealing out files can make the run faster than that one file, and a count-based split just happened to put it in the same shard as another heavy one.
Adding the three shard times gives about 13.1 seconds against 9.7 for the whole suite. Shards 2 and 3 ran 119 and 272 tests in about 2 seconds each, so most of the extra looks like each Vitest process starting up, though I didn’t isolate startup.
How do you run the shards and merge the results?
Start one command per machine in the background, remember each process ID, and wait on each one. A bare wait returns 0 no matter what the jobs did, so collect the exit codes yourself:
pids=()
for i in 1 2 3; do
ssh "box$i" "cd src/app && bunx vitest run --shard=$i/3" &
pids+=($!)
done
status=0
for p in "${pids[@]}"; do wait "$p" || status=1; done
exit $status
I checked the exit-code pattern with two background jobs, one exiting 0 and one exiting 3. The script finished with status 1. Getting your code onto the machines is covered in running tests on a remote machine over SSH.
Three consoles’ worth of output is hard to read, and CI wants one report. Vitest and Playwright both have a blob reporter for that. Each shard writes a blob file, you copy the files into one directory, and a final step merges them. With Vitest:
vitest run --shard=1/3 --reporter=blob # on each machine, with its own index
vitest run --merge-reports # once, after copying the blob files together
I ran all three shards with the blob reporter, and Vitest wrote blob-1-3.json, blob-2-3.json and blob-3-3.json into .vitest/blob/. The merge step then reported 44 files and 628 tests passed, with a “Per blob” line of 9.95 s, 2.81 s and 3.19 s. Vitest’s docs say to set VITEST_BLOB_LABEL if you run the same shards in several environments. Playwright’s equivalent is playwright merge-reports, which reads a directory of blob reports and writes a single report. For the other runners, check whether yours has a merge step before you write one.
What does every machine pay for?
Each machine needs your code, your dependencies and, if the tests run against built output, a build. Sharding divides the tests, and it divides none of that.
Startup shows up in the table above (see the 13.1 versus 9.7 seconds). On this suite, bun install --frozen-lockfile with a warm cache took 1.9 seconds. I didn’t measure a cold cache.
If sync, install and build take two minutes on every box, a ten-minute suite cut into five shards still costs each machine two minutes before the first test runs. A cache for dependencies, and building once and shipping the output to every shard, are the usual ways to keep that cost from multiplying.
What breaks when shards run at the same time?
Anything the tests share. On separate machines, the shared things are external: one database server, one staging environment, one third-party sandbox account. Two shards that create the same user or truncate the same table will fail intermittently, and the failure moves around between runs.
If two shards land on the same machine (a CI runner with spare cores, or your laptop), there’s more to collide on: a hard-coded port, a SQLite file or temp directory with a fixed name, a coverage output path, a lock file in the project directory. Derive these from the shard index, such as port 4000 + i or a database named test_shard_2, so each slice has its own. And give every shard its own output file for reports, since three shards writing report.xml to the same path leaves you with one report.
When is sharding not worth it?
When the suite is already short. Under a minute, the fixed costs above are a large share of the total, and the coordination (merging reports, combining exit codes, debugging a failure that only shows up in shard 3) costs more than the time saved. This site’s suite is in that range: about 10 seconds, and the slowest shard still took about 9.1 seconds because one file dominates. The better fix there is the 8.5-second file, not more machines.
Look at the slowest single file before you shard. If one file is most of the runtime, splitting files across machines can’t help, and splitting that file can. Check the same thing after you shard: the slowest shard sets your time, and if it’s much slower than the others, switch to a duration-based split.
Can a tool run the shards for you?
If you’d rather not write the SSH loop, rr is a small open source CLI I work on that grew out of this problem. You list tasks under parallel: in .rr.yaml, and rr sends each one to a free host over SSH, taking a lock on the host so two runs never share it. rr’s README has an example with three tasks that would take about 97 seconds in sequence and finished in 52.9 seconds in parallel, the time of the slowest task. That’s rr’s own example, not something I measured here.
$ brew install rileyhilliard/tap/rr