Jest: Parallelizing across machines in CI

Created on 25 May 2018  路  4Comments  路  Source: facebook/jest

Many CI systems support splitting up a single task across multiple machines. This can drastically speed up CI times even when you are duplicating some of the work compiling or whatever.

From what I've seen, they mostly work like this:

steps:
  - command: yarn install && yarn test
    parallelism: 3
# on machine 1
CI_NODE_TOTAL=3 CI_NODE_INDEX=0 yarn install && yarn test

# on machine 2
CI_NODE_TOTAL=3 CI_NODE_INDEX=1 yarn install && yarn test

# on machine 3
CI_NODE_TOTAL=3 CI_NODE_INDEX=2 yarn install && yarn test

For example: https://buildkite.com/docs/builds/parallel-builds

It would be awesome if there was an easy way to integrate this with Jest such that you could automatically chunk tests up so that they can be run across machines.

I imagine you'd have to discover all the tests and then have some stable way of splitting them up, either by counting the tests or by splitting the files up.

I think you could even automatically do this without any additional setup from developers. I created a module to help you do just that: https://github.com/jamiebuilds/ci-parallel-vars


We do this in our project today by starting 4 separate Jest runs on different machines:

steps:
  - CI_NODE_TOTAL=4 CI_NODE_INDEX=0 JEST_TESTS=$(jest --listTests --json) jest
  - CI_NODE_TOTAL=4 CI_NODE_INDEX=1 JEST_TESTS=$(jest --listTests --json) jest
  - CI_NODE_TOTAL=4 CI_NODE_INDEX=2 JEST_TESTS=$(jest --listTests --json) jest
  - CI_NODE_TOTAL=4 CI_NODE_INDEX=3 JEST_TESTS=$(jest --listTests --json) jest

Then in jest.config.js we split up the tests:

let parallelism = require('ci-parallel-vars')
let chunkd = require('chunkd')

let tests = JSON.parse(process.env.JEST_TESTS).sort((a, b) => {
  return b.localeCompare(a)
})

if (parallelism) {
  tests = chunkd(tests, parallelism.index, parallelism.total)
}

module.exports = {
  testMatch: tests,
}

This sped up our builds significantly

Most helpful comment

i was talking about something else (host process and a bunch of remote clients), but i think it would be nice to do jest --chunk=2/10 kind of stuff in Jest

All 4 comments

@aaronabramov you talked about something like this at FB. But I don't remember if you said it worked for you, or if you wanted something like it?

i was talking about something else (host process and a bunch of remote clients), but i think it would be nice to do jest --chunk=2/10 kind of stuff in Jest

I would like to see this to make CI fast and it feels like a common request.

Also reported in: https://github.com/facebook/jest/issues/2330

Cypress has the feature to slice or load balance tests, and it's really neat, since it can be set up just by adding a parallel execution strategy to the CI pipeline. I'm surprised there's been no traction for this feature.

Was this page helpful?
0 / 5 - 0 ratings