@magic/test
CLI Sharding Cheat-Sheet
Run tests in parallel across multiple processes to speed up large test suites:
Basic Sharding Commands
# Run 4 shards, this is shard 0 (of 0-3)t --shards 4 --shard-id 0# Run shard 1t --shards 4 --shard-id 1# Run shard 2t --shards 4 --shard-id 2# Run shard 3t --shards 4 --shard-id 3
Tests are distributed deterministically using a hash of the test file path, ensuring each test always runs in the same shard.
Combine with Other Flags
# Run with production mode (no coverage)t -p --shards 4 --shard-id 2# Add verbose outputt -l --shards 4 --shard-id 1# Custom worker countt --shards 4 --shard-id 0 --workers 2
Package.json for CI/CD
Add these scripts to your package.json:
{"scripts": {"test": "t -p","test:shard:0": "t -p --shards 4 --shard-id 0","test:shard:1": "t -p --shards 4 --shard-id 1","test:shard:2": "t -p --shards 4 --shard-id 2","test:shard:3": "t -p --shards 4 --shard-id 3"}}
Run All Shards in Parallel
Use a single command to run all shards in parallel:
# Run all 4 shards in parallel and wait for all to completenpm run test:shard:0 & npm run test:shard:1 & npm run test:shard:2 & npm run test:shard:3 & wait
Or with production mode:
npm run test:shard:0 & npm run test:shard:1 & npm run test:shard:2 & npm run test:shard:3 & waitDeterministic Distribution
Test distribution is deterministic - the same test file will always run in the same shard. This ensures consistent behavior across CI runs.
See Also
- CLI & Usage - full CLI documentation
- CLI Flags Reference - detailed flag list
- Performance Tips - optimization techniques