DEV Community

Rafael Garcia Lara
Rafael Garcia Lara

Posted on

Fix slow tests with Jest in Github Actions

At bob.io we were migrating a ton of repositories from our oldie Jenkins to GitHub Actions and everything was going really smooth, in some of our tests with mocha and chai we shaved a nice 40% running time, and with the matrix strategy, we can run a lot of test in parallel removing a lot of friction from our CI process.

We were in heaven until we migrated a frontend app with tests made in Jest.

On our first try the tests took a hefty amount of time

Image description

For reference the same tests in Jenkins πŸ€”

Image description

The same tests on my computer πŸš€

Image description

At first glance, it appeared that is CPU bound, but in mocha and chai the history was the opposite, we reduced the run times, so I did a small investigation using the time command like this:


 -v npm run test

Enter fullscreen mode Exit fullscreen mode

In my computer the tests were parallelized and used a good amount of CPU power
Image description

Meanwhile, in the Github runners, the history was different
Image description

Looks like, under the hood, Jest checks the number of cores available and uses as much as possible but never uses all the cores, the instances running our test have 2 cores available but Jest only uses 1.

After digging into the Jest documentation I found the maxWorkers option which allows you to control the number of CPU cores used.

I tried to run the same test but forced the workers to fully use the available cores in the runner:


 -v npm run test -- --maxWorkers=2

Enter fullscreen mode Exit fullscreen mode

Not 200% but better CPU usage:
Image description

And even better run times:
Image description

Just for the sake of curiosity, I tested with 3 workers and it took 1:34 so we left it with 2 workers.

Top comments (0)