<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Rafael Garcia Lara</title>
    <description>The latest articles on DEV Community by Rafael Garcia Lara (@ddoice).</description>
    <link>https://dev.to/ddoice</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F491031%2F8fae51d3-1e20-40d2-9284-a6ce1143fef2.png</url>
      <title>DEV Community: Rafael Garcia Lara</title>
      <link>https://dev.to/ddoice</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/ddoice"/>
    <language>en</language>
    <item>
      <title>Fix slow tests with Jest in Github Actions</title>
      <dc:creator>Rafael Garcia Lara</dc:creator>
      <pubDate>Fri, 26 Nov 2021 10:07:05 +0000</pubDate>
      <link>https://dev.to/ddoice/fix-slow-tests-with-jest-in-github-actions-5h00</link>
      <guid>https://dev.to/ddoice/fix-slow-tests-with-jest-in-github-actions-5h00</guid>
      <description>&lt;p&gt;At &lt;a href="https://bob.io" rel="noopener noreferrer"&gt;bob.io&lt;/a&gt; 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.&lt;/p&gt;

&lt;p&gt;We were in heaven until we migrated a frontend app with tests made in Jest.&lt;/p&gt;

&lt;p&gt;On our first try the tests took a hefty amount of time&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fuguk96s70ed64zph3l93.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fuguk96s70ed64zph3l93.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;For reference the same tests in Jenkins 🤔&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fjuz2tt33sjgzadf5b3k7.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fjuz2tt33sjgzadf5b3k7.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The same tests on my computer 🚀&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F5323pc59fugrnuz85zof.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F5323pc59fugrnuz85zof.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;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:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
 -v npm run test

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;In my computer the tests were parallelized and used a good amount of CPU power&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fa6bg95pdant3sxx84gdu.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fa6bg95pdant3sxx84gdu.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Meanwhile, in the Github runners, the history was different&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Frc86zvl4zsmp819m4au1.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Frc86zvl4zsmp819m4au1.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;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.&lt;/p&gt;

&lt;p&gt;After digging into the Jest documentation I found the &lt;a href="https://jestjs.io/es-ES/docs/cli#--maxworkersnumstring" rel="noopener noreferrer"&gt;maxWorkers&lt;/a&gt; option which allows you to control the number of CPU cores used.&lt;/p&gt;

&lt;p&gt;I tried to run the same test but forced the workers to fully use the available cores in the runner:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
 -v npm run test -- --maxWorkers=2

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Not 200% but better CPU usage:&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fbq3korhcy2ktm3dvwn47.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fbq3korhcy2ktm3dvwn47.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;And even better run times:&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fpr1odede1rw8ffp13ejg.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fpr1odede1rw8ffp13ejg.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Just for the sake of curiosity, I tested with 3 workers and it took 1:34 so we left it with 2 workers.&lt;/p&gt;

</description>
      <category>testing</category>
      <category>github</category>
      <category>javascript</category>
    </item>
  </channel>
</rss>
