All Articles

Speedup your Jest tests in GitHub Actions

2 min read

Recently at work, was working on optimizing the jest unit test case CI execution time for our front-end application. We are using GitHub Action that runs on every PR raised against master branch.

Backstory

At our current scale, it roughly takes about ~14mins to complete. We have this branch protection rule where the automated test cases should pass in order to be eligible for merging to master.

We have many developers working on this repository and on average 15+ PRs are raised per day. Also, this CI action will run every commit that is pushed to the branch after the PR is raised.

This reduces the velocity of the developers at the same increases billing costs unnecessarily.

In the above image, you can see we also have Jest Cache enabled. Though this reduces the time considerably still it is not enough.

Mainstory

Asked my buddy Google and tried a couple of solutions suggested. Nothing worked!!!😔. At first glance, this appeared like this is expected as the codebase has grown large, but wanted to confirm before arriving at a conclusion as usual 😜.

Started verifying whether the optimization configurations provided by jest out of the box are working as expected one by one. Surprisingly one setting maxWorkers was not working as mentioned in the CI environment. As per the docs, jest will utilize all the available cores in CI env. GH Action provides 2 cores Linux machines and Jest was consuming only one!!!.

When we explicitly passed the value as 2 --maxWorkers=2, the time got reduced by ~6mins.

Yeah! I know it is time-consuming to verify, but is really worth it since it is now 6 mins less wait for every developer working on this project for every PR push. Just do the Math!!!

Happy Hacking!

Bharathvaj Ganesan

Software developer 👨🏻‍💻 Loves building things over web...
Design Systems @chargebee!!!
Bharathvaj Ganesan on Twitter