<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Nicolas Mitchell on Medium]]></title>
        <description><![CDATA[Stories by Nicolas Mitchell on Medium]]></description>
        <link>https://medium.com/@nclsmitchell?source=rss-91fdf39b8692------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*CTh-CmTmKSwoy06YcHYE8w.jpeg</url>
            <title>Stories by Nicolas Mitchell on Medium</title>
            <link>https://medium.com/@nclsmitchell?source=rss-91fdf39b8692------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Wed, 18 Nov 2026 11:50:22 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@nclsmitchell/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[A quick introduction to test driven development with Jest]]></title>
            <link>https://medium.com/free-code-camp/a-quick-introduction-to-test-driven-development-with-jest-cac71cb94e50?source=rss-91fdf39b8692------2</link>
            <guid isPermaLink="false">https://medium.com/p/cac71cb94e50</guid>
            <category><![CDATA[test-driven-development]]></category>
            <category><![CDATA[jest]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[tech]]></category>
            <dc:creator><![CDATA[Nicolas Mitchell]]></dc:creator>
            <pubDate>Mon, 25 Jun 2018 21:18:39 GMT</pubDate>
            <atom:updated>2018-06-25T21:18:39.545Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*0jCUKud4CkbbmNrFDzIQZw.png" /></figure><p>This article is a simple walkthrough of how to apply Test Driven Development (TDD) principles to a JavaScript exercise using Jest.</p><h3>Intro</h3><p>After a few years of experience developing on my own personal projects, I recently decided to become a Full-Stack developer.</p><p>This new situation encouraged me to start thinking about practices that I’ve neglected until now, such as testing my code.</p><p>That is why I wanted to start my journey through Test Driven Development. I’ve decided to share my first steps here with you.</p><h3>The exercise</h3><p>I decided to start with the first Osherove TDD kata. You can access the full exercise <a href="http://osherove.com/tdd-kata-1/">here</a>.</p><p>The goal is to deliver a function that takes a string entry (&quot;1, 2, 3&quot; for instance) and returns the sum of all numbers.</p><p>Our project will have the following structure:</p><pre>js-kata-jest/</pre><pre>├─ src/</pre><pre>  └─ kata.js</pre><pre>├─ test/</pre><pre>  └─ kata.test.js</pre><pre>└─ package.json</pre><h3>Setting up the test environment</h3><p>First we have to set up the test environment. As a React developer, I decided to go with <a href="https://facebook.github.io/jest/">Jest</a>. You may use any other testing library of your choice.</p><h4>Installing Jest dev dependency</h4><pre>yarn add --dev jest</pre><p>or with <a href="https://www.npmjs.com/">npm</a>:</p><pre>npm install --save-dev jest</pre><h4>Activating Jest on your code editor</h4><p>I am using Atom as a code editor, and installed the <a href="https://atom.io/packages/tester-jest">tester-jest</a> package. This allowed me to run my tests on save for any *.test.js file.</p><h3>Test Driven Development</h3><p>The theory behind TDD is quite simple, and revolves around 3 steps:</p><ol><li>Writing a test for a small part of a functionality and checking that this new test is failing (Red step)</li><li>Writing the code that makes the test pass, then checking that your previous test and the new one are successful (Green step)</li><li>Refactoring the code to make sure it is clear, understandable, and behaves well with the previous functionalities</li></ol><p>In the next part, we are going to go into detail for each of these steps.</p><h3>Solving the exercise</h3><h4>First loop</h4><p>First, we want to handle the case where our add function is given an empty string or one with a single element.</p><ol><li><strong>Writing the tests</strong></li></ol><ul><li>The first test checks that an empty string returns 0</li><li>The second checks that a single element string returns the provided element</li></ul><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/a6697979cb3f03140a51abd30c3e77de/href">https://medium.com/media/a6697979cb3f03140a51abd30c3e77de/href</a></iframe><p><strong>2. Writing the code</strong></p><ul><li>First we return 0 by default</li><li>Then we provide an if statement that handles the parsing of a single provided element</li></ul><p>Here is the final code:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/69be44ffbb71af464efffaaa6ad41b07/href">https://medium.com/media/69be44ffbb71af464efffaaa6ad41b07/href</a></iframe><p><strong>3. Refactoring the code</strong></p><p>As it is our first functionality, we can skip this step for now — but we will soon return to it. ;)</p><h4>Second loop</h4><p>We will now handle the case where the string contains multiple elements:</p><ol><li><strong>Writing the tests</strong></li></ol><p>The new test makes sure that calculation of a multiple element string was done correctly:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/a1fb4bd5fd026732911c54ca3ac0eb60/href">https://medium.com/media/a1fb4bd5fd026732911c54ca3ac0eb60/href</a></iframe><p><strong>2. Writing the code</strong></p><ul><li>First we create a new if statement on purpose to be sure that our first two tests affect the new solution</li><li>Second we create a new array from the entry string, using the , as a separator</li><li>Finally, we parse each element of the newly created array to calculate the sum</li></ul><p>Here is the final code:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/cdb5c03cbfbc8eff802cbcadc4f675c9/href">https://medium.com/media/cdb5c03cbfbc8eff802cbcadc4f675c9/href</a></iframe><p><strong>3. Refactoring the code</strong></p><p>As we can see above, there are several problems within our code:</p><ul><li>the two if statement shouldn’t be decorrelated, as adding one or more to zero should behave the same.</li><li>the separator value is drowned in the code. This adds complexity if we want to change to a ; separator, for instance.</li><li>a large part of our code is located in an if statement. We could reverse the logic to extract our main code from it.</li></ul><p>So we can add a new separator variable, which will decide on the separator type. We can also merge the two if statement into one, and then reverse the logic.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/b1c1857e5abc9247d5d3c2db0a71106a/href">https://medium.com/media/b1c1857e5abc9247d5d3c2db0a71106a/href</a></iframe><p>We can now run our test again before moving on to the next loop.</p><h4>Third loop</h4><p>We can now handle the declaration of new separators and avoid the entry of negative numbers.</p><ol><li><strong>Writing the tests</strong></li></ol><ul><li>The first new test focuses on a single separator within the default values.</li><li>The second will make sure that we can configure a new separator directly within the input.</li><li>The third one will check that an error is thrown when a negative value is passed as an entry.</li></ul><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/714e59a4a46bdaaf45926b30de0c1b52/href">https://medium.com/media/714e59a4a46bdaaf45926b30de0c1b52/href</a></iframe><p><strong>2. Writing the code</strong></p><ul><li>First, we replace the separator string by a separators array, where we add the \n value.</li><li>Second, we introduce a new separator search through a regular expression. That will be added to the previous array.</li><li>We can now join the previous array elements to split the string with them.</li><li>We filter the resulting array to remove all non-number elements.</li><li>We add a new array, negatives, that will spot negative values within the entry.</li><li>If the negatives array isn’t empty, throw an error.</li></ul><p>Here is the final code:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/f8fff0d9f588a1ec297f11b6259b3fc1/href">https://medium.com/media/f8fff0d9f588a1ec297f11b6259b3fc1/href</a></iframe><p><strong>3. Refactoring the code</strong></p><p>We now have two new possible optimizations:</p><ul><li>We are using the regular expression twice, and are willing to change it easily. We can extract it within a new const regexp.</li><li>We calculate parseInt(list[i])several times, so we should store the value only once to speed up the for loop.</li></ul><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/9628422312e18e5e3e427708a4601df1/href">https://medium.com/media/9628422312e18e5e3e427708a4601df1/href</a></iframe><h3>Conclusion</h3><p>We can now run our tests again to make sure that all our expected functionalities are still working.</p><p>You may now continue on your own as well. Check out the definitive version of the kata with all tests passing <a href="https://github.com/nclsmitchell/js-kata-jest">here</a>.</p><p>Feel free to reach me on Twitter if you have any questions or comments <a href="https://twitter.com/nclsmitchell">@nclsmitchell</a> or through Medium :)</p><p>Thanks for your reading, and please clap for me if you like this post.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=cac71cb94e50" width="1" height="1" alt=""><hr><p><a href="https://medium.com/free-code-camp/a-quick-introduction-to-test-driven-development-with-jest-cac71cb94e50">A quick introduction to test driven development with Jest</a> was originally published in <a href="https://medium.com/free-code-camp">We’ve moved to freeCodeCamp.org/news</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>