<?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: Lucas Bandeira</title>
    <description>The latest articles on DEV Community by Lucas Bandeira (@lucasbandeiramj).</description>
    <link>https://dev.to/lucasbandeiramj</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%2F709946%2F1dbdc1df-a084-4fed-925f-b61440148e78.jpeg</url>
      <title>DEV Community: Lucas Bandeira</title>
      <link>https://dev.to/lucasbandeiramj</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/lucasbandeiramj"/>
    <language>en</language>
    <item>
      <title>Your front door to testing with Jest</title>
      <dc:creator>Lucas Bandeira</dc:creator>
      <pubDate>Tue, 21 Sep 2021 13:20:27 +0000</pubDate>
      <link>https://dev.to/lucasbandeiramj/your-front-door-to-testing-with-jest-2cl</link>
      <guid>https://dev.to/lucasbandeiramj/your-front-door-to-testing-with-jest-2cl</guid>
      <description>&lt;h2&gt;
  
  
  What is testing:
&lt;/h2&gt;

&lt;p&gt;Testing is, in short, to see if our code is executing properly, there is a number of ways to do so, but today we will be talking about automated tests.&lt;/p&gt;

&lt;p&gt;Automated tests are a software testing technique where we write pre-determined tests to check automatically if our code is running as expected.&lt;br&gt;
There are a number of benefits to writing automated tests, such as:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Saves a lot of time as oposed of manual testing&lt;/li&gt;
&lt;li&gt;Is a reliable way to prevent bugs and issues as we write our code&lt;/li&gt;
&lt;li&gt;Monitors the functionality of our code to see if any alterations we make breaks the app, before sending it to production&lt;/li&gt;
&lt;li&gt;Overall improves our coding skill&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;There are diferent types of testing, like:&lt;/p&gt;
&lt;h4&gt;
  
  
  End to end:
&lt;/h4&gt;

&lt;p&gt;Tests the entire aplication by clicking on links and navigating through the page as a user would interact with the app.&lt;/p&gt;
&lt;h4&gt;
  
  
  Integration:
&lt;/h4&gt;

&lt;p&gt;Verifies if multiple units of our code are working together as they should and returning the wanted outcome.&lt;/p&gt;
&lt;h4&gt;
  
  
  Unit:
&lt;/h4&gt;

&lt;p&gt;Verifies if individual, isolated units of our code (a Class or a function) are working.&lt;/p&gt;
&lt;h4&gt;
  
  
  Static:
&lt;/h4&gt;

&lt;p&gt;Catches typos or type errors as we are writing the code.&lt;/p&gt;

&lt;p&gt;Today we will be focusing on unit testing.&lt;/p&gt;
&lt;h2&gt;
  
  
  What is unit testing:
&lt;/h2&gt;

&lt;p&gt;Unit testing is to verify if individual parts of our code work as expected, such as methods returning a specified outcome if we pass specific parameters to it. They should be short isolated tests to check if there is any error in the functions that compose our app, and for that, today we will be introducing Jest.&lt;/p&gt;
&lt;h2&gt;
  
  
  What is Jest
&lt;/h2&gt;

&lt;p&gt;Jest describes itself as "a delightful JavaScript Testing Framework with a focus on simplicity", it was created by Christoph Nakazawa and is maintained by Facebook. It can be used to test code in projects that use: Babel, TypeScript, Node, React, Angular, Vue and more.&lt;/p&gt;
&lt;h2&gt;
  
  
  Installing Jest
&lt;/h2&gt;

&lt;p&gt;We can Install Jest by initiating a project with&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;npm&lt;/span&gt; &lt;span class="nx"&gt;init&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nx"&gt;y&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;to generate our package.json file. Then, we can add jest to our project with&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;npm&lt;/span&gt; &lt;span class="nx"&gt;install&lt;/span&gt; &lt;span class="nx"&gt;jest&lt;/span&gt; &lt;span class="o"&gt;--&lt;/span&gt;&lt;span class="nx"&gt;save&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nx"&gt;dev&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We are saving it as a dev dependency because we will be running our tests in development, before we send our app into production.&lt;/p&gt;

&lt;p&gt;Next, we should go into our generated package.json file and write our scripts for testing, like so&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;scripts&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;test&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;jest&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;
  &lt;span class="p"&gt;},&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Then, when we run this code with &lt;code&gt;npm run test&lt;/code&gt;, jest will automatically search our project for our test files and execute the tests we've written.&lt;/p&gt;

&lt;h3&gt;
  
  
  Creating our test file:
&lt;/h3&gt;

&lt;p&gt;Our test file should be named after the file we're trying to test, with the extension of ".test.js" or ".spec.js", these extensions are what Jest will look for in order to execute our tests, so, as an example, if we're trying to test a file called "hello.js", we should create a testing script called "hello.test.js".&lt;/p&gt;

&lt;p&gt;Lets create our "hello.js" file, which will contain the following code:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// hello.js&lt;/span&gt;
&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;hello&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;firstName&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;lastName&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="s2"&gt;`Hello &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;firstName&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt; &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;lastName&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;`&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="nx"&gt;module&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;exports&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;hello&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We will be using this syntax for exporting because jest doesn't natively support ES6+ exporting, it is possible to use, but would require some extra configuration.&lt;/p&gt;

&lt;p&gt;Now, we will create our test file by importing the method we're trying to test, then we will create our test using Jest's global functions "test" or "it", which takes two parameters, the first is a string which contains a brief description of the test we're trying to execute, and the second is a callback function containing the actual test.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// hello.test.js&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;hello&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;require&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;./hello&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="nf"&gt;test&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;should say hello to the full name passed&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;firstName&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;John&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;lastName&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Doe&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;result&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;hello&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;firstName&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;lastName&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="nf"&gt;expect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;result&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;toBe&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Hello John Doe&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;})&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We can run this code by typing &lt;code&gt;npm run test&lt;/code&gt; in our terminal, and we can see our first test running successfully.&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%2Fn8jn5pci9tpzw42dpbm3.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%2Fn8jn5pci9tpzw42dpbm3.png" alt="Passing test"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;And thats it! We've just written our first unit test, so lets go back and take a look at what we just did in greater detail.&lt;/p&gt;

&lt;p&gt;In this test we are using what is called a "triple A" pattern which stands for:&lt;/p&gt;

&lt;h3&gt;
  
  
  Arrange:
&lt;/h3&gt;

&lt;p&gt;To prepare what will be needed in order to execute our test&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;firstName&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;John&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;lastName&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Doe&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Act
&lt;/h3&gt;

&lt;p&gt;To call the method we're trying to test with the information we've already arranged&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;result&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;hello&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;firstName&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;lastName&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Assert
&lt;/h3&gt;

&lt;p&gt;To determine what is the expected outcome and if the function we've acted upon is executing properly.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nf"&gt;expect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;result&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;toBe&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Hello John Doe&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The first two steps are easy to follow, but let's take another look at step three and explain what is going on.&lt;/p&gt;

&lt;p&gt;Here, we are calling another one of Jest's functions, "expect", and that gives us access to what jest calls "matchers", which are methods that are used to see if our result meets the requirements of our test. They are pretty self explanatory, so in this example we want our result .toBe("Hello John Doe"). &lt;/p&gt;

&lt;p&gt;For a more in depth look into Jest Expect method and its Matchers we can access this link: &lt;a href="https://jestjs.io/docs/expect" rel="noopener noreferrer"&gt;Expect . Jest&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;And that's it for our introduction about unit testing with Jest, if you want to expand your knowledge about testing and what Jest is capable of, here are some links that I used as learning material:&lt;br&gt;
&lt;a href="https://jestjs.io/docs/getting-started" rel="noopener noreferrer"&gt;Getting Started . Jest&lt;/a&gt;&lt;br&gt;
&lt;a href="https://www.youtube.com/watch?v=r9HdJ8P6GQI" rel="noopener noreferrer"&gt;JavaScript Testing Introduction Tutorial - Unit Tests, Integration Tests &amp;amp; e2e Tests&lt;/a&gt;&lt;br&gt;
&lt;a href="https://www.youtube.com/watch?v=ajiAl5UNzBU" rel="noopener noreferrer"&gt;Jest Crash Course&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Next time we will take a deeper dive at Jest's capabilities by writting some tests to a faulty app.&lt;/p&gt;

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