<?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: Christian Arty</title>
    <description>The latest articles on DEV Community by Christian Arty (@christianarty).</description>
    <link>https://dev.to/christianarty</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.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F430811%2F25d5f9db-aa32-42cb-87c4-0ff6b7ea62ad.jpeg</url>
      <title>DEV Community: Christian Arty</title>
      <link>https://dev.to/christianarty</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/christianarty"/>
    <language>en</language>
    <item>
      <title>Custom Arguments with Jest</title>
      <dc:creator>Christian Arty</dc:creator>
      <pubDate>Thu, 29 Apr 2021 11:59:10 +0000</pubDate>
      <link>https://dev.to/christianarty/custom-arguments-with-jest-1p5p</link>
      <guid>https://dev.to/christianarty/custom-arguments-with-jest-1p5p</guid>
      <description>&lt;p&gt;&lt;a href="https://jestjs.io/" rel="noopener noreferrer"&gt;Jest &lt;/a&gt; is a powerful test runner, assertion library, and simple to use. Personally, It has replaced other configurations I've used like &lt;a href="https://mochajs.org/" rel="noopener noreferrer"&gt;Mocha&lt;/a&gt;, &lt;a href="https://www.chaijs.com/" rel="noopener noreferrer"&gt;Chai&lt;/a&gt;, &lt;a href="https://sinonjs.org/" rel="noopener noreferrer"&gt;Sinon&lt;/a&gt; for my Javascript testing. But one thing I &lt;strong&gt;really&lt;/strong&gt; wished for, in Jest, is the ability to enable custom command line arguments. With custom arguments, I'm able to isolate testing configurations between npm scripts. Let's dive into what they are.&lt;/p&gt;

&lt;h2&gt;
  
  
  What are &lt;strong&gt;custom&lt;/strong&gt; arguments
&lt;/h2&gt;

&lt;p&gt;Normally, Jest has its own arguments that you can run like below:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;jest --runInBand
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The command above allows you to run your tests sequentially, instead of in parallel. It's a useful command for when you need to debug your tests.&lt;/p&gt;

&lt;p&gt;Custom arguments are similar to the above example, except you define what you want to send to the jest cli (aka "arguments")&lt;/p&gt;

&lt;p&gt;For example, let's say I want to do something like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;jest --failFast=true
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The example above illustrates that I would like the test suite to stop executing the rest of the test when the first failed test. As of right now, this is not supported by Jest natively. However, with custom arguments, this can become a reality. &lt;/p&gt;

&lt;p&gt;&lt;em&gt;(Note: Jest does have a fail fast(-ish) option built-in (&lt;code&gt;--bail&lt;/code&gt;) however, it exits on the first failed test **suite&lt;/em&gt;&lt;em&gt;, not **individual&lt;/em&gt;* test. Open issue about this: &lt;a href="https://github.com/facebook/jest/issues/6527" rel="noopener noreferrer"&gt;Github Issue&lt;/a&gt;)*&lt;/p&gt;

&lt;h2&gt;
  
  
  How to enable custom arguments
&lt;/h2&gt;

&lt;p&gt;Upon researching this topic, I came to the conclusion that the best way for this to work without impacting the existing functionality of jest and it's cli, was to create a &lt;code&gt;.js&lt;/code&gt; file that we pass custom arguments to, perform computation,  and pass the jest runner the modified, but supported arguments for the test suite(s).&lt;/p&gt;

&lt;p&gt;It would look something like this:&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%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1618637125626%2F-ZvZFX1CL.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%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1618637125626%2F-ZvZFX1CL.png" alt="carbon.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;And in your package.json, in your scripts portion, you would run it like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;test: "node preJest.js --runInBand",
test:failFast: "node preJest.js --failFast=true --runInBand"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This configuration utilizes a third-party dependency called &lt;a href="https://github.com/yargs/yargs" rel="noopener noreferrer"&gt;Yargs&lt;/a&gt; and &lt;a href="https://github.com/yargs/yargs-unparser" rel="noopener noreferrer"&gt;Yargs-unparser&lt;/a&gt;. Both of these utilities allow me to easily manipulate the CLI like JavaScript. &lt;/p&gt;

&lt;p&gt;With this configuration, you are able to share easily different configurations for your tests without the hassle of sharing and updating environment files and/or creating new ones for different use cases. &lt;/p&gt;

&lt;p&gt;Additionally, we are able to run the commands above, mixing both the custom arguments and the official arguments without any conflict.&lt;/p&gt;

&lt;p&gt;If you made it this far, thank you for reading my article. If you have any questions, comments, or concerns about this, either leave a comment or contact me at &lt;a href="//mailto:contact@christianarty.com"&gt;contact@christianarty.com&lt;/a&gt;&lt;/p&gt;

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