16

Fix slow tests with Jest in Github Actions

 2 years ago
source link: https://dev.to/ddoice/fix-slow-tests-with-jest-in-github-actions-5h00
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

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

For reference the same tests in Jenkins 🤔

The same tests on my computer 🚀

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:

/usr/bin/time -v npm run test

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

Meanwhile, in the Github runners, the history was different

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:

/usr/bin/time -v npm run test -- --maxWorkers=2

Not 200% but better CPU usage:

And even better run times:

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


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK