How to Balance Test Shards With Knapsack Pro
Knapsack Pro Queue Mode assigns tests to nodes at runtime, so a slow node simply receives fewer remaining tests.
Instead of splitting upfront, Queue Mode has each node request the next test from the Knapsack API, self-balancing as the run proceeds.
Steps
- Add the Knapsack Pro adapter for your test runner.
- Set
KNAPSACK_PRO_CI_NODE_TOTALandKNAPSACK_PRO_CI_NODE_INDEXper job. - Provide
KNAPSACK_PRO_TEST_SUITE_TOKENas a secret.
Workflow
.github/workflows/ci.yml
jobs:
test:
runs-on: ubuntu-latest
strategy:
fail-fast: false
matrix:
ci_node_index: [0, 1, 2]
env:
KNAPSACK_PRO_CI_NODE_TOTAL: 3
KNAPSACK_PRO_CI_NODE_INDEX: ${{ matrix.ci_node_index }}
KNAPSACK_PRO_TEST_SUITE_TOKEN: ${{ secrets.KNAPSACK_TOKEN }}
steps:
- uses: actions/checkout@v4
- run: npm ci
- run: npx knapsack-pro-jestGotchas
- Queue Mode needs network access to the Knapsack API during the run.
- Node total must match the matrix length or some tests never get assigned.
Related guides
How to Split Tests Dynamically at Runtime in CIAssign tests to CI nodes at runtime with a shared work queue so a slow node simply takes fewer remaining test…
How to Parallelize Cypress Tests With Cypress CloudRun Cypress specs across multiple CI machines with cypress run --parallel and --record, letting Cypress Cloud…