question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Parallelizing across machines in CI

See original GitHub issue

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

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:43
  • Comments:11 (4 by maintainers)

github_iconTop GitHub Comments

15reactions
aaronabramovcommented, May 27, 2018

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

9reactions
thiamsantoscommented, Dec 8, 2021

I would love to see this feature been native to jest. Right now we have been using the approach suggested at https://github.com/facebook/jest/issues/11252#issuecomment-813494558. But seems to be a common enough functionality to be added to jest itself. When a project reaches a certain size is nice to be able to split across different jobs/machines in CI.

Seems that we have a few suggestions on how the API would look like:

  • Parallel vars: CI_NODE_TOTAL=3 CI_NODE_INDEX=0 yarn test
  • Chunk: yarn test --chunk=0/3
  • Shard from #11252 : jest --shard-from=0 --shard-to=0.2

I would like to add another suggestion, based on elixir test framework:

JEST_PARTITION=2 yarn test --partitions 3 or yarn test --total-partitions 4 --partition 1


I would be happy to send a PR if the feature is welcome.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Parallelization | Cypress Documentation
Running tests in parallel across many virtual machines can save your team time and money when running tests in Continuous Integration (CI).
Read more >
Comparing CI Systems Parallelization | Incredibuild
This post about Parallel CI reviews the available parallelization features of common CI/CD platforms: Jenkins, Bamboo, GitLab, CircleCI, ...
Read more >
What is Parallel Testing and How Does it Work? - Semaphore CI
Vertical parallelization happens when using tools that can isolate tasks and run them concurrently, taking advantage of the many cores the CI ......
Read more >
How to Save Time Running Automated Tests with Parallel CI ...
As a first step you can apply a simple solution. Sort all of your test files alphabetically and divide them by the number...
Read more >
Accelerate CI/CD pipelines with Parallel Testing | BrowserStack
Selenium Grid allows running test cases in different machines across different platforms simultaneously. The triggering of these test cases is ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found