<?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: Sir Muel I 🇳🇬</title>
    <description>The latest articles on DEV Community by Sir Muel I 🇳🇬 (@imolorhe).</description>
    <link>https://dev.to/imolorhe</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%2F141410%2F73c56ad8-7099-4d37-b152-709557f8a841.jpg</url>
      <title>DEV Community: Sir Muel I 🇳🇬</title>
      <link>https://dev.to/imolorhe</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/imolorhe"/>
    <language>en</language>
    <item>
      <title>Unit testing JavaScript Applications - Part 1</title>
      <dc:creator>Sir Muel I 🇳🇬</dc:creator>
      <pubDate>Tue, 26 May 2020 23:31:09 +0000</pubDate>
      <link>https://dev.to/imolorhe/unit-testing-javascript-applications-part-1-1i9n</link>
      <guid>https://dev.to/imolorhe/unit-testing-javascript-applications-part-1-1i9n</guid>
      <description>&lt;blockquote&gt;
&lt;p&gt;Read the full post at: &lt;a href="https://www.xkoji.dev/blog/unit-testing-javascript-applications-part-1/"&gt;https://www.xkoji.dev/blog/unit-testing-javascript-applications-part-1/&lt;/a&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;As developers, a lot of the time we find writing unit tests for our application to be a daunting task, which we would prefer not to do. Considering that writing tests doesn't really have any business value either, it becomes even easier to postpone writing the tests for your application till much later. However we know that writing tests have a number of benefits that become more apparent as the project grows in complexity and the team working on the product increases in size, or goes through an organizational restructuring. In this series, we would be looking at writing unit tests for JavaScript applications.&lt;/p&gt;

&lt;h2&gt;
  
  
  What is Unit Testing?
&lt;/h2&gt;

&lt;p&gt;Unit testing refers to writing tests that verify that a &lt;em&gt;piece (unit) of code&lt;/em&gt; behaves as the author intended, giving a set of inputs in all conceivable conditions. Unit tests always tests the piece of code in &lt;em&gt;isolation&lt;/em&gt;, without any external factors or dependencies influencing the code. When thinking about unit tests, it helps to think about the unit of code in terms of a function. &lt;/p&gt;

&lt;p&gt;In its simplest form, a function accepts a set of &lt;em&gt;inputs&lt;/em&gt;, performs some &lt;em&gt;computation&lt;/em&gt; based on the inputs, and returns some &lt;em&gt;output&lt;/em&gt;. Let's create a simple function called &lt;code&gt;sum()&lt;/code&gt; which takes two numbers and returns their sum as an output.&lt;br&gt;
&lt;/p&gt;

&lt;div class="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;sum&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;a&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;b&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;a&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;b&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;output&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;sum&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="c1"&gt;// expected output -&amp;gt; 5&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;From my perspective as a user of this function, I don't really care &lt;em&gt;how&lt;/em&gt; the &lt;code&gt;sum()&lt;/code&gt; function decides to compute the output. All I care about is that: if I provide 2 and 3 as the inputs, &lt;code&gt;sum()&lt;/code&gt; should return 5 always, no matter how many times I run it. The &lt;code&gt;sum&lt;/code&gt; function could have been written a different way to compute the output, but as long as the output is the same, then the function still does what it was intended to do.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// a random contrived implementation of sum&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;sum&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;a&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;b&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;let&lt;/span&gt; &lt;span class="nx"&gt;output&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;a&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;isAdd&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;b&lt;/span&gt; &lt;span class="o"&gt;&amp;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;b&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;Math&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;abs&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;b&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

  &lt;span class="k"&gt;while&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;b&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;b&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;isAdd&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nx"&gt;output&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="k"&gt;else&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nx"&gt;output&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="nx"&gt;b&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="k"&gt;else&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;isAdd&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nx"&gt;output&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="nx"&gt;b&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
      &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nx"&gt;output&lt;/span&gt; &lt;span class="o"&gt;-=&lt;/span&gt; &lt;span class="nx"&gt;b&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
      &lt;span class="p"&gt;}&lt;/span&gt;
      &lt;span class="nx"&gt;b&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&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="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;output&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;output&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;sum&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="c1"&gt;// expected output -&amp;gt; 5&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Unit testing comes with a number of benefits as well:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;you can change (refactor) your code confidently when you have solid unit tests that cover all the requirements in place.&lt;/li&gt;
&lt;li&gt;you end up writing better code as a result of writing unit tests. Some approaches to coding are very problematic when you need to write unit tests. In order to write tests, you need to write modular code with clear explicit dependencies (more on this later).&lt;/li&gt;
&lt;li&gt;your code is more reliable as it has tests covering the requirements.&lt;/li&gt;
&lt;li&gt;the test cases provide some form of living documentation about your code. Test cases describe the various requirements your code handles, and gives examples of how to use your code in form of the tests.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Pure Functions
&lt;/h2&gt;

&lt;p&gt;When a function always returns the same output everytime for a given set of inputs, and it doesn't change anything else outside the scope of the input and output, then that function is called a &lt;a href="https://www.wikiwand.com/en/Pure_function"&gt;pure function&lt;/a&gt;. However if the function changes anything else (like mutating a referenced variable, performing I/O operations e.g. saving file to disk, making a network request, modifying the DOM), the function is no longer pure. The reason why this concept is important is because it is easy to predict the behavior of a pure function when using it than an impure function. Also it is very easy to test pure functions without much complexity or knowledge of the internal workings of the function. Let's write a &lt;code&gt;sum()&lt;/code&gt; function that is not pure to compare with our previous versions.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// Already loaded https://cdn.jsdelivr.net/npm/add@latest/index.min.js on the page&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;sum&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;a&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;b&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="k"&gt;return&lt;/span&gt; &lt;span class="nb"&gt;window&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;add&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt; &lt;span class="nx"&gt;a&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;b&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;output&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;sum&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="c1"&gt;// expected output -&amp;gt; 5&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;This function still returns the correct output, but it relies on an external function (&lt;code&gt;window.add()&lt;/code&gt;) existing for it to function. The function no longer depends on only the input parameters, but it also depends on the existence of an external factor, and that the external factor actually returns the right output.&lt;/p&gt;

&lt;p&gt;If we want to unit test this function, remember we need to test the function in isolation. When running a test for the &lt;code&gt;sum()&lt;/code&gt; function, we don't want to be executing code in the &lt;code&gt;window.add()&lt;/code&gt; function. We only want to execute and test the code in the unit of code we are testing, which is the &lt;code&gt;sum()&lt;/code&gt; function in this case. For us to write the unit test, we would need to &lt;em&gt;mock&lt;/em&gt; the &lt;code&gt;window.add()&lt;/code&gt; dependency.&lt;/p&gt;

&lt;h2&gt;
  
  
  Mocking Dependencies
&lt;/h2&gt;

&lt;p&gt;Mocking simply refers to replacing the actual implementation of a piece of code with a fake one that resembles the original one, and behaves like the original one. This is usually done to avoid executing the actual piece of code while trying to test another piece of code. In the example above, we want to test the &lt;code&gt;sum()&lt;/code&gt; function without executing code in the &lt;code&gt;window.add()&lt;/code&gt; function. This adds a major complexity to the unit testing process because you would need to rely on specialized tools and testing frameworks to be able to mock dependencies easily. This can be one of the most painful parts about writing unit tests, if the code structure for your piece of code depends on several external factors, especially if those external factors aren't passed into the function as input parameters, but instead are accessed directly from a global scope from inside the piece of code.&lt;/p&gt;

&lt;p&gt;In the impure &lt;code&gt;sum()&lt;/code&gt; function example, the function access &lt;code&gt;add()&lt;/code&gt; directly from the global &lt;code&gt;window&lt;/code&gt; object. Like we mentioned above, this increases the complexity of the tests. A way to make it easier to mock the dependency would be by passing the dependencies as input parameters as well before using them.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// Already loaded https://cdn.jsdelivr.net/npm/add@latest/index.min.js on the page&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;sum&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;windowObj&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;a&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;b&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="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;windowObj&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;add&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt; &lt;span class="nx"&gt;a&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;b&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;output&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;sum&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;window&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="c1"&gt;// expected output -&amp;gt; 5&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Here, we are passing the &lt;code&gt;window&lt;/code&gt; object as one of the parameters of the &lt;code&gt;sum()&lt;/code&gt; function. Now it is easier to mock the &lt;code&gt;window&lt;/code&gt; object, by just creating a fake object that behaves the same way as the &lt;code&gt;window&lt;/code&gt; object.&lt;br&gt;
&lt;/p&gt;

&lt;div class="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;sum&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;windowObj&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;a&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;b&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="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;windowObj&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;add&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt; &lt;span class="nx"&gt;a&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;b&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;w&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;add&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;arr&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;arr&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="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&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="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;output&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;sum&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;w&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="c1"&gt;// expected output -&amp;gt; 5&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;As you can see, we created a fake object that also has an &lt;code&gt;add&lt;/code&gt; function. Now we have easily mocked the &lt;code&gt;window&lt;/code&gt; object. Also we were able to make the &lt;code&gt;sum()&lt;/code&gt; function pure again, since it only depends on its inputs and doesn't modify anything outside of itself. Of course if the &lt;code&gt;add()&lt;/code&gt; function used is an impure function, that would also make the &lt;code&gt;sum()&lt;/code&gt; function impure, since purity of a function is transitive.&lt;/p&gt;

&lt;h2&gt;
  
  
  Whitebox vs Blackbox Testing
&lt;/h2&gt;

&lt;p&gt;Generally speaking, there are two categories of testing: whitebox and blackbox testing. Whitebox testing refers to tests that depend on the tester having knowledge about the way the piece of code being tested works internally. Blackbox testing is the opposite. The person writing the tests can be unaware about the way the piece of software functions internally. &lt;/p&gt;

&lt;p&gt;Unit testing is an example of whitebox testing and is usually written by the author of the code. As the author, you are usually aware of all the conditions, and logical flows of execution within the code, and so you are able to write tests with all possible combinations of inputs to test all the execution flows. Remember the definition of unit testing: &lt;em&gt;writing tests that verify that a piece (unit) of code behaves as the author intended, giving a set of inputs in &lt;strong&gt;all conceivable&lt;/strong&gt; conditions&lt;/em&gt;. You want to make sure all the execution flows are tested to avoid any unexpected behaviors when the piece of code is being executed in a production environment. Also note that I said "all conceivable conditions". This is because there can be an infinite combination of inputs that your piece of code would be executed with, and your test cases might not cover all these combinations. There could be some edge cases that aren't obvious when writing the code and which aren't covered by the tests but would show up when the code is in use. Usually when that happens, you should add a new test case to cover such a case.&lt;/p&gt;

&lt;p&gt;When writing unit tests, I like to think about the piece of code from both the blackbox (how the users would see it) and the whitebox perspectives:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;em&gt;I think from a blackbox perspective when&lt;/em&gt; determining the &lt;em&gt;inputs&lt;/em&gt; and &lt;em&gt;outputs&lt;/em&gt; of the piece of code to test, and I use that to create the test cases.&lt;/li&gt;
&lt;li&gt;
&lt;em&gt;I think from a whitebox perspective when&lt;/em&gt; I need to mock dependencies, since we need knowledge about the internals of the piece of code in order to be able to mock the dependencies.&lt;/li&gt;
&lt;li&gt;
&lt;em&gt;I think from a whitebox perspective when&lt;/em&gt; I want to cover all the logical execution flows within the piece of code, and I use this to add extra test cases, making the test suite robust.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  What should I know about TDD?
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://www.wikiwand.com/en/Test-driven_development"&gt;Test Driven Development (TDD)&lt;/a&gt; is an approach to development that involves defining test cases, and using those test cases to define what code needs to be written. According to Wikipedia,&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Test-driven development (TDD) is a software development process that relies on the repetition of a very short development cycle: requirements are turned into very specific test cases, then the code is improved so that the tests pass. This is opposed to software development that allows code to be added that is not proven to meet requirements.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;This means you end up writing your tests while writing the actual code, and you no longer have writing tests as a chore that you need to do at a later time. It also makes writing tests much easier, since you write your code with the tests in mind.&lt;/p&gt;

&lt;p&gt;One of the benefits of the test driven approach is that it helps you write much better code with much cleaner code structure.&lt;/p&gt;

&lt;p&gt;Following the TDD approach involves &lt;a href="https://blog.cleancoder.com/uncle-bob/2014/12/17/TheCyclesOfTDD.html"&gt;three laws&lt;/a&gt; that you need to follow:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;You must write a failing test before you write any production code.&lt;/li&gt;
&lt;li&gt;You must not write more of a test than is sufficient to fail, or fail to compile.&lt;/li&gt;
&lt;li&gt;You must not write more production code than is sufficient to make the currently failing test pass.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;...&lt;/p&gt;

&lt;p&gt;Continue reading at xkoji.dev: &lt;a href="https://www.xkoji.dev/blog/unit-testing-javascript-applications-part-1/"&gt;https://www.xkoji.dev/blog/unit-testing-javascript-applications-part-1/&lt;/a&gt;&lt;/p&gt;

</description>
    </item>
    <item>
      <title>Set up a blog with Gatsby, GitHub pages and GitHub actions</title>
      <dc:creator>Sir Muel I 🇳🇬</dc:creator>
      <pubDate>Thu, 30 Apr 2020 00:45:45 +0000</pubDate>
      <link>https://dev.to/imolorhe/set-up-a-blog-with-gatsby-github-pages-and-github-actions-3h6k</link>
      <guid>https://dev.to/imolorhe/set-up-a-blog-with-gatsby-github-pages-and-github-actions-3h6k</guid>
      <description>&lt;p&gt;&lt;em&gt;Originally posted at &lt;a href="https://www.xkoji.dev/blog/set-up-a-blog-with-gatsby-github-pages-and-github-actions/"&gt;xkoji.dev&lt;/a&gt;&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;This is my first post on my new blog! How exciting! Creating this blog was an interesting process for me since I decided to use Gatsby for it. While I had created a &lt;a href="https://www.gatsbyjs.org/packages/gatsby-plugin-altair-graphql/"&gt;gatsby plugin&lt;/a&gt; before, I hadn't actually built anything with gatsby. Starting this new blog was an opportunity to change that.&lt;/p&gt;

&lt;p&gt;For my setup, I decided to start off with hosting the blog on &lt;a href="https://pages.github.com/"&gt;GitHub pages&lt;/a&gt;. &lt;a href="https://github.com/features/actions"&gt;GitHub actions&lt;/a&gt; made sense as the perfect tool for continuous deployment of the site, which also happened to be the first time I am using GitHub actions.&lt;/p&gt;

&lt;p&gt;After a couple of hours spent figuring out how to properly setup everything, I finally had the blog live and running at &lt;a href="https://xkojimedia.github.io/"&gt;xkojimedia.github.io&lt;/a&gt; with a minimal template.&lt;/p&gt;

&lt;p&gt;Here I would go over how I was able to setup everything.&lt;/p&gt;

&lt;p&gt;...&lt;/p&gt;

&lt;p&gt;Continue reading at: &lt;a href="https://www.xkoji.dev/blog/set-up-a-blog-with-gatsby-github-pages-and-github-actions/"&gt;https://www.xkoji.dev/blog/set-up-a-blog-with-gatsby-github-pages-and-github-actions/&lt;/a&gt;&lt;/p&gt;

</description>
      <category>gatsby</category>
      <category>github</category>
      <category>pages</category>
      <category>actions</category>
    </item>
  </channel>
</rss>
