<?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</title>
    <description>The latest articles on DEV Community by Rafael (@rafavls).</description>
    <link>https://dev.to/rafavls</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%2F535707%2Fcbeab7a1-c23f-4251-a22f-4cbfe071d05f.jpg</url>
      <title>DEV Community: Rafael</title>
      <link>https://dev.to/rafavls</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/rafavls"/>
    <language>en</language>
    <item>
      <title>Testing JavaScript with Jest - Unit Testing</title>
      <dc:creator>Rafael</dc:creator>
      <pubDate>Wed, 11 Aug 2021 22:52:16 +0000</pubDate>
      <link>https://dev.to/rafavls/testing-javascript-with-jest-unit-testing-jmb</link>
      <guid>https://dev.to/rafavls/testing-javascript-with-jest-unit-testing-jmb</guid>
      <description>&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;Over the last couple of weeks I've been immersing myself in the world of testing my JavaScript and Python projects, and OH BOY. It's so much fun that I can't believe I didn't start learning it sooner.&lt;/p&gt;

&lt;p&gt;I've come to realize that testing our code is &lt;strong&gt;essential&lt;/strong&gt; for writing maintainable, reusable, and modular code. And it also makes it easy for any contributors, colleagues, and in general any people we work with to be almost absolutely sure their new &lt;code&gt;coolAndGreatFunction420()&lt;/code&gt; doesn't break our entire project.&lt;/p&gt;

&lt;p&gt;This article will cover the very basics of how testing works, what it's used for, and how to implement it in our Node.js apps using jest.&lt;/p&gt;




&lt;h2&gt;
  
  
  What is testing?
&lt;/h2&gt;

&lt;p&gt;Testing code is the process of making sure our software behaves the way we intend it to. Testing our code can help us feel more comfortable with our final product.&lt;/p&gt;

&lt;p&gt;For example, if we have a program which purpose is to add 2 + 2 and return 4, we'd like to make sure it does &lt;strong&gt;exactly&lt;/strong&gt; that. We don't want it to return 5, or 1, or "cuatro", we want it to return 4. Tests enable us to make sure this program behaves as expected every time we run it. &lt;/p&gt;

&lt;p&gt;Testing software comes in different shapes and sizes. For example, we could test the program mentioned above by simply using it the way a user would. We could launch a terminal, or a browser, or any kind of GUI, and run the program several times, making sure it always returns the expected value. The fun kind of testing, however, is &lt;strong&gt;automated testing&lt;/strong&gt;. &lt;/p&gt;

&lt;p&gt;Automated testing is code that tests code. Awesome, right? This can be achieved by using frameworks that enable us to write testing code. &lt;/p&gt;

&lt;p&gt;Even though automated testing is the focus of this article, I think it's still important to manually test our programs. This way we make sure our end-users have the best experience possible with our products.&lt;/p&gt;

&lt;p&gt;It's important to note that testing -no matter how in-depth or complex our tests are- can't ensure bug-free code. However, I do believe that testing improves code quality and makes better products in the end.&lt;/p&gt;




&lt;h2&gt;
  
  
  Types of tests
&lt;/h2&gt;

&lt;p&gt;Before we get into practical examples, we should know the common types of testing. These are not the only types that exist, but the most popular ones in the world of JavaScript.&lt;/p&gt;

&lt;h3&gt;
  
  
  Unit tests
&lt;/h3&gt;

&lt;p&gt;Unit testing covers &lt;em&gt;blocks of code&lt;/em&gt;, making sure they work the way the are intended to work. A unit could be a function, a class, or an entire module. Personally, I recommend unit tests to be limited to functions, just because I try to test the smallest parts of my code first, but there is no real rule for this. We can have two types of units:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Isolated or solitary units&lt;/strong&gt;: units that have no other dependencies, and which behavior and/or output depend only of the block contained within it.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Sociable units&lt;/strong&gt;: these are units that have dependencies. Their execution and optional output depends on other units. When testing, this means that we gotta make sure their dependencies work as expected before testing them.&lt;br&gt;
&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// This is an isolated unit&lt;/span&gt;
&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;myNameIs&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;nameString&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;`Will the real &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;nameString&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt; please stand up`&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="c1"&gt;// This is a sociable unit, because it depends on other units&lt;/span&gt;
&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;pleaseStandUp&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="nx"&gt;myNameIs&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Slim Shady&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;please stand up, please stand up&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;h3&gt;
  
  
  Integration tests
&lt;/h3&gt;

&lt;p&gt;Just because our unit tests pass doesn't mean we have a functioning and complete application. Once we have made sure that our units are properly tested and work by themselves, we test them together in the same way they are used in our software. This is integration testing. Putting these units and testing them together ensures that our functions, classes, and modules play well with each other.&lt;/p&gt;

&lt;h3&gt;
  
  
  End to end tests (E2E)
&lt;/h3&gt;

&lt;p&gt;End to end testing (E2E) takes our application for a ride from beginning to end. By this, I mean that this type of testing focuses on the user's experience whe using our software.&lt;/p&gt;

&lt;p&gt;Remember how I said that manual testing is important, even when we have automated tests set up? Well, E2E testing is basically &lt;em&gt;automated manual testing&lt;/em&gt; (try to explain that to a non-developer). These tests take place in the browser typically in a &lt;a href="https://en.wikipedia.org/wiki/Headless_browser"&gt;headless browser&lt;/a&gt;, although they can be run in browsers with a GUI. Through our test, we try to replicate as much as possible a user's interactions with our site, and make sure the output is what we expect.&lt;/p&gt;

&lt;p&gt;In addition to replicating a user's &lt;em&gt;navigational flow&lt;/em&gt; through the website, I actually also like to try to break things in these types of tests, as if I were a user typing and clicking madly through the site.&lt;/p&gt;




&lt;h2&gt;
  
  
  Unit testing with Jest
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://jestjs.io/"&gt;Jest&lt;/a&gt; is a Facebook Open Source product that enables us to write and run tests in pretty much any kind of JavaScript framework we prefer. &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;📖 NOTE&lt;/p&gt;

&lt;p&gt;I know, I also don't love the fact that Jest -and React, for that matter- are Facebook products. However, I gotta admit that they are good products and Facebook is a topic for another post.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;To install and use Jest in our project, we can run:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;npm i &lt;span class="nt"&gt;-D&lt;/span&gt; jest
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Then we can add a testing script to our &lt;code&gt;package.json&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="nl"&gt;"scripts"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"test"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"jest"&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Whenever Jest is run, it'll automatically look for and run files that end in &lt;code&gt;.test.js&lt;/code&gt;, &lt;code&gt;.spec.js&lt;/code&gt; or any &lt;code&gt;.js&lt;/code&gt; files that are inside of the &lt;code&gt;__tests__&lt;/code&gt; directory.&lt;/p&gt;

&lt;p&gt;Now, let's go ahead and write the unit that we want to test. And don't worry, these may look simple, but they are actual functions that I've had to use in real-life projects.&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;// helpers.js&lt;/span&gt;
&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;isNumber&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;possibleNumber&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="k"&gt;typeof&lt;/span&gt; &lt;span class="nx"&gt;possibleNumber&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;number&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="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;isNumber&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;There we go, a very simple function that shouldn't be hard to test... right? Let's try writing our first test. For this example, let's assume the test file is in the same directory as the helpers.js module.&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;// helpers.test.js&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;isNumber&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;require&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;./helpers&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="nx"&gt;test&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;should return true if type of object is a number&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="nx"&gt;expect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;isNumber&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;)).&lt;/span&gt;&lt;span class="nx"&gt;toBe&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;true&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;That is what a basic jest file looks like. We import the module/class/function we want to test, we specify some description for what we expect the test result to be, and then we actually tell Jest what we think the function result will be. Let's break it down a bit.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;test()&lt;/code&gt; is a Jest function that defines a single test to be run. You can have as many &lt;code&gt;test&lt;/code&gt; statements in a single file as you like. It takes two required arguments and an optional third one. The &lt;strong&gt;first argument&lt;/strong&gt; is the test &lt;em&gt;name&lt;/em&gt;. It's customary to use it as a clear description of what is being tested. The &lt;strong&gt;second argument&lt;/strong&gt; is a function where the body of our test lives. This is where we tell Jest what our expectations from the test are. In this case, we &lt;em&gt;expect&lt;/em&gt; the return value from &lt;code&gt;isNumber(5)&lt;/code&gt; to be &lt;code&gt;true&lt;/code&gt;. The &lt;strong&gt;third argument&lt;/strong&gt; is an optional &lt;code&gt;timeout&lt;/code&gt; value in milliseconds. Since tests are usually really fast, we don't expect any singular test to take longer than 5 seconds, which is the default &lt;code&gt;timeout&lt;/code&gt; value.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;expect()&lt;/code&gt; is the function we use to actually test our expectations. We use &lt;code&gt;expect&lt;/code&gt; along with "matcher" functions which asserts certain conditions about a value. In this test we're using the &lt;code&gt;toBe()&lt;/code&gt; matcher, which compares actual values with our expectations. There's a lot of matchers, and I'll only cover a few here, but you can read more about them in the &lt;a href="https://jestjs.io/docs/using-matchers"&gt;Jest matchers section of their documentation&lt;/a&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
&lt;p&gt;📖 NOTE&lt;/p&gt;

&lt;p&gt;You can have as many &lt;code&gt;expect()&lt;/code&gt; statements in a test as you like, there's no limit! It's recommended you use only as many as it makes sense, since you can also write as many &lt;code&gt;test()&lt;/code&gt; statements as you like, and it becomes more readable to organize your &lt;code&gt;expect()&lt;/code&gt; statements with their respective test cases.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Now that we have written our first test, we can run &lt;code&gt;npm run test&lt;/code&gt; and see the magic happen:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;npm run &lt;span class="nb"&gt;test&lt;/span&gt;

&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; testing-javascript-with-jest@1.0.0 &lt;span class="nb"&gt;test&lt;/span&gt;
&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; jest

 PASS  ./helpers.test.js
  ✓ should &lt;span class="k"&gt;return &lt;/span&gt;&lt;span class="nb"&gt;true &lt;/span&gt;&lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="nb"&gt;type &lt;/span&gt;of object is a number &lt;span class="o"&gt;(&lt;/span&gt;2 ms&lt;span class="o"&gt;)&lt;/span&gt;

Test Suites: 1 passed, 1 total
Tests:       1 passed, 1 total
Snapshots:   0 total
Time:        0.279 s, estimated 1 s
Ran all &lt;span class="nb"&gt;test &lt;/span&gt;suites.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;📖 NOTE&lt;/p&gt;

&lt;p&gt;"Test Suites" refers to the number of files jest ran, while "Tests" refers to all the &lt;code&gt;test()&lt;/code&gt; statements found within those files.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Like I said before, Jest automatically looks for and runs all test files in our source code, and it does this &lt;em&gt;really&lt;/em&gt; fast. Congratulations on writing your first unit test!&lt;/p&gt;

&lt;p&gt;Let's write a couple more tests for this function, just so we make sure we cover as many use cases as we can.&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;// helpers.test.js&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;isNumber&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;require&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;./helpers&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="nx"&gt;test&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;should return true if type of object is a number&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="nx"&gt;expect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;isNumber&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)).&lt;/span&gt;&lt;span class="nx"&gt;toBe&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="nx"&gt;expect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;isNumber&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;)).&lt;/span&gt;&lt;span class="nx"&gt;toBe&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="nx"&gt;expect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;isNumber&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;+&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;5&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)).&lt;/span&gt;&lt;span class="nx"&gt;toBe&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;

&lt;span class="nx"&gt;test&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;should return false if type of object is not a number&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="nx"&gt;expect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;isNumber&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="p"&gt;)).&lt;/span&gt;&lt;span class="nx"&gt;toBe&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="nx"&gt;expect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;isNumber&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;number&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)).&lt;/span&gt;&lt;span class="nx"&gt;toBe&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="nx"&gt;expect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;isNumber&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;undefined&lt;/span&gt;&lt;span class="p"&gt;)).&lt;/span&gt;&lt;span class="nx"&gt;toBe&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;false&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 run &lt;code&gt;npm run test&lt;/code&gt; again and...&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;npm run &lt;span class="nb"&gt;test&lt;/span&gt;
...

 PASS  ./helpers.test.js
  ✓ should &lt;span class="k"&gt;return &lt;/span&gt;&lt;span class="nb"&gt;true &lt;/span&gt;&lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="nb"&gt;type &lt;/span&gt;of object is a number &lt;span class="o"&gt;(&lt;/span&gt;2 ms&lt;span class="o"&gt;)&lt;/span&gt;
  ✓ should &lt;span class="k"&gt;return &lt;/span&gt;&lt;span class="nb"&gt;false &lt;/span&gt;&lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="nb"&gt;type &lt;/span&gt;of object is not a number

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

&lt;/div&gt;



&lt;p&gt;Great! Our function seems to be working as intended.&lt;/p&gt;




&lt;h2&gt;
  
  
  Grouping tests under &lt;code&gt;describe()&lt;/code&gt;
&lt;/h2&gt;

&lt;p&gt;We could get away with just writing our tests at the top level like the one we just did. However, we can see that despite seeing our test descriptions and their results, we can't tell by the terminal output what unit we're testing. Let's illustrate this better by writing a second function in &lt;code&gt;helpers.js&lt;/code&gt; and adding its respective tests to &lt;code&gt;helpers.test.js&lt;/code&gt;.&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;// helpers.js&lt;/span&gt;
&lt;span class="p"&gt;...&lt;/span&gt;

&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;isObject&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;possibleObject&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="k"&gt;typeof&lt;/span&gt; &lt;span class="nx"&gt;possibleObject&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;object&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="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="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;isNumber&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;isObject&lt;/span&gt; &lt;span class="p"&gt;};&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// helpers.test.js&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;isNumber&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;isObject&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;require&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;./helpers&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="nx"&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 return true if type of object is "object"&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="nx"&gt;expect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;isObject&lt;/span&gt;&lt;span class="p"&gt;({})).&lt;/span&gt;&lt;span class="nx"&gt;toBe&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="nx"&gt;expect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;isObject&lt;/span&gt;&lt;span class="p"&gt;([])).&lt;/span&gt;&lt;span class="nx"&gt;toBe&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;

&lt;span class="nx"&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 return false if type of object is not "object"&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="nx"&gt;expect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;isObject&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;)).&lt;/span&gt;&lt;span class="nx"&gt;toBe&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="nx"&gt;expect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;isObject&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;object&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)).&lt;/span&gt;&lt;span class="nx"&gt;toBe&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;false&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 run &lt;code&gt;npm run test&lt;/code&gt; again and we get the &lt;em&gt;expected&lt;/em&gt; (ha, get it?) result:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;npm run &lt;span class="nb"&gt;test&lt;/span&gt;

&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; testing-javascript-with-jest@1.0.0 &lt;span class="nb"&gt;test&lt;/span&gt;
&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; jest

 PASS  ./helpers.test.js
  ✓ should &lt;span class="k"&gt;return &lt;/span&gt;&lt;span class="nb"&gt;true &lt;/span&gt;&lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="nb"&gt;type &lt;/span&gt;of object is a number &lt;span class="o"&gt;(&lt;/span&gt;1 ms&lt;span class="o"&gt;)&lt;/span&gt;
  ✓ should &lt;span class="k"&gt;return &lt;/span&gt;&lt;span class="nb"&gt;false &lt;/span&gt;&lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="nb"&gt;type &lt;/span&gt;of object is not a number &lt;span class="o"&gt;(&lt;/span&gt;1 ms&lt;span class="o"&gt;)&lt;/span&gt;
  ✓ should &lt;span class="k"&gt;return &lt;/span&gt;&lt;span class="nb"&gt;true &lt;/span&gt;&lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="nb"&gt;type &lt;/span&gt;of object is &lt;span class="s2"&gt;"object"&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;1 ms&lt;span class="o"&gt;)&lt;/span&gt;
  ✓ should &lt;span class="k"&gt;return &lt;/span&gt;&lt;span class="nb"&gt;false &lt;/span&gt;&lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="nb"&gt;type &lt;/span&gt;of object is not &lt;span class="s2"&gt;"object"&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;1 ms&lt;span class="o"&gt;)&lt;/span&gt;

Test Suites: 1 passed, 1 total
Tests:       4 passed, 4 total
Snapshots:   0 total
Time:        0.204 s, estimated 1 s
Ran all &lt;span class="nb"&gt;test &lt;/span&gt;suites.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Like I said before, while these results are great and we got all the green checkmarks, they're not the most readable, and we don't know which test belongs to what unit. There's a better way to organize our tests so that the output to the terminal is cleaner and easier to read.&lt;/p&gt;

&lt;p&gt;By using &lt;code&gt;describe()&lt;/code&gt;, we can group our tests together under a single block, and therefore, under the same scope -which will become useful later-. To implement the &lt;code&gt;describe()&lt;/code&gt; function on our existing tests, all we gotta do is wrap &lt;code&gt;describe()&lt;/code&gt; statements around a related group of &lt;code&gt;test()&lt;/code&gt; statements.&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;// helpers.test.js&lt;/span&gt;
&lt;span class="p"&gt;...&lt;/span&gt;

&lt;span class="nx"&gt;describe&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;isNumber&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="nx"&gt;test&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;should return true if type of object is a number&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="nx"&gt;expect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;isNumber&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)).&lt;/span&gt;&lt;span class="nx"&gt;toBe&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="nx"&gt;expect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;isNumber&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;)).&lt;/span&gt;&lt;span class="nx"&gt;toBe&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="nx"&gt;expect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;isNumber&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;+&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;5&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)).&lt;/span&gt;&lt;span class="nx"&gt;toBe&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;});&lt;/span&gt;

    &lt;span class="nx"&gt;test&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;should return false if type of object is not a number&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="nx"&gt;expect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;isNumber&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="p"&gt;)).&lt;/span&gt;&lt;span class="nx"&gt;toBe&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="nx"&gt;expect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;isNumber&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;number&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)).&lt;/span&gt;&lt;span class="nx"&gt;toBe&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="nx"&gt;expect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;isNumber&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;undefined&lt;/span&gt;&lt;span class="p"&gt;)).&lt;/span&gt;&lt;span class="nx"&gt;toBe&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;});&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;

&lt;span class="nx"&gt;describe&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;isObject&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="nx"&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 return true if type of object is "object"&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="nx"&gt;expect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;isObject&lt;/span&gt;&lt;span class="p"&gt;({})).&lt;/span&gt;&lt;span class="nx"&gt;toBe&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="nx"&gt;expect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;isObject&lt;/span&gt;&lt;span class="p"&gt;([])).&lt;/span&gt;&lt;span class="nx"&gt;toBe&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;});&lt;/span&gt;

    &lt;span class="nx"&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 return false if type of object is not "object"&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="nx"&gt;expect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;isObject&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;)).&lt;/span&gt;&lt;span class="nx"&gt;toBe&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="nx"&gt;expect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;isObject&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;object&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)).&lt;/span&gt;&lt;span class="nx"&gt;toBe&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="p"&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;This time, when we run &lt;code&gt;npm run test&lt;/code&gt;, we'll see groups of tests organized under the same name.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;npm run &lt;span class="nb"&gt;test&lt;/span&gt;
...

 PASS  ./helpers.test.js
  isNumber
    ✓ should &lt;span class="k"&gt;return &lt;/span&gt;&lt;span class="nb"&gt;true &lt;/span&gt;&lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="nb"&gt;type &lt;/span&gt;of object is a number &lt;span class="o"&gt;(&lt;/span&gt;2 ms&lt;span class="o"&gt;)&lt;/span&gt;
    ✓ should &lt;span class="k"&gt;return &lt;/span&gt;&lt;span class="nb"&gt;false &lt;/span&gt;&lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="nb"&gt;type &lt;/span&gt;of object is not a number &lt;span class="o"&gt;(&lt;/span&gt;1 ms&lt;span class="o"&gt;)&lt;/span&gt;
  isObject
    ✓ should &lt;span class="k"&gt;return &lt;/span&gt;&lt;span class="nb"&gt;true &lt;/span&gt;&lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="nb"&gt;type &lt;/span&gt;of object is &lt;span class="s2"&gt;"object"&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;1 ms&lt;span class="o"&gt;)&lt;/span&gt;
    ✓ should &lt;span class="k"&gt;return &lt;/span&gt;&lt;span class="nb"&gt;false &lt;/span&gt;&lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="nb"&gt;type &lt;/span&gt;of object is not &lt;span class="s2"&gt;"object"&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;1 ms&lt;span class="o"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Both the terminal output and the written code become much more readable when grouping tests together, and for reasons that will become important in future articles, it also groups related tests under the same scope.&lt;/p&gt;

&lt;h2&gt;
  
  
  Running multiple test cases using Jest Each
&lt;/h2&gt;

&lt;p&gt;As of Jest version 23, we've been able to use the &lt;code&gt;each&lt;/code&gt; method on both the &lt;code&gt;test&lt;/code&gt; and &lt;code&gt;describe&lt;/code&gt; functions. &lt;code&gt;each&lt;/code&gt; allows us to run the same test multiple times using values defined in a "table column". The table can be both array types and template literals using &lt;a href="https://spockframework.org/spock/docs/1.1/data_driven_testing.html#data-tables"&gt;Spock Data Tables&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;We can simplify our tests with multiple &lt;code&gt;expect&lt;/code&gt; statements that contain different values 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="c1"&gt;//helpers.test.js&lt;/span&gt;
&lt;span class="p"&gt;...&lt;/span&gt;

&lt;span class="nx"&gt;describe&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;isNumber&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="c1"&gt;// Instead of this:&lt;/span&gt;
    &lt;span class="c1"&gt;// test("should return true if type of object is a number", () =&amp;gt; {&lt;/span&gt;
    &lt;span class="c1"&gt;//     expect(isNumber(0)).toBe(true);&lt;/span&gt;
    &lt;span class="c1"&gt;//     expect(isNumber(5)).toBe(true);&lt;/span&gt;
    &lt;span class="c1"&gt;//     expect(isNumber(+"5")).toBe(true);&lt;/span&gt;
    &lt;span class="c1"&gt;// });&lt;/span&gt;


    &lt;span class="c1"&gt;// We use this:&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;numbers&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;5&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
    &lt;span class="nx"&gt;test&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;each&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;numbers&lt;/span&gt;&lt;span class="p"&gt;)(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;should return true since type of %j is a number&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; 
        &lt;span class="nx"&gt;numberToTest&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="nx"&gt;expect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;isNumber&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;numberToTest&lt;/span&gt;&lt;span class="p"&gt;)).&lt;/span&gt;&lt;span class="nx"&gt;toBe&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;true&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;It's a weird syntax, I know, but it makes it so much easier to test a large number of tests with fewer lines. In this case, we can just keep adding values to the &lt;code&gt;numbers&lt;/code&gt; array and keep checking to see if they all return &lt;code&gt;true&lt;/code&gt; without adding extra &lt;code&gt;expect()&lt;/code&gt; statements.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;📖 NOTE&lt;/p&gt;

&lt;p&gt;%j is a &lt;a href="https://nodejs.org/api/util.html#util_util_format_format_args"&gt;&lt;code&gt;printf formatting specifier&lt;/code&gt;&lt;/a&gt;. When we run jest, each test will have a unique name with its specific value injected in the string by the parameters passed into it by the &lt;code&gt;table&lt;/code&gt; argument.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Let's do this for all our tests:&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;// helpers.test.js&lt;/span&gt;
&lt;span class="p"&gt;...&lt;/span&gt;

&lt;span class="nx"&gt;describe&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;isNumber&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;numbers&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;5&lt;/span&gt;&lt;span class="dl"&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;notNumbers&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;number&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kc"&gt;undefined&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;

    &lt;span class="nx"&gt;test&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;each&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;numbers&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 return true since type of %j is "number"&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="nx"&gt;possibleNumber&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="nx"&gt;expect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;isNumber&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;possibleNumber&lt;/span&gt;&lt;span class="p"&gt;)).&lt;/span&gt;&lt;span class="nx"&gt;toBe&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="p"&gt;});&lt;/span&gt;

    &lt;span class="nx"&gt;test&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;each&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;notNumbers&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 return false since type of %j is not "number"&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="nx"&gt;possibleNumber&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="nx"&gt;expect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;isNumber&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;possibleNumber&lt;/span&gt;&lt;span class="p"&gt;)).&lt;/span&gt;&lt;span class="nx"&gt;toBe&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="p"&gt;});&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;

&lt;span class="nx"&gt;describe&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;isObject&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;objects&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&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;notObjects&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;object&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;

    &lt;span class="nx"&gt;test&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;each&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;objects&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 return true since type of %j is "object"&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="nx"&gt;possibleObject&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="nx"&gt;expect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;isObject&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;possibleObject&lt;/span&gt;&lt;span class="p"&gt;)).&lt;/span&gt;&lt;span class="nx"&gt;toBe&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
            &lt;span class="nx"&gt;expect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;isObject&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;possibleObject&lt;/span&gt;&lt;span class="p"&gt;)).&lt;/span&gt;&lt;span class="nx"&gt;toBe&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="p"&gt;});&lt;/span&gt;

    &lt;span class="nx"&gt;test&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;each&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;notObjects&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 return false since type of %j is not "object"&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="nx"&gt;possibleObject&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="nx"&gt;expect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;isObject&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;possibleObject&lt;/span&gt;&lt;span class="p"&gt;)).&lt;/span&gt;&lt;span class="nx"&gt;toBe&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
            &lt;span class="nx"&gt;expect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;isObject&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;possibleObject&lt;/span&gt;&lt;span class="p"&gt;)).&lt;/span&gt;&lt;span class="nx"&gt;toBe&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="p"&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;Now not only do we save unnecessary lines of code, but our tests all have unique names when prined to the terminal:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;npm run &lt;span class="nb"&gt;test&lt;/span&gt;
...

 PASS  ./helpers.test.js
  isNumber
    ✓ should &lt;span class="k"&gt;return &lt;/span&gt;&lt;span class="nb"&gt;true &lt;/span&gt;since &lt;span class="nb"&gt;type &lt;/span&gt;of 0 is &lt;span class="s2"&gt;"number"&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;1 ms&lt;span class="o"&gt;)&lt;/span&gt;
    ✓ should &lt;span class="k"&gt;return &lt;/span&gt;&lt;span class="nb"&gt;true &lt;/span&gt;since &lt;span class="nb"&gt;type &lt;/span&gt;of 5 is &lt;span class="s2"&gt;"number"&lt;/span&gt;
    ✓ should &lt;span class="k"&gt;return &lt;/span&gt;&lt;span class="nb"&gt;true &lt;/span&gt;since &lt;span class="nb"&gt;type &lt;/span&gt;of 5 is &lt;span class="s2"&gt;"number"&lt;/span&gt;
    ✓ should &lt;span class="k"&gt;return &lt;/span&gt;&lt;span class="nb"&gt;false &lt;/span&gt;since &lt;span class="nb"&gt;type &lt;/span&gt;of null is not &lt;span class="s2"&gt;"number"&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;1 ms&lt;span class="o"&gt;)&lt;/span&gt;
    ✓ should &lt;span class="k"&gt;return &lt;/span&gt;&lt;span class="nb"&gt;false &lt;/span&gt;since &lt;span class="nb"&gt;type &lt;/span&gt;of &lt;span class="s2"&gt;"number"&lt;/span&gt; is not &lt;span class="s2"&gt;"number"&lt;/span&gt;
    ✓ should &lt;span class="k"&gt;return &lt;/span&gt;&lt;span class="nb"&gt;false &lt;/span&gt;since &lt;span class="nb"&gt;type &lt;/span&gt;of undefined is not &lt;span class="s2"&gt;"number"&lt;/span&gt;
  isObject
    ✓ should &lt;span class="k"&gt;return &lt;/span&gt;&lt;span class="nb"&gt;true &lt;/span&gt;since &lt;span class="nb"&gt;type &lt;/span&gt;of &lt;span class="o"&gt;{}&lt;/span&gt; is &lt;span class="s2"&gt;"object"&lt;/span&gt;
    ✓ should &lt;span class="k"&gt;return &lt;/span&gt;&lt;span class="nb"&gt;true &lt;/span&gt;since &lt;span class="nb"&gt;type &lt;/span&gt;of &lt;span class="o"&gt;[]&lt;/span&gt; is &lt;span class="s2"&gt;"object"&lt;/span&gt;
    ✓ should &lt;span class="k"&gt;return &lt;/span&gt;&lt;span class="nb"&gt;false &lt;/span&gt;since &lt;span class="nb"&gt;type &lt;/span&gt;of 5 is not &lt;span class="s2"&gt;"object"&lt;/span&gt;
    ✓ should &lt;span class="k"&gt;return &lt;/span&gt;&lt;span class="nb"&gt;false &lt;/span&gt;since &lt;span class="nb"&gt;type &lt;/span&gt;of &lt;span class="s2"&gt;"object"&lt;/span&gt; is not &lt;span class="s2"&gt;"object"&lt;/span&gt;

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

&lt;/div&gt;






&lt;h2&gt;
  
  
  Summary
&lt;/h2&gt;

&lt;p&gt;This is an introductory article, and as such, we learnt the very basics of what testing is, the most common types of testing in JavaScript, and how to do test our units using the testing framework Jest. We know now that to test our code we use the &lt;code&gt;test()&lt;/code&gt; and &lt;code&gt;expect()&lt;/code&gt; functions together. We also know that we can group tests that share similar logic under the same scope by using the &lt;code&gt;describe()&lt;/code&gt; function, and we can reuse the same test under different test cases with the &lt;code&gt;each&lt;/code&gt; method.&lt;/p&gt;

&lt;p&gt;Thank you for reading, and see you next time!&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;📖 NOTE&lt;/p&gt;

&lt;p&gt;This post was first published over at &lt;a href="https://personal-website-rafavls.vercel.app/posts/testing-javascript-with-jest-unit-testing"&gt;my website&lt;/a&gt;, go check it out if you wanna read more content like this.&lt;/p&gt;
&lt;/blockquote&gt;

</description>
      <category>webdev</category>
      <category>jest</category>
      <category>testing</category>
      <category>javascript</category>
    </item>
    <item>
      <title>Creating a weather app with Reactjs - Part 3</title>
      <dc:creator>Rafael</dc:creator>
      <pubDate>Fri, 05 Feb 2021 00:25:07 +0000</pubDate>
      <link>https://dev.to/rafavls/creating-a-weather-app-with-reactjs-part-3-2o6m</link>
      <guid>https://dev.to/rafavls/creating-a-weather-app-with-reactjs-part-3-2o6m</guid>
      <description>&lt;h2&gt;
  
  
  Using our data
&lt;/h2&gt;

&lt;p&gt;Alright! Now we have visible data that we can work with. If we go to our DevTools =&amp;gt; Network tab, and we look for our fetch calls (one starts with &lt;strong&gt;"onecall"&lt;/strong&gt; and the other one with &lt;strong&gt;"json?latlng"&lt;/strong&gt;, we can see what the response looks like. That's our data!&lt;br&gt;
For Google Map's Geolocation API, we can see that they give us a &lt;em&gt;lot&lt;/em&gt; of information. Which is good, but we don't really need all of that for this app. So, since I only really care about the city's name and the state's "short name" (i.e. California = CA), we can refactor our fetchData function's final lines like so: &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%2Fi%2Fd26tn7m1r6u7scyr40v9.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%2Fi%2Fd26tn7m1r6u7scyr40v9.png" alt="fetchData function refactored to filter Google Map's Geolocation Response"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Displaying our data
&lt;/h2&gt;

&lt;p&gt;Great! Now our data is more managable. Now, let's create a simple section that displays current temperature, humidity, wind speed, city name and the state's short name.&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%2Fi%2Fv85jj1fmfv4ksoj58n3j.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%2Fi%2Fv85jj1fmfv4ksoj58n3j.png" alt="JSX code for displaying the response data"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Awesome! Now we can see the info on the page. We have to add a couple checks before trying to display the info, since it takes a bit for our app to get the data responses. This way, we won't get errors when we first render our app. These checks will also account for the case in which the user denies location access. So, whatever we put in that last part of the tertiary statement will display in case of any errors.&lt;/p&gt;

&lt;p&gt;Great, we can see our data, but it's kind of ugly, isn't it? Let's fix that.&lt;/p&gt;

&lt;h2&gt;
  
  
  Styling using CSS modules
&lt;/h2&gt;

&lt;p&gt;To use CSS modules in React, we'll create a new directory in our /src folder called /styles. Here, we'll have our stylesheets for all our React components (granted, some people prefer to have their css modules in the same directory as the component, but for this case I think this works fine). Let's also create a folder called /components, which we'll populate soon enough.&lt;/p&gt;

&lt;p&gt;Before we start using css modules, let's convert our displayed data into it's own component, this will keep our App.js file cleaner and our files easier to handle. So, inside of /components, create a file named "CurrentData.js". Inside our /styles directory, create two css module files: one for App.js, and another for CurrentData.js. The App.module.css file will be our global stylesheet, where we'll reset the margins, define a font for the entire app, define css variables, etc. This is what those files contain.&lt;/p&gt;

&lt;p&gt;App.js return statement&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%2Fi%2Fx9be8gksq72dq5viedir.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%2Fi%2Fx9be8gksq72dq5viedir.png" alt="App.js return statement code"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;CurrentData.js, inside the /components dir&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%2Fi%2F3313n3inmvkajoeai9pw.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%2Fi%2F3313n3inmvkajoeai9pw.png" alt="CurrentData.js code, displaying weather data"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;CurrentData.module.css, inside the /styles dir&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%2Fi%2F78g42bcbn4ysan0fqfdj.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%2Fi%2F78g42bcbn4ysan0fqfdj.png" alt="The stylesheet for the CurrentData.js file"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;App.module.css, inside the /styles dir&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%2Fi%2Fb1gjs4pdhnma3iajfvda.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%2Fi%2Fb1gjs4pdhnma3iajfvda.png" alt="App.module.css stylesheet"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Our webpage in its current state!&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%2Fi%2Foygoz2zkxcqbypwjzm88.jpeg" 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%2Fi%2Foygoz2zkxcqbypwjzm88.jpeg" alt="The current look of the webpage"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Now we're coding!&lt;/strong&gt; Don't worry, it looks like a lot but it really isn't. Also, I know the app isn't the most beautifully designed out there, but it serves its teaching purposes.&lt;/p&gt;

&lt;p&gt;So, we separated the extense code into the CurrentData component (I called it that because we'll implement daily forecast later). In this component we make use of the data that we retrieved before, and we display it to the user. The unixToDate() function lets us convert the unix timestamp that we get from the One Call API into readable human time format. And that's it! Our app works! I'll be adding a couple more components over the next posts in the series, but this is the skeleton of it. You've made a weather app that works!&lt;/p&gt;

</description>
      <category>react</category>
      <category>hooks</category>
      <category>webdev</category>
      <category>fetch</category>
    </item>
    <item>
      <title>Creating a weather app with Reactjs - Part 2</title>
      <dc:creator>Rafael</dc:creator>
      <pubDate>Wed, 03 Feb 2021 18:46:38 +0000</pubDate>
      <link>https://dev.to/rafavls/creating-a-weather-app-with-reactjs-part-2-5gi3</link>
      <guid>https://dev.to/rafavls/creating-a-weather-app-with-reactjs-part-2-5gi3</guid>
      <description>&lt;h2&gt;
  
  
  Fetching and using data.
&lt;/h2&gt;

&lt;p&gt;Now that we have the user's coordinates, we can make a fetch call to the &lt;a href="https://openweathermap.org/api/one-call-api" rel="noopener noreferrer"&gt;Open Wather Map's One Call API&lt;/a&gt;; which we can use to get very useful data, like:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Current weather&lt;/li&gt;
&lt;li&gt;Hourly forecast for 48 hours&lt;/li&gt;
&lt;li&gt;Daily forecast for 7 days&lt;/li&gt;
&lt;li&gt;And more...&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;First things first. You'll need to create an account with them, which is free, and generate an API key for the One Call API, which is also free!&lt;br&gt;
Once you do that, you should have an API key that looks something like this: "jadi1923mdas012jda...". Should be around 32 characters long. Now we're ready to implement our fetch call!&lt;/p&gt;

&lt;p&gt;If we take a look at the One Call API Documentation, we'll see that to make an API call, we use an address like this:&lt;br&gt;
"&lt;a href="https://api.openweathermap.org/data/2.5/onecall?lat=%7Blat%7D&amp;amp;lon=%7Blon%7D&amp;amp;exclude=%7Bpart%7D&amp;amp;appid=%7BAPIKey%7D" rel="noopener noreferrer"&gt;https://api.openweathermap.org/data/2.5/onecall?lat={lat}&amp;amp;lon={lon}&amp;amp;exclude={part}&amp;amp;appid={APIKey}&lt;/a&gt;".&lt;/p&gt;

&lt;p&gt;The exclude parameter lets you, well... &lt;em&gt;exclude&lt;/em&gt; weather data from the API response. For this app, I don't really care about the minutely or alerts parts of the data, so I'm gonna exclude those like this: "...&amp;amp;exclude=minutely,alerts...".&lt;/p&gt;

&lt;p&gt;Since we also want to display the name of the city, we're gonna use Google's &lt;a href="https://developers.google.com/maps/documentation/geocoding/overview?csw=1#ReverseGeocoding" rel="noopener noreferrer"&gt;Reverse Geocoding API&lt;/a&gt;, which takes geographical coordinates, and returns an array of data about those coordinates. To access this API you again need to have a google account and generate an API Key, which is free. This is a process that unfortunately falls out of the scope of this series, but you can learn how to do it &lt;a href="https://developers.google.com/maps/gmp-get-started" rel="noopener noreferrer"&gt;here&lt;/a&gt;. The reverse geocoding API url looks like this: "&lt;a href="https://maps.googleapis.com/maps/api/geocode/json?latlng=%7Blat%7D,%7Blong%7D&amp;amp;key=%7BAPIKey%7D" rel="noopener noreferrer"&gt;https://maps.googleapis.com/maps/api/geocode/json?latlng={lat},{long}&amp;amp;key={APIKey}&lt;/a&gt;"&lt;br&gt;
Once you have both your keys, it's finally time to code!&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;Important&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;If you're using git/GitHub, it's important that you don't put sensitive information out there, like your API Keys. To work around this, put your keys in a separate .js file that you can access from your other files. Then, add this file to .gitignore. This way, no one but you should be able to see your keys.&lt;/p&gt;

&lt;p&gt;Since we're gonna need all of this data at initial render, we'll do our fetch calls inside a useEffect() hook. We'll use an additional one to the one we already have, since they do different things. It looks 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%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fsw6bblf8yle3cwbj6ucc.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%2Fi%2Fsw6bblf8yle3cwbj6ucc.png" alt="The useEffect() that fetches data from APIs"&gt;&lt;/a&gt;&lt;br&gt;
As you can see, all we're doing is requesting data from both APIs. To prevent errors, we check first to see if the currentLocation object contains a latitude key. If it does, we'll run our functions. At the end, we tell react to run this hook everytime our currentLocation state updates. If you go to your React page now, and look at the console, you'll see two objects with the data that we requested. Good job!&lt;/p&gt;

&lt;p&gt;Since we're using very similar functions both times, we should refactor this into a single function, and save ourselves some space.&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%2Fi%2Fzrabonzpzfho0p3o6yqx.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%2Fi%2Fzrabonzpzfho0p3o6yqx.png" alt="Refactored useEffect() to fetch data"&gt;&lt;/a&gt;&lt;br&gt;
A bit cleaner, huh?&lt;/p&gt;

&lt;p&gt;That data is not useful to us in the console tho, we should store it in state so we can display it. Let's use two new useState() hooks for 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%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fp2qjxdcr7zkarcfw93ky.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%2Fi%2Fp2qjxdcr7zkarcfw93ky.png" alt="Two useState() hooks for storing city and forecast data"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;And instead of displaying the data to the console, let's set it to these new state variables inside our fetchData function.&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%2Fi%2Fl97ekmogoy4s4w6of6li.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%2Fi%2Fl97ekmogoy4s4w6of6li.png" alt="Using functions to set state inside of useEffect()"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Done! Our data is now saved in state and we can access it later whenever we need it.&lt;/p&gt;

</description>
      <category>react</category>
      <category>hooks</category>
      <category>webdev</category>
      <category>fetch</category>
    </item>
    <item>
      <title>Creating a weather app with Reactjs - Part 1</title>
      <dc:creator>Rafael</dc:creator>
      <pubDate>Wed, 03 Feb 2021 03:09:32 +0000</pubDate>
      <link>https://dev.to/rafavls/creating-a-weather-app-with-reactjs-part-1-5a</link>
      <guid>https://dev.to/rafavls/creating-a-weather-app-with-reactjs-part-1-5a</guid>
      <description>&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;Hello! In this series, I'll show you how I went about creating a weather forecast app entirely with Reactjs. The app uses &lt;a href="https://reactjs.org/docs/hooks-intro.html" rel="noopener noreferrer"&gt;React Hooks&lt;/a&gt;, API calls from &lt;a href="https://openweathermap.org/" rel="noopener noreferrer"&gt;OpenWeatherMap&lt;/a&gt; &amp;amp; &lt;a href="https://developers.google.com/maps/documentation/geocoding/overview" rel="noopener noreferrer"&gt;Google Geocoding Services&lt;/a&gt;, and some CSS modules to make it all responsive (and look nice too, of course). Let's begin!&lt;/p&gt;

&lt;h2&gt;
  
  
  Pre-requisites
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Node &amp;gt;= 8.10&lt;/li&gt;
&lt;li&gt;npm &amp;gt;= 5.6&lt;/li&gt;
&lt;li&gt;A text editor (VSCode recommended).&lt;/li&gt;
&lt;li&gt;Some previous knowledge of Reactjs and React Hooks.&lt;/li&gt;
&lt;li&gt;Some familiarity with using a command-line tool.&lt;/li&gt;
&lt;li&gt;Usage of git/github is optional but recommended.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Starting out
&lt;/h2&gt;

&lt;h3&gt;
  
  
  1. Creating the React folder
&lt;/h3&gt;

&lt;p&gt;To begin, open up a new terminal and initialize a new react-app in a directory called react-weather with the create-react-app command:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npx create-react-app react-weather
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This will create a template Reactjs app that we can modify to make it our own.&lt;/p&gt;

&lt;h3&gt;
  
  
  2. Cleaning up our template
&lt;/h3&gt;

&lt;p&gt;We won't use all files or sections of code created by the template, so let's take some time to clean up our workspace. We can go ahead and delete all of these files:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;README.md&lt;/li&gt;
&lt;li&gt;All files in the &lt;strong&gt;/src&lt;/strong&gt; directory&lt;/li&gt;
&lt;li&gt;All files in the &lt;strong&gt;/public&lt;/strong&gt; directory but &lt;strong&gt;index.html&lt;/strong&gt;, &lt;strong&gt;manifest.json&lt;/strong&gt; and robots.txt&lt;/li&gt;
&lt;li&gt;We can delete all the commented lines in &lt;strong&gt;index.html&lt;/strong&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  3. Create initial component
&lt;/h3&gt;

&lt;p&gt;Now that our src folder is empty, nothing will be rendered to the webpage if we were to run our app. To create our first component, and see it in action, we're gonna need two files: &lt;strong&gt;index.js&lt;/strong&gt; and &lt;strong&gt;App.js&lt;/strong&gt;. Each one will look like this for now:&lt;/p&gt;

&lt;p&gt;index.js&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%2Fi%2F2b11nms1znmgpfhp7rkq.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%2Fi%2F2b11nms1znmgpfhp7rkq.png" alt="Initial index.js code"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;App.js&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%2Fi%2F3x3m43ily01t6vmw8m68.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%2Fi%2F3x3m43ily01t6vmw8m68.png" alt="Initial App.js code"&gt;&lt;/a&gt;&lt;br&gt;
Now, we can finally start creating our weather forecast app!&lt;/p&gt;

&lt;h2&gt;
  
  
  Working with hooks
&lt;/h2&gt;

&lt;p&gt;Since this is a weather forecast app, I'd like to give the user their initial location's forecast, and after that, they can choose to look for a different city's data. To do that, we'll use the &lt;a href="https://reactjs.org/docs/hooks-effect.html" rel="noopener noreferrer"&gt;useEffect()&lt;/a&gt; hook, the &lt;a href="https://reactjs.org/docs/hooks-state.html" rel="noopener noreferrer"&gt;useState()&lt;/a&gt; hook and the &lt;a href="https://developer.mozilla.org/es/docs/Web/API/Geolocation/getCurrentPosition" rel="noopener noreferrer"&gt;getCurrentPosition()&lt;/a&gt; function from the Geolocation API.&lt;/p&gt;

&lt;p&gt;So, when the user launches the app, we want to get their current geographical coordinates and store them in state as an object. This is what that looks like:&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%2Fi%2F73lqcmqv5lj2wyka4mqj.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%2Fi%2F73lqcmqv5lj2wyka4mqj.png" alt="Code for getting current location and sending it to the console"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Woah! Where did all that code come from? Let me explain.&lt;/p&gt;

&lt;h3&gt;
  
  
  useState()
&lt;/h3&gt;

&lt;p&gt;The useState() hook sets state for a React component. It returns two values that we can get by declaring an array. First, a default value for the state variable that we want to set (an empty object in this case). Second, a function that we can call later to set the value of this state variable.&lt;/p&gt;

&lt;h3&gt;
  
  
  useEffect()
&lt;/h3&gt;

&lt;p&gt;useEffect() is a tricky but powerful hook. It runs after initial render and also after every update. By specifying an empty array as a second argument, we tell this hook to only run on initial render. If we populate that array with one or more state variables, the hook will run after any of those variables changes. &lt;br&gt;
So, this hook will run as the page renders, it will ask the user for permission to access their location, and will save that location data in state.&lt;/p&gt;

&lt;p&gt;Now, as soon as the app launches, it'll ask the user for their location. If the user allows geolocation, we'll store their coordinates as an object in state. We'll handle the case in which the user blocks location in the future, for now, we'll assume that users always allow us to know their current location.&lt;/p&gt;

&lt;p&gt;You can see that we have a button for displaying the geological information to the devtools console. Try it!&lt;/p&gt;

</description>
      <category>react</category>
      <category>hooks</category>
      <category>webdev</category>
      <category>fetch</category>
    </item>
  </channel>
</rss>
