<?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: Daniel Irvine 🏳️‍🌈</title>
    <description>The latest articles on DEV Community by Daniel Irvine 🏳️‍🌈 (@d_ir).</description>
    <link>https://dev.to/d_ir</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%2F3314%2F9755992b-b127-49e3-9c2a-54c891cb250b.jpg</url>
      <title>DEV Community: Daniel Irvine 🏳️‍🌈</title>
      <link>https://dev.to/d_ir</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/d_ir"/>
    <language>en</language>
    <item>
      <title>Staying out of trouble</title>
      <dc:creator>Daniel Irvine 🏳️‍🌈</dc:creator>
      <pubDate>Mon, 07 Sep 2020 20:21:11 +0000</pubDate>
      <link>https://dev.to/d_ir/staying-out-of-trouble-4n12</link>
      <guid>https://dev.to/d_ir/staying-out-of-trouble-4n12</guid>
      <description>&lt;p&gt;This is the final part of a series on React component mocks. We’ll wrap up with a recap and then look at some common difficulties that you’ll encounter.&lt;/p&gt;




&lt;p&gt;&lt;em&gt;All the code samples for this post are available at the following repo.&lt;/em&gt;&lt;/p&gt;


&lt;div class="ltag-github-readme-tag"&gt;
  &lt;div class="readme-overview"&gt;
    &lt;h2&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%2Fassets%2Fgithub-logo-5a155e1f9a670af7944dd5e12375bc76ed542ea80224905ecaf878b9157cdefc.svg" alt="GitHub logo"&gt;
      &lt;a href="https://github.com/dirv" rel="noopener noreferrer"&gt;
        dirv
      &lt;/a&gt; / &lt;a href="https://github.com/dirv/mocking-react-components" rel="noopener noreferrer"&gt;
        mocking-react-components
      &lt;/a&gt;
    &lt;/h2&gt;
    &lt;h3&gt;
      An example of how to mock React components
    &lt;/h3&gt;
  &lt;/div&gt;
&lt;/div&gt;





&lt;p&gt;Mocks are a notoriously difficult testing facility. That’s why some educators don’t use and don’t teach them.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;But mastering mocks will make give you an extra weapon to fight brittle, time-sinking tests.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;So how can you ensure you stay safe with mocks? Simple: stick to the patterns!&lt;/p&gt;

&lt;h3&gt;
  
  
  Stick to the patterns
&lt;/h3&gt;

&lt;p&gt;If you stick to the patterns I’ve shown you in this series, you shouldn’t have problems.&lt;/p&gt;

&lt;p&gt;Start with the basic mock function which renders a &lt;code&gt;div&lt;/code&gt; with a &lt;code&gt;data-testid&lt;/code&gt; attached. We looked at this in &lt;a href="https://dev.to/d_ir/verifying-children-passed-to-react-mock-components-2mf9"&gt;part two&lt;/a&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="nx"&gt;jest&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;mock&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;../src/PostContent&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="na"&gt;PostContent&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;jest&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;fn&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="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;div&lt;/span&gt; &lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nx"&gt;testid&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;PostContent&lt;/span&gt;&lt;span class="dl"&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="p"&gt;}))&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If you need to, you can render &lt;code&gt;children&lt;/code&gt; too. That was described in &lt;a href="https://dev.to/d_ir/verifying-children-passed-to-react-mock-components-2mf9"&gt;part three&lt;/a&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="nx"&gt;jest&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;mock&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;../src/PostContent&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="na"&gt;PostContent&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;jest&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;fn&lt;/span&gt;&lt;span class="p"&gt;(({&lt;/span&gt; &lt;span class="nx"&gt;children&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="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;div&lt;/span&gt; &lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nx"&gt;testid&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;PostContent&lt;/span&gt;&lt;span class="dl"&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;children&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/div&lt;/span&gt;&lt;span class="err"&gt;&amp;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;If you &lt;em&gt;really&lt;/em&gt; need to, you can use prop value to make unique &lt;code&gt;data-testid&lt;/code&gt;s. But this is often unnecessary complexity. That was in &lt;a href="https://dev.to/d_ir/testing-multiple-instances-of-the-same-mocked-component-c16"&gt;part four&lt;/a&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="nx"&gt;jest&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;mock&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;../src/PostContent&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="na"&gt;PostContent&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;jest&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;fn&lt;/span&gt;&lt;span class="p"&gt;(({&lt;/span&gt; &lt;span class="nx"&gt;children&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;id&lt;/span&gt; &lt;span class="p"&gt;})&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;div&lt;/span&gt; &lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nx"&gt;testid&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="s2"&gt;`PostContent-&lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;id&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;`&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;children&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/div&amp;gt;&lt;/span&gt;&lt;span class="err"&gt;)
&lt;/span&gt;&lt;span class="p"&gt;}))&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;I always dislike giving any kind of advice on what to &lt;em&gt;avoid&lt;/em&gt;: every technique has its place. But if I was to name one thing to be careful about, I’d say it would be building fakes and in particular use of the Jest function &lt;code&gt;mockImplementation&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Why? Well, a major reason for using spies and stubs is to help build independent test suites that don’t slow you down.&lt;/p&gt;

&lt;p&gt;An important way to do that is to restrict your code to a small number of patterns. It’s a little bit like having a set of coding standards, but at a higher level.&lt;/p&gt;

&lt;p&gt;When you start building fakes and elaborate mock implementations, you are moving away from that goal because now you have logic within your tests: you can’t glance at them and immediately know how they work. And any change to the production code requires you to re-understand the fake implementation code before inevitably changing that too.&lt;/p&gt;

&lt;p&gt;Conversely, when you stick to the handful of mock patterns I’ve shown you, you will get faster and faster at writing tests that use them. That’s because you recognise the patterns and don’t need to re-evaluate them in your brain each time you see them.&lt;/p&gt;

&lt;h3&gt;
  
  
  What if none of the patterns works for your test suites?
&lt;/h3&gt;

&lt;p&gt;If you’re stuck, the first question to ask yourself is: how &lt;strong&gt;testable&lt;/strong&gt; is my production code?&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Because it’s not mocks that are causing you test pain, but production code that isn’t structured for testability.&lt;/strong&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Improving the testability of your code
&lt;/h3&gt;

&lt;p&gt;The number one problem I see with React codebases is very large components that express many different ideas. Often, new features are just piled on top of each other rather than taking time to sort out abstractions or find a logical organisational structure.&lt;/p&gt;

&lt;p&gt;So a good place to start is to &lt;strong&gt;split your large components apart.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;How large is large? File size is often a good metric to use: for me, anything more than 100 lines is suspect. And many of my components are fewer than 10 lines in size!&lt;/p&gt;

&lt;p&gt;What if it’s not obvious how to split a component? Start with the &lt;strong&gt;single responsibility principle&lt;/strong&gt;: each component should do one thing and one thing only.&lt;/p&gt;

&lt;p&gt;Of course, the notion of one “thing” leaves you plenty of rope to hang yourself. Figuring out elegant “things” is most of the difficulty in software design.&lt;/p&gt;

&lt;p&gt;If you’re interested in this topic then I’d suggest learning about coupling, cohesion and &lt;a href="https://vimeo.com/10837903" rel="noopener noreferrer"&gt;connascence&lt;/a&gt;, all of which apply to React components, even though you won’t often hear React educators talking about them.&lt;/p&gt;

&lt;h2&gt;
  
  
  Where to go from here
&lt;/h2&gt;

&lt;p&gt;In this series I’ve shown you a very specific way to test React components. If you’re interested in more detailed theory and history about these techniques, then take a look at my book, &lt;a href="https://www.packtpub.com/product/mastering-react-test-driven-development/9781789133417" rel="noopener noreferrer"&gt;Mastering React Test-Driven Development&lt;/a&gt;. It doesn’t use React Testing Library but instead explores testing from first principles. Doing that will give you a much deeper understanding of successful React testing.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>react</category>
      <category>testing</category>
    </item>
    <item>
      <title>Testing multiple instances of the same mocked component</title>
      <dc:creator>Daniel Irvine 🏳️‍🌈</dc:creator>
      <pubDate>Mon, 07 Sep 2020 20:21:04 +0000</pubDate>
      <link>https://dev.to/d_ir/testing-multiple-instances-of-the-same-mocked-component-c16</link>
      <guid>https://dev.to/d_ir/testing-multiple-instances-of-the-same-mocked-component-c16</guid>
      <description>&lt;p&gt;This is part four of a series on testing React with component mocks. In part 2 we looked at the basic form of component mocks. In part 3, we added the ability to assert on component children. Now we’ll look at the most complex piece of the puzzle: handling multiple instances of the same mock.&lt;/p&gt;




&lt;p&gt;&lt;em&gt;All the code samples for this post are available at the following repo.&lt;/em&gt;&lt;/p&gt;


&lt;div class="ltag-github-readme-tag"&gt;
  &lt;div class="readme-overview"&gt;
    &lt;h2&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%2Fassets%2Fgithub-logo-5a155e1f9a670af7944dd5e12375bc76ed542ea80224905ecaf878b9157cdefc.svg" alt="GitHub logo"&gt;
      &lt;a href="https://github.com/dirv" rel="noopener noreferrer"&gt;
        dirv
      &lt;/a&gt; / &lt;a href="https://github.com/dirv/mocking-react-components" rel="noopener noreferrer"&gt;
        mocking-react-components
      &lt;/a&gt;
    &lt;/h2&gt;
    &lt;h3&gt;
      An example of how to mock React components
    &lt;/h3&gt;
  &lt;/div&gt;
&lt;/div&gt;





&lt;p&gt;Let’s continue with a new component, &lt;code&gt;TopFivePostsPage&lt;/code&gt;, which perhaps unsurprisingly shows the top five posts.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;

&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;PostContent&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;./PostContent&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;TopFivePostsPage&lt;/span&gt; &lt;span class="o"&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="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;ol&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;PostContent&lt;/span&gt; &lt;span class="nx"&gt;id&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;top1&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="o"&gt;/&amp;gt;&lt;/span&gt;
    &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;PostContent&lt;/span&gt; &lt;span class="nx"&gt;id&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;top2&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="o"&gt;/&amp;gt;&lt;/span&gt;
    &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;PostContent&lt;/span&gt; &lt;span class="nx"&gt;id&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;top3&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="o"&gt;/&amp;gt;&lt;/span&gt;
    &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;PostContent&lt;/span&gt; &lt;span class="nx"&gt;id&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;top4&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="o"&gt;/&amp;gt;&lt;/span&gt;
    &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;PostContent&lt;/span&gt; &lt;span class="nx"&gt;id&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;top5&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="o"&gt;/&amp;gt;&lt;/span&gt;
  &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/ol&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;


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

&lt;/div&gt;

&lt;p&gt;To test that, we use &lt;code&gt;queryAllByTestId&lt;/code&gt; in combination with the &lt;code&gt;toHaveLength&lt;/code&gt; matcher.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;

&lt;span class="nf"&gt;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;BlogPage&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="nf"&gt;it&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;renders five PostContent components&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="nf"&gt;render&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;TopFivePostsPage&lt;/span&gt; &lt;span class="o"&gt;/&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="nf"&gt;expect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;screen&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;queryAllByTestId&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;PostContent&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="nf"&gt;toHaveLength&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="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;And for our second test, we can use five &lt;code&gt;expect&lt;/code&gt; statements, each with the different prop values.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;

&lt;span class="nf"&gt;it&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;constructs a PostContent for each top 5 entry&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="nf"&gt;render&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;TopFivePostsPage&lt;/span&gt; &lt;span class="o"&gt;/&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="nf"&gt;expect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;PostContent&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;toHaveBeenCalledWith&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;top1&lt;/span&gt;&lt;span class="dl"&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="nf"&gt;anything&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt;
  &lt;span class="nf"&gt;expect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;PostContent&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;toHaveBeenCalledWith&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;top2&lt;/span&gt;&lt;span class="dl"&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="nf"&gt;anything&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt;
  &lt;span class="nf"&gt;expect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;PostContent&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;toHaveBeenCalledWith&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;top3&lt;/span&gt;&lt;span class="dl"&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="nf"&gt;anything&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt;
  &lt;span class="nf"&gt;expect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;PostContent&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;toHaveBeenCalledWith&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;top4&lt;/span&gt;&lt;span class="dl"&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="nf"&gt;anything&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt;
  &lt;span class="nf"&gt;expect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;PostContent&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;toHaveBeenCalledWith&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;top5&lt;/span&gt;&lt;span class="dl"&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="nf"&gt;anything&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;But there’s something not quite right about this. We haven’t tested the &lt;em&gt;order&lt;/em&gt; of rendering. The &lt;code&gt;toHaveBeenCalledWith&lt;/code&gt; matcher doesn’t care about order.&lt;/p&gt;

&lt;p&gt;We can use &lt;code&gt;.mock.calls&lt;/code&gt; instead.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;

&lt;span class="nf"&gt;it&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;renders PostContent items in the right order&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="nf"&gt;render&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;TopFivePostsPage&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;postContentIds&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;PostContent&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;mock&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;calls&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;map&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="nx"&gt;args&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;args&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;id&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

  &lt;span class="nf"&gt;expect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;postContentIds&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;toEqual&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;
    &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;top1&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;top2&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;top3&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;top4&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;top5&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;
  &lt;span class="p"&gt;])&lt;/span&gt;
&lt;span class="p"&gt;})&lt;/span&gt;


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

&lt;/div&gt;

&lt;p&gt;If you try running this after the first two tests for &lt;code&gt;TopFivePostsPage&lt;/code&gt;, you’ll get a strange error that &lt;code&gt;PostContent&lt;/code&gt; was actually called &lt;em&gt;fifteen&lt;/em&gt; times! That’s because when we need to &lt;em&gt;clear&lt;/em&gt; our mock between each test.&lt;/p&gt;

&lt;p&gt;We do that by adding the &lt;code&gt;clearMocks&lt;/code&gt; property to our Jest config. Here’s my &lt;code&gt;package.json&lt;/code&gt; for comparison.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="w"&gt;

&lt;/span&gt;&lt;span class="nl"&gt;"jest"&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;"transform"&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;"^.+\\.jsx?$"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"babel-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;span class="nl"&gt;"setupFilesAfterEnv"&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="s2"&gt;"./jest.setup.js"&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"clearMocks"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="kc"&gt;true&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;Notice the last test we wrote actually makes the previous test redundant, so you can delete that one safely.&lt;/p&gt;

&lt;h2&gt;
  
  
  When that’s not enough: mock instance IDs
&lt;/h2&gt;

&lt;p&gt;Very occasionally, you'll need more than this. For example, if you need to test children passed &lt;em&gt;and&lt;/em&gt; you also have multiple instances. In that case, you can use one of the component’s props to give a &lt;strong&gt;unique&lt;/strong&gt; test ID to your component instance.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;

&lt;span class="nx"&gt;jest&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;mock&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;../src/PostContent&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="na"&gt;PostContent&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;jest&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;fn&lt;/span&gt;&lt;span class="p"&gt;(({&lt;/span&gt; &lt;span class="nx"&gt;children&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;id&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="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;div&lt;/span&gt; &lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nx"&gt;testid&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="s2"&gt;`PostContent-&lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;id&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;`&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
      &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;children&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/div&lt;/span&gt;&lt;span class="err"&gt;&amp;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;Personally, I really dislike this. It’s &lt;em&gt;complex&lt;/em&gt;, and more complex than I’m comfortable with. But it exists, and sometimes it’s necessary to use it.&lt;/p&gt;




&lt;p&gt;&lt;em&gt;Remember that mocks are there to help you speed up your testing, and testing is there to help speed up your development. When mocks become overly complex, you have to spend more time reading them and maintaining them, so they slow you down. I’ll cover more on this in the next part.&lt;/em&gt;&lt;/p&gt;




&lt;h2&gt;
  
  
  Yet more lessons
&lt;/h2&gt;

&lt;p&gt;So what have we learned now?&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Use &lt;code&gt;queryAllByTestId&lt;/code&gt; when testing multiple instances of a mocked component&lt;/li&gt;
&lt;li&gt;Use &lt;code&gt;.mock.calls&lt;/code&gt; to check ordering of calls, or for testing render props.&lt;/li&gt;
&lt;li&gt;Use Jest’s &lt;code&gt;clearMocks&lt;/code&gt; configuration setting to ensure your spies are cleared before each test.&lt;/li&gt;
&lt;li&gt;If all else fails, you can use props within your rendered output to give unique &lt;code&gt;data-testid&lt;/code&gt; values for each instance.&lt;/li&gt;
&lt;li&gt;Keep your mocks as simple as possible!&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;That’s all there is to it. In the final part, we’ll look at why mocks can get you into trouble—and how to avoid it.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>react</category>
      <category>testing</category>
    </item>
    <item>
      <title>Verifying children passed to React mock components</title>
      <dc:creator>Daniel Irvine 🏳️‍🌈</dc:creator>
      <pubDate>Mon, 07 Sep 2020 20:20:49 +0000</pubDate>
      <link>https://dev.to/d_ir/verifying-children-passed-to-react-mock-components-2mf9</link>
      <guid>https://dev.to/d_ir/verifying-children-passed-to-react-mock-components-2mf9</guid>
      <description>&lt;p&gt;This is the third part in a series on React testing. In the last part, we looked at &lt;a href="https://dev.to/d_ir/the-basic-form-for-react-component-mocks-g5"&gt;the basic form of React component mocks&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Another thing you might want to do with mocks is test that it has the correct children passed. That’s what we’ll look at now.&lt;/p&gt;




&lt;p&gt;&lt;em&gt;All the code samples for this post are available at the following repo.&lt;/em&gt;&lt;/p&gt;


&lt;div class="ltag-github-readme-tag"&gt;
  &lt;div class="readme-overview"&gt;
    &lt;h2&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%2Fassets%2Fgithub-logo-5a155e1f9a670af7944dd5e12375bc76ed542ea80224905ecaf878b9157cdefc.svg" alt="GitHub logo"&gt;
      &lt;a href="https://github.com/dirv" rel="noopener noreferrer"&gt;
        dirv
      &lt;/a&gt; / &lt;a href="https://github.com/dirv/mocking-react-components" rel="noopener noreferrer"&gt;
        mocking-react-components
      &lt;/a&gt;
    &lt;/h2&gt;
    &lt;h3&gt;
      An example of how to mock React components
    &lt;/h3&gt;
  &lt;/div&gt;
&lt;/div&gt;





&lt;p&gt;Imagine that we want to inset a mailing list sign up form inside of the &lt;code&gt;PostContent&lt;/code&gt;. We can do that by passing children elements to it.&lt;/p&gt;

&lt;p&gt;Here’s the newly improved &lt;code&gt;BlogPage&lt;/code&gt; component:&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="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;BlogPage&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="nx"&gt;url&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;id&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;getPostIdFromUrl&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;url&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;handleSignUp&lt;/span&gt; &lt;span class="o"&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;// ...&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="k"&gt;return &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;PostContent&lt;/span&gt; &lt;span class="nx"&gt;id&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;id&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
      &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;input&lt;/span&gt; &lt;span class="nx"&gt;type&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;email&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="nx"&gt;placeholder&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Sign up to my mailing list!&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="o"&gt;/&amp;gt;&lt;/span&gt;
      &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;button&lt;/span&gt; &lt;span class="nx"&gt;onClick&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;handleSignUp&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;Sign&lt;/span&gt; &lt;span class="nx"&gt;up&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/button&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;    &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/PostContent&lt;/span&gt;&lt;span class="err"&gt;&amp;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;Crucially, our &lt;code&gt;BlogPage&lt;/code&gt; tests shouldn’t care &lt;em&gt;what&lt;/em&gt; &lt;code&gt;PostContent&lt;/code&gt; does with the children. They should just care that it was given the children.&lt;/p&gt;

&lt;p&gt;We could test this by pulling out the &lt;code&gt;children&lt;/code&gt; prop from the &lt;code&gt;.mock.calls&lt;/code&gt; entry and then rendering it with &lt;code&gt;render&lt;/code&gt;. In other words, treating it like a render prop.&lt;/p&gt;

&lt;p&gt;But there’s a more straightforward way, which is to modify the mock component to render its &lt;code&gt;children&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="nx"&gt;jest&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;mock&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;../src/PostContent&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="na"&gt;PostContent&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;jest&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;fn&lt;/span&gt;&lt;span class="p"&gt;(({&lt;/span&gt; &lt;span class="nx"&gt;children&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="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;div&lt;/span&gt; &lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nx"&gt;testid&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;PostContent&lt;/span&gt;&lt;span class="dl"&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;children&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/div&lt;/span&gt;&lt;span class="err"&gt;&amp;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 we can write a test that checks that a &lt;code&gt;button&lt;/code&gt; was rendered as a child of &lt;code&gt;PostContent&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="nf"&gt;it&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;renders the mailing list sign up button as a child of PostContent&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="nf"&gt;render&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;BlogPage&lt;/span&gt; &lt;span class="nx"&gt;url&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;http://example.com/blog/my-web-page&lt;/span&gt;&lt;span class="dl"&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;postContentElement&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;screen&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;getByTestId&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;PostContent&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;button&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;screen&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;queryByRole&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;button&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="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Sign up&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="p"&gt;})&lt;/span&gt;

  &lt;span class="nf"&gt;expect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;postContentElement&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;toContainElement&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;button&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;The same technique can be repeated for the &lt;code&gt;input&lt;/code&gt; field.&lt;/p&gt;

&lt;p&gt;If you run this test, you’ll notice a problem. Our previous test that checks the passed props is now failing. Its expectation looked like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;  &lt;span class="nf"&gt;expect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;PostContent&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;toHaveBeenCalledWith&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;postId&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="nf"&gt;anything&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 failing because all of a sudden we have a &lt;code&gt;children&lt;/code&gt; prop, which is unexpected according to this test.&lt;/p&gt;

&lt;p&gt;We fix that using &lt;code&gt;expect.objectContaining&lt;/code&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  Use &lt;code&gt;expect.objectContaining&lt;/code&gt; to narrow down your tests
&lt;/h3&gt;




&lt;p&gt;It’s often useful to have multiple unit tests for a single mock component call! I usually start with one test, with all props specified. But for any prop values of sufficient complexity, it can be useful to split that out into a test of its own with a good test description. The &lt;code&gt;children&lt;/code&gt; prop is a special case of that: our test that checks we pass the right ID is independent of anything to do with the displaying inset content.&lt;/p&gt;




&lt;p&gt;We can avoid testing &lt;code&gt;content&lt;/code&gt; by using &lt;code&gt;expect.objectContaining&lt;/code&gt; in our expectation:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;  &lt;span class="nf"&gt;expect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;PostContent&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;toHaveBeenCalledWith&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="nf"&gt;objectContaining&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="na"&gt;id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;postId&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="nf"&gt;anything&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  More lessons
&lt;/h2&gt;

&lt;p&gt;So what have we learned now?&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;To test children passed to mocks, modify the mock component to be `jest.fn(({ children }) = {children})&lt;/li&gt;
&lt;li&gt;Use &lt;code&gt;toContainElement&lt;/code&gt; from the &lt;code&gt;jest-dom&lt;/code&gt; matchers package to check that components are rendered as children of your mocked component.&lt;/li&gt;
&lt;li&gt;Use &lt;code&gt;expect.objectContaining&lt;/code&gt; to write unit tests that don’t break when your props change.&lt;/li&gt;
&lt;li&gt;Use Jest’s &lt;code&gt;clearMocks&lt;/code&gt; configuration setting to ensure your spies are cleared before each test.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;In part 4, we’ll see how we go about &lt;a href="https://dev.to/d_ir/testing-multiple-instances-of-the-same-mocked-component-c16"&gt;testing multiple rendered instances of the same mock component&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>react</category>
      <category>testing</category>
    </item>
    <item>
      <title>The basic form for React component mocks</title>
      <dc:creator>Daniel Irvine 🏳️‍🌈</dc:creator>
      <pubDate>Mon, 07 Sep 2020 20:20:36 +0000</pubDate>
      <link>https://dev.to/d_ir/the-basic-form-for-react-component-mocks-g5</link>
      <guid>https://dev.to/d_ir/the-basic-form-for-react-component-mocks-g5</guid>
      <description>&lt;p&gt;In &lt;a href="https://dev.to/d_ir/mocks-aren-t-evil-better-mocking-with-react-testing-library-3hii"&gt;the first part of this series&lt;/a&gt; I looked at why mocking is useful.&lt;/p&gt;

&lt;p&gt;In this part I’ll cover the basic format of React mock components.&lt;/p&gt;




&lt;p&gt;&lt;em&gt;All the code samples for this post are available at the following repo.&lt;/em&gt;&lt;/p&gt;


&lt;div class="ltag-github-readme-tag"&gt;
  &lt;div class="readme-overview"&gt;
    &lt;h2&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%2Fassets%2Fgithub-logo-5a155e1f9a670af7944dd5e12375bc76ed542ea80224905ecaf878b9157cdefc.svg" alt="GitHub logo"&gt;
      &lt;a href="https://github.com/dirv" rel="noopener noreferrer"&gt;
        dirv
      &lt;/a&gt; / &lt;a href="https://github.com/dirv/mocking-react-components" rel="noopener noreferrer"&gt;
        mocking-react-components
      &lt;/a&gt;
    &lt;/h2&gt;
    &lt;h3&gt;
      An example of how to mock React components
    &lt;/h3&gt;
  &lt;/div&gt;
&lt;/div&gt;





&lt;p&gt;Let’s look again at the components we’re working with: &lt;code&gt;BlogPage&lt;/code&gt; and &lt;code&gt;PostContent&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Here’s &lt;code&gt;BlogPage&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="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;getPostIdFromUrl&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;url&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt;
  &lt;span class="nx"&gt;url&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;substr&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;url&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;lastIndexOf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;/&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="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;BlogPage&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="nx"&gt;url&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;id&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;getPostIdFromUrl&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;url&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

  &lt;span class="k"&gt;return &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;PostContent&lt;/span&gt; &lt;span class="nx"&gt;id&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;id&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="sr"&gt;/&lt;/span&gt;&lt;span class="err"&gt;&amp;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;&lt;code&gt;BlogPage&lt;/code&gt; doesn’t do much other than show a &lt;code&gt;PostContent&lt;/code&gt;. But it does have a little piece of functionality that we’re interested in, which is parsing the &lt;code&gt;url&lt;/code&gt; prop value to pull out the required post &lt;code&gt;id&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;PostContent&lt;/code&gt; is a little more complicated: it calls the browser’s in-built &lt;code&gt;fetch&lt;/code&gt; function to retrieve the text of a blog post at the URL &lt;code&gt;/post?id=${id}&lt;/code&gt;, where &lt;code&gt;id&lt;/code&gt; is a prop passed to it.&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="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;PostContent&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="nx"&gt;id&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="p"&gt;[&lt;/span&gt; &lt;span class="nx"&gt;text&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;setText&lt;/span&gt; &lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;useState&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;""&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

  &lt;span class="nf"&gt;useEffect&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="nf"&gt;fetchPostContent&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;id&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;id&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;fetchPostContent&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;async &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;result&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nf"&gt;fetch&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;`/post?id=&lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;id&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;`&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;result&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;ok&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="nf"&gt;setText&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;result&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;text&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="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;p&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;text&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/p&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Actually, what &lt;code&gt;PostContent&lt;/code&gt; does isn’t important because we’re not going to look at it again!&lt;/p&gt;

&lt;p&gt;We’re going to write some tests for &lt;code&gt;BlogPage&lt;/code&gt; in our test file &lt;code&gt;BlogPage.test.js&lt;/code&gt;. To do that, we’ll mock out &lt;code&gt;PostContent&lt;/code&gt; so that we won’t have to worry about its implementation.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The important point is that we stub out &lt;code&gt;PostContent&lt;/code&gt; so that our &lt;code&gt;BlogPage.test.js&lt;/code&gt; test suite is shielded from whatever it is that &lt;code&gt;PostContent&lt;/code&gt; does.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Here’s the mock for &lt;code&gt;PostContent&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="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;PostContent&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;../src/PostContent&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;

&lt;span class="nx"&gt;jest&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;mock&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;../src/PostContent&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="na"&gt;PostContent&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;jest&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;fn&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="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;div&lt;/span&gt; &lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nx"&gt;testid&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;PostContent&lt;/span&gt;&lt;span class="dl"&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="p"&gt;}))&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Let’s break this down.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The mock is defined with &lt;code&gt;jest.mock&lt;/code&gt;. This must mirror the corresponding import. The call is hoisted so that the &lt;code&gt;import&lt;/code&gt; can be replaced. Jest replaces the entire module with your newly defined module. So in this case, we’re mocking out the entire &lt;code&gt;../src/PostContent&lt;/code&gt; file.&lt;/li&gt;
&lt;li&gt;Since mocks are at the module level, any component you’re mocking will need to be in its own module.&lt;/li&gt;
&lt;li&gt;The call to &lt;code&gt;jest.fn&lt;/code&gt; produces a &lt;strong&gt;spy&lt;/strong&gt;: an object that records when it is called and with what parameters. We can then test calls using the &lt;code&gt;toHaveBeenCalled&lt;/code&gt; and &lt;code&gt;toHaveBeenCalledWith&lt;/code&gt; matchers.&lt;/li&gt;
&lt;li&gt;The parameter to &lt;code&gt;jest.fn&lt;/code&gt; defines a &lt;strong&gt;stub&lt;/strong&gt; value which is returned when the function is called (when the component is rendered).&lt;/li&gt;
&lt;li&gt;Stub implementations &lt;strong&gt;should always be as simple as you can make them&lt;/strong&gt;. For React components, that means a &lt;code&gt;div&lt;/code&gt;—which is arguably the HTML element with the least amount of meaning!&lt;/li&gt;
&lt;li&gt;It &lt;em&gt;does&lt;/em&gt; have an attribute of &lt;code&gt;data-testid&lt;/code&gt; that we’ll use to get hold of this specific element in the DOM.&lt;/li&gt;
&lt;li&gt;React Testing Library argues against using &lt;code&gt;data-testid&lt;/code&gt; where possible, because it wants you to treat your testing as if the test runner was a real person using your software. But for mocks I ignore that guidance, because mocks are by definition a technical concern.&lt;/li&gt;
&lt;li&gt;The &lt;code&gt;data-testid&lt;/code&gt; value matches the name of component. In this case that means it’s &lt;code&gt;PostContent&lt;/code&gt;. This is a standard convention that I follow for all my mocks.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This is the basic form of React component mocks. 90% (or more) of my mocks look this. The other 10% have some small additions that we’ll look at in later posts.&lt;/p&gt;

&lt;p&gt;With that mock in place, let’s write some tests for &lt;code&gt;BlogPage&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Verifying that the mocked component is rendered in the DOM
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nf"&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;BlogPage&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="nf"&gt;it&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;renders a PostContent&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="nf"&gt;render&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;BlogPage&lt;/span&gt; &lt;span class="nx"&gt;url&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;http://example.com/blog/my-web-page&lt;/span&gt;&lt;span class="dl"&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="nf"&gt;expect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;screen&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;queryByTestId&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;PostContent&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="nf"&gt;toBeInTheDocument&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 test is the first of &lt;strong&gt;two&lt;/strong&gt; tests that are &lt;em&gt;always&lt;/em&gt; required when you use component mocks. The &lt;code&gt;screen.queryByTestId&lt;/code&gt; searches in the current DOM  for a component with a &lt;code&gt;data-testid&lt;/code&gt; value of &lt;code&gt;PostContent&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;In other words, it checks that we did in fact render the &lt;code&gt;PostContent&lt;/code&gt; component.&lt;/p&gt;

&lt;h3&gt;
  
  
  The responsible use of &lt;code&gt;queryByTestId&lt;/code&gt;
&lt;/h3&gt;

&lt;p&gt;Notice that I’ve used &lt;code&gt;queryByTestId&lt;/code&gt;. React Testing Library tries to push you away from this function on two accounts: first, it wants you to use &lt;code&gt;getBy&lt;/code&gt; in favour of &lt;code&gt;queryBy&lt;/code&gt;, and second, as I’ve already mentioned above, it doesn’t want you to search by test ID.&lt;/p&gt;

&lt;p&gt;In fact, testing mocks is about the only time I use &lt;code&gt;queryByTestId&lt;/code&gt;. I can’t think of a time that I’ve not managed to avoid using &lt;code&gt;TestId&lt;/code&gt; variants for non-mocked components.  But for mocks, its perfect: because it’s exactly that technical detail that we want to check. The user will never see this component, it’s purely there for our tests.&lt;/p&gt;

&lt;p&gt;What we gain is the ability to have a consistent way of building mock objects: &lt;code&gt;&amp;lt;div data-testid="ComponentName" /&amp;gt;&lt;/code&gt; is the standard pattern we can use for all mock objects.&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;code&gt;getBy*&lt;/code&gt; vs &lt;code&gt;queryBy*&lt;/code&gt;
&lt;/h3&gt;

&lt;p&gt;&lt;code&gt;getBy&lt;/code&gt; variants raise exceptions if they can’t match an element. In my opinion, this is only appropriate when the calls are &lt;em&gt;not&lt;/em&gt; part of an expectation.&lt;/p&gt;

&lt;p&gt;So if you had:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nf"&gt;expect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;screen&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;getByTestId&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;PostContent&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="nf"&gt;toBeInTheDocument&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If you hadn’t rendered &lt;code&gt;&amp;lt;PostContent /&amp;gt;&lt;/code&gt; this test would blow up with an exception from &lt;code&gt;getByTestId&lt;/code&gt;. The expectation is never run at all!&lt;/p&gt;

&lt;p&gt;Given the choice between an expectation failing and an exception being raised, I’ll choose the expectation any day, since it’s more meaningful to the test runner.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Unit tests, and in particular when TDD style tests, are very often about the presence of elements. For these tests I find the &lt;code&gt;queryBy&lt;/code&gt; much more to my liking.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  Verifying that the mock is passed the correct props
&lt;/h2&gt;

&lt;p&gt;The second test we need checks that the right props were passed to &lt;code&gt;PostContent&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="nf"&gt;it&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;constructs a PostContent with an id prop created from the url&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;postId&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;my-amazing-post&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;
  &lt;span class="nf"&gt;render&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;BlogPage&lt;/span&gt; &lt;span class="nx"&gt;url&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="s2"&gt;`http://example.com/blog/&lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;postId&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;`&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="sr"&gt;/&amp;gt;&lt;/span&gt;&lt;span class="err"&gt;)
&lt;/span&gt;  &lt;span class="nf"&gt;expect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;PostContent&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;toHaveBeenCalledWith&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;postId&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="nf"&gt;anything&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 uses the standard Jest matchers, &lt;code&gt;toHaveBeenCalledWith&lt;/code&gt; to ensure that the &lt;code&gt;PostContent&lt;/code&gt; function was called with the parameters we’re expecting.&lt;/p&gt;




&lt;p&gt;&lt;em&gt;When React instantiates your component, it’s simply calling the defined function with props as an object as the first parameter, and a ref as the second parameter. The second parameter is usually unimportant.&lt;/em&gt;&lt;/p&gt;




&lt;p&gt;The JSX statement &lt;code&gt;&amp;lt;PostContent id="my-amazing-post" /&amp;gt;&lt;/code&gt; results in the function call &lt;code&gt;PostContent({ id: "my-amazing-post" })&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;However, it also includes a phantom second parameter that is never useful to us, so we have to account for that.&lt;/p&gt;

&lt;h3&gt;
  
  
  Using &lt;code&gt;expect.anything&lt;/code&gt; for the second parameter to &lt;code&gt;toHaveBeenCalledWith&lt;/code&gt;
&lt;/h3&gt;

&lt;p&gt;The second parameter that React passes to your component is an instance ref. It’s usually unimportant to our tests, so you’ll always want to pass &lt;code&gt;expect.anything()&lt;/code&gt; to signify that you aren’t interested in its value.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;If you wanted to get rid of the &lt;code&gt;expect.anything()&lt;/code&gt; call, you could write your own Jest matcher that passes it for you.&lt;/em&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  If you’re passing no props, just use &lt;code&gt;toHaveBeenCalled&lt;/code&gt;
&lt;/h3&gt;

&lt;p&gt;On rare occasions the component you’ve mocked will take no parameters. You can use &lt;code&gt;toHaveBeenCalled&lt;/code&gt; as a simpler version of &lt;code&gt;toHaveBeenCalledWith&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Understanding the basic rules of component mocks
&lt;/h2&gt;

&lt;p&gt;We’ve written two tests and one mock. Here’s the important lessons that we’ve uncovered so far:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Your mock should be a &lt;strong&gt;spy&lt;/strong&gt; using &lt;code&gt;jest.fn&lt;/code&gt; and have a &lt;strong&gt;stub&lt;/strong&gt; return value of the simplest component you can possibly have, which is &lt;code&gt;&amp;lt;div /&amp;gt;&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;You should also set a &lt;code&gt;data-testid&lt;/code&gt; attribute so you can directly pinpoint this element in the DOM.&lt;/li&gt;
&lt;li&gt;The value of this attribute is, by convention, the name of the mocked component. So for the &lt;code&gt;PostContent&lt;/code&gt; component, its stubbed value is &lt;code&gt;&amp;lt;div data-testid="PostContent" /&amp;gt;&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Every mock requires at least two tests: the first checks that it is present in the DOM, and the second tests that it was called with the correct props.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Why two tests?
&lt;/h2&gt;

&lt;p&gt;I’ve mentioned a couple of times that we need at least two tests. But why is this?&lt;/p&gt;

&lt;p&gt;If you didn't have the first test, to check for presence in the DOM, then you could make the second test pass by using a simple function call:&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="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;BlogPost&lt;/span&gt; &lt;span class="o"&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="nc"&gt;PostContent&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="na"&gt;id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;my-awesome-post&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="p"&gt;})&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Why you would want to do this is a subject of a whole other blog post, but here’s the short version: generally we consider a function call to be &lt;em&gt;simpler&lt;/em&gt; than a JSX statement. When you’re using &lt;em&gt;strict&lt;/em&gt; test principles you should &lt;em&gt;always&lt;/em&gt; write the simplest code to make your test pass.&lt;/p&gt;

&lt;p&gt;Now what about if you had the first test, but not the second?&lt;/p&gt;

&lt;p&gt;You could make it pass like this:&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="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;BlogPost&lt;/span&gt; &lt;span class="o"&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="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;PostContent&lt;/span&gt; &lt;span class="o"&gt;/&amp;gt;&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Again, this is the simplest production code to make the test pass.&lt;/p&gt;

&lt;p&gt;In order to get to the actual solution, you need both tests.&lt;/p&gt;

&lt;p&gt;This is an important difference between end-to-end tests and unit tests: unit tests are defensive in a way that end-to-end tests tend not to be.&lt;/p&gt;




&lt;p&gt;&lt;em&gt;&lt;strong&gt;Key point&lt;/strong&gt;: Always write the simplest production code to make your tests pass. Doing so will help you write a test suite which covers all scenarios.&lt;/em&gt;&lt;/p&gt;




&lt;p&gt;That covers the basics of mock components. In the next part, we’ll look at &lt;a href="https://dev.to/d_ir/verifying-children-passed-to-react-mock-components-2mf9"&gt;testing child components that are passed to your mocks&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>react</category>
      <category>testing</category>
    </item>
    <item>
      <title>Mocks aren’t evil! Better mocking with React Testing Library</title>
      <dc:creator>Daniel Irvine 🏳️‍🌈</dc:creator>
      <pubDate>Mon, 07 Sep 2020 20:19:31 +0000</pubDate>
      <link>https://dev.to/d_ir/mocks-aren-t-evil-better-mocking-with-react-testing-library-3hii</link>
      <guid>https://dev.to/d_ir/mocks-aren-t-evil-better-mocking-with-react-testing-library-3hii</guid>
      <description>&lt;p&gt;Mocks aren’t evil!&lt;/p&gt;

&lt;p&gt;They can help you build simpler, more resilient tests. In this series I’ll show you the patterns I use when writing React component mocks.&lt;/p&gt;

&lt;p&gt;Here’s a quick example of a component mock. I’m using &lt;code&gt;jest.mock&lt;/code&gt; here, which we’ll look at in more detail below.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;jest&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;mock&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;../src/PostContent&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="na"&gt;PostContent&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;jest&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;fn&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="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;div&lt;/span&gt; &lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nx"&gt;testid&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;PostContent&lt;/span&gt;&lt;span class="dl"&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="p"&gt;}))&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;React component mocks don’t get much more complicated than this. The important thing is that it has a very simple stub value (the &lt;code&gt;div&lt;/code&gt;) and a &lt;code&gt;data-testid&lt;/code&gt; attribute that allows us to find the rendered instance very easily in the DOM. By convention, the test ID used is always the same as the component name. In this case, that’s &lt;code&gt;PostContent&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Before we look at how this is used, let’s just recap what mocks are and why you might want to use them.&lt;/p&gt;

&lt;h3&gt;
  
  
  What’s a mock?
&lt;/h3&gt;

&lt;p&gt;In the JavaScript world, the term &lt;em&gt;mock&lt;/em&gt; is very loosely applied to mean any &lt;em&gt;test double&lt;/em&gt;. Test doubles are simply values that replace others in your production code while tests run. They take on the interface of the object they’re replacing so that the rest of your code operates as if it hadn’t been replaced.&lt;/p&gt;

&lt;p&gt;There are a few different reasons why you’d want to do this; we’ll cover them in examples.&lt;/p&gt;

&lt;p&gt;If you’re curious about test doubles in general then I suggest reading Martin Fowler’s &lt;a href="https://martinfowler.com/articles/mocksArentStubs.html"&gt;Mocks Aren't Stubs&lt;/a&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  Jest and mocking
&lt;/h3&gt;

&lt;p&gt;Jest has a function called &lt;code&gt;jest.mock&lt;/code&gt; that allows you to mock out entire modules that you’re replacing. This is what I’m using in this guide, although there are other ways of replacing objects in JavaScript.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;In &lt;a href="https://www.packtpub.com/product/mastering-react-test-driven-development/9781789133417"&gt;Mastering React Test-Driven Development&lt;/a&gt; I use ES6 named module imports to create test doubles. That approaches gives slightly more flexibility but feels slightly more hacky.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;a href="https://jestjs.io/docs/en/manual-mocks"&gt;The Jest page on &lt;code&gt;jest.mock&lt;/code&gt;&lt;/a&gt; says that mocks &lt;em&gt;ensure your tests are fast and not flaky&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;While that’s true, &lt;strong&gt;it’s not the primary reason why I use mocks&lt;/strong&gt;. &lt;/p&gt;

&lt;p&gt;I use mocks because they help me keep my tests &lt;strong&gt;independent&lt;/strong&gt; of each other.&lt;/p&gt;

&lt;p&gt;To see why that is, let’s look at an example.&lt;/p&gt;

&lt;h3&gt;
  
  
  Why mocks?
&lt;/h3&gt;

&lt;p&gt;Below is the listing for the &lt;code&gt;BlogPage&lt;/code&gt; component, which has two jobs: it pulls an &lt;code&gt;id&lt;/code&gt; out of a &lt;code&gt;url&lt;/code&gt; prop and it then renders a &lt;code&gt;PostContent&lt;/code&gt; component with that &lt;code&gt;id&lt;/code&gt;.&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;getPostIdFromUrl&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;url&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt;
  &lt;span class="nx"&gt;url&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;substr&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;url&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;lastIndexOf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;/&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="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;BlogPage&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="nx"&gt;url&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;id&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;getPostIdFromUrl&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;url&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;PostContent&lt;/span&gt; &lt;span class="nx"&gt;id&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;id&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="sr"&gt;/&lt;/span&gt;&lt;span class="err"&gt;&amp;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;Imagine you’ve been writing tests for this component, and all of your tests go into &lt;code&gt;BlogPage.test.js&lt;/code&gt;, which is a single test suite that covers both the &lt;code&gt;BlogPage&lt;/code&gt; and the &lt;code&gt;PostContent&lt;/code&gt; components.&lt;/p&gt;

&lt;p&gt;At this stage you have no need for mocks: we haven’t seen &lt;code&gt;PostContent&lt;/code&gt; yet, but given the size of &lt;code&gt;BlogPage&lt;/code&gt; there’s really no need to have two separate test suites, since &lt;code&gt;BlogPage&lt;/code&gt; is &lt;em&gt;mostly&lt;/em&gt; just &lt;code&gt;PostContent&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;To stretch your imagination further, now pretend that both &lt;code&gt;BlogPage&lt;/code&gt; and &lt;code&gt;PostContent&lt;/code&gt; grow in functionality. You, being the gifted developer that you are, are adding more and more features each day.&lt;/p&gt;

&lt;p&gt;Keeping your tests in a working state is starting to prove difficult. Each new test has more elaborate set up, and the test suite is becoming a time sink. It’s a burden to maintain.&lt;/p&gt;

&lt;p&gt;This is a common problem, and I see it all the time with React codebases. Test suites where even the simplest change causes many tests to break.&lt;/p&gt;

&lt;p&gt;One solution to this is to split the test suites. We’ll keep  &lt;code&gt;BlogPage.test.js&lt;/code&gt; and create a new &lt;code&gt;PostContent.test.js&lt;/code&gt;, which should house tests specifically for behavior in &lt;code&gt;PostContent&lt;/code&gt;. The basic idea is that any functionality that’s housed in &lt;code&gt;PostContent&lt;/code&gt; should be specified in &lt;code&gt;PostContent.test.js&lt;/code&gt;, and any functionality that’s housed in &lt;code&gt;BlogPage&lt;/code&gt; (like the URL parsing) should be in &lt;code&gt;BlogPage.test.js&lt;/code&gt;.&lt;/p&gt;

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

&lt;p&gt;But what if rendering &lt;code&gt;PostContent&lt;/code&gt; has side effects?&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;PostContent&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="nx"&gt;id&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="p"&gt;[&lt;/span&gt; &lt;span class="nx"&gt;text&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;setText&lt;/span&gt; &lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;useState&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;""&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

  &lt;span class="nx"&gt;useEffect&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;fetchPostContent&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;id&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;id&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;fetchPostContent&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;async&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;result&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;fetch&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;`/post?id=&lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;id&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;`&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;result&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;ok&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="nx"&gt;setText&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;result&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;text&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="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;p&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;text&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/p&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;&lt;span class="p"&gt;};&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;The test suite in &lt;code&gt;BlogPage.test.js&lt;/code&gt; needs to be aware of the side effects and be ready to handle them. For example, it will need to have a &lt;code&gt;fetch&lt;/code&gt; response lined up and waiting.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The dependency that we’ve tried to remove by splitting our test suites is still there&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Our test organization is better for sure, but nothing’s ultimately changed that causes our tests to be less brittle.&lt;/p&gt;

&lt;p&gt;For that we need to &lt;strong&gt;stub out&lt;/strong&gt; (or mock) &lt;code&gt;PostContent&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;In the next part we’ll look at how to do that.&lt;/p&gt;

&lt;h3&gt;
  
  
  Is this really necessary?
&lt;/h3&gt;

&lt;p&gt;By the way, this is when you move from the realm of &lt;strong&gt;end-to-end tests&lt;/strong&gt; and into the realm of &lt;strong&gt;unit tests&lt;/strong&gt;.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;The presence of test doubles is a key indicator that you’re writing unit tests.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Many experienced testers will immediately start new projects with unit tests (and mocks) because they know that as their codebase grows they’ll face this problem of test brittleness.&lt;/p&gt;




&lt;p&gt;&lt;em&gt;Unit tests tend to be much smaller than end-to-end tests. So small that they’re often not much more than three or four lines of code. That makes them excellent candidates for &lt;strong&gt;social coding&lt;/strong&gt; practices like pair and ensemble programming.&lt;/em&gt;&lt;/p&gt;




&lt;p&gt;Even when we’re unit testing, test doubles aren’t always necessary—it’s just another tool in your toolbox that you should know when and how to apply.&lt;/p&gt;

&lt;p&gt;In the next part, &lt;a href="https://dev.to/d_ir/the-basic-form-for-react-component-mocks-g5"&gt;I’ll look at basic mocking techniques&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>react</category>
      <category>testing</category>
    </item>
    <item>
      <title>Online me vs offline me</title>
      <dc:creator>Daniel Irvine 🏳️‍🌈</dc:creator>
      <pubDate>Tue, 30 Jun 2020 16:39:28 +0000</pubDate>
      <link>https://dev.to/d_ir/online-me-vs-offline-me-3n3g</link>
      <guid>https://dev.to/d_ir/online-me-vs-offline-me-3n3g</guid>
      <description>&lt;p&gt;&lt;strong&gt;There’s a vast disconnect between the version of me that I present online and the version of me that exists in the real world.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Does anyone else struggle with this?&lt;/p&gt;

&lt;p&gt;When I write stuff online, usually it’s about serious (and yes... boring) technical topics like unit testing and TDD. That’s only fair because that’s something I care about, have a lot of experience with, and is something I can help people with.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;But it’s not who I am.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Most of my work as a software consultant is about &lt;strong&gt;helping people have a happier existence at work.&lt;/strong&gt; It sounds totally ridiculous to say it like that, but it’s true.&lt;/p&gt;

&lt;p&gt;It starts by bringing my whole self to work. My lazy, erratic and often clueless self. And I encourage others to do the same. To be themselves, and to not take their work too seriously.&lt;/p&gt;

&lt;p&gt;And that stuff is hard to write about. Because it’s just... &lt;em&gt;existence&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;It’s the almost unnoticeable words and actions that happen every single day. An attitude to work and life.&lt;/p&gt;

&lt;p&gt;Telling jokes. Laughing together. Watching YouTube videos together while you wait for the build to finish. Talking about what you got up to over the weekend... and being honest about it. Listening.&lt;/p&gt;

&lt;p&gt;Taken on their own, these actions aren’t very interesting to outsiders. But together, they add up to something worth sharing.&lt;/p&gt;

&lt;p&gt;This blog post is brief, but it’s a &lt;em&gt;start&lt;/em&gt;. There will be more to follow!&lt;/p&gt;

&lt;p&gt;🙏&lt;/p&gt;

</description>
      <category>career</category>
      <category>mentalhealth</category>
      <category>life</category>
    </item>
    <item>
      <title>When does TDD make sense?</title>
      <dc:creator>Daniel Irvine 🏳️‍🌈</dc:creator>
      <pubDate>Mon, 29 Jun 2020 12:43:15 +0000</pubDate>
      <link>https://dev.to/d_ir/when-does-tdd-make-sense-3obl</link>
      <guid>https://dev.to/d_ir/when-does-tdd-make-sense-3obl</guid>
      <description>&lt;p&gt;Over the weekend, I noticed this tweet appear in my timeline:&lt;/p&gt;


&lt;blockquote class="ltag__twitter-tweet"&gt;

  &lt;div class="ltag__twitter-tweet__main"&gt;
    &lt;div class="ltag__twitter-tweet__header"&gt;
      &lt;img class="ltag__twitter-tweet__profile-image" src="https://res.cloudinary.com/practicaldev/image/fetch/s--1M-657o0--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://pbs.twimg.com/profile_images/759557613445001216/6M2E1l4q_normal.jpg" alt="Kent C. Dodds 🛰️ profile image"&gt;
      &lt;div class="ltag__twitter-tweet__full-name"&gt;
        Kent C. Dodds 🛰️
      &lt;/div&gt;
      &lt;div class="ltag__twitter-tweet__username"&gt;
        &lt;a class="comment-mentioned-user" href="https://dev.to/kentcdodds"&gt;@kentcdodds&lt;/a&gt;

      &lt;/div&gt;
      &lt;div class="ltag__twitter-tweet__twitter-logo"&gt;
        &lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--P4t6ys1m--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://practicaldev-herokuapp-com.freetls.fastly.net/assets/twitter-f95605061196010f91e64806688390eb1a4dbc9e913682e043eb8b1e06ca484f.svg" alt="twitter logo"&gt;
      &lt;/div&gt;
    &lt;/div&gt;
    &lt;div class="ltag__twitter-tweet__body"&gt;
      I write tests following TDD when it makes sense
    &lt;/div&gt;
    &lt;div class="ltag__twitter-tweet__date"&gt;
      00:28 AM - 27 Jun 2020
    &lt;/div&gt;


    &lt;div class="ltag__twitter-tweet__actions"&gt;
      &lt;a href="https://twitter.com/intent/tweet?in_reply_to=1276673772251017216" class="ltag__twitter-tweet__actions__button"&gt;
        &lt;img src="/assets/twitter-reply-action.svg" alt="Twitter reply action"&gt;
      &lt;/a&gt;
      &lt;a href="https://twitter.com/intent/retweet?tweet_id=1276673772251017216" class="ltag__twitter-tweet__actions__button"&gt;
        &lt;img src="/assets/twitter-retweet-action.svg" alt="Twitter retweet action"&gt;
      &lt;/a&gt;
      16
      &lt;a href="https://twitter.com/intent/like?tweet_id=1276673772251017216" class="ltag__twitter-tweet__actions__button"&gt;
        &lt;img src="/assets/twitter-like-action.svg" alt="Twitter like action"&gt;
      &lt;/a&gt;
      138
    &lt;/div&gt;
  &lt;/div&gt;
&lt;/blockquote&gt;


&lt;p&gt;A bunch of people replied to this tweet asking: &lt;strong&gt;&lt;em&gt;Great! But... when does TDD make sense?&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Especially on the front-end, this question comes up a whole lot. Front-end programming has a legacy of being unsuited for TDD.&lt;/p&gt;

&lt;p&gt;With that in mind, here’s a simple set of guidelines that I use to help answer this question.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;If...&lt;/th&gt;
&lt;th&gt;Then...&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;You’re unsure if TDD makes sense&lt;/td&gt;
&lt;td&gt;Use TDD if you can (see below for why)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;You’re working on a team&lt;/td&gt;
&lt;td&gt;Use TDD&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;You’re writing static pages without behavior, like a marketing site&lt;/td&gt;
&lt;td&gt;Don’t use TDD&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Getting to market &lt;strong&gt;fast&lt;/strong&gt; is more important than quality or maintainability&lt;/td&gt;
&lt;td&gt;Don’t use TDD&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;You’re unsure of what you’re building&lt;/td&gt;
&lt;td&gt;Spike first, then test later&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;You’re working alone on an unreleased project&lt;/td&gt;
&lt;td&gt;Don’t use TDD&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;You’re working alone on a released project with users&lt;/td&gt;
&lt;td&gt;Use TDD&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;Interested in why I arrived at these conclusions? Read on.&lt;/p&gt;

&lt;h3&gt;
  
  
  You’re unsure if TDD makes sense
&lt;/h3&gt;

&lt;p&gt;With enough experience of TDD, you will understand when to apply TDD and when not.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;So if you need to ask the question “When does TDD make sense?”, then TDD makes sense.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;You need more experience. You need to get more practice.&lt;/p&gt;

&lt;p&gt;Practice doesn’t necessarily mean building toy programs. Instead, find opportunities in your daily work to try TDD.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Finding ways to shoe horn TDD into your daily work practice is a whole topic itself&lt;/strong&gt;, so, for now, I’ll suggest just this:&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Start small.&lt;/em&gt; Next time you’ve got to add a new component to your React app, try doing it with TDD.&lt;/p&gt;

&lt;h3&gt;
  
  
  If you’re working on a team
&lt;/h3&gt;

&lt;p&gt;TDD is a structure that helps you articulate and outwardly express ideas that would otherwise be locked in your head.&lt;/p&gt;

&lt;p&gt;That makes it &lt;strong&gt;a great socializing tool for getting feedback early on your designs.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;TDD goes hand-in-hand with pair programming (when you work with someone else) and team programming (sometimes called mob programming, which is when an entire team works together in front of one screen and one keyboard).&lt;/p&gt;

&lt;p&gt;TDD can also give a speed boost to teams.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Ever experienced how slow the code review process can be?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;You wait patiently for feedback and then, when it arrives, you context switch back to make the suggested changes. Then you repeat the cycle. This takes a whole lot of time and it’s mentally (and emotionally!) draining.&lt;/p&gt;

&lt;p&gt;When you apply TDD with others, you pull that feedback process forward, which saves time later on.&lt;/p&gt;

&lt;h3&gt;
  
  
  If you’re writing static pages without behavior, like a marketing site
&lt;/h3&gt;

&lt;p&gt;TDD is about testing behavior. Your marketing site does not have behavior.&lt;/p&gt;

&lt;p&gt;Sure, you might want to add acceptance tests for this code, but that’s a different part of your development process.&lt;/p&gt;

&lt;h3&gt;
  
  
  If getting to market &lt;strong&gt;fast&lt;/strong&gt; is more important than quality or maintainability
&lt;/h3&gt;

&lt;p&gt;If you think following TDD will slow you down, and that in turn will lose you money and market share, then don’t use TDD. You can always come back to add tests later.&lt;/p&gt;

&lt;h3&gt;
  
  
  If you’re unsure of what you’re building
&lt;/h3&gt;

&lt;p&gt;To “spike” is to &lt;em&gt;write code without tests&lt;/em&gt;. Often you do this when you’re exploring a new technical domain for the first time. Spike until the point that you’re confident of the shape of your code. Then you can start adding tests.&lt;/p&gt;

&lt;h3&gt;
  
  
  You’re working alone on an unreleased project
&lt;/h3&gt;

&lt;p&gt;If you’re building a side project that doesn’t have any users yet, and may not ever have any, then I’d avoid using TDD unless you’re specifically aiming to get better at TDD.&lt;/p&gt;

&lt;p&gt;This can be argued two ways. On one hand you could say that TDD will delay finishing, and that translates into a risk that you will never finish.&lt;/p&gt;

&lt;p&gt;On the other hand, you could say that without TDD you’ll end up with low quality software that risks never attaining any users because of the poor quality.&lt;/p&gt;

&lt;p&gt;Of course, there are other ways to have a poor quality product other than simply having buggy software. You could build the wrong thing entirely: in other words, something that no one wants to use.&lt;/p&gt;

&lt;p&gt;So for me at least, the scales tip in favor of not using TDD. Instead, you should focus on getting a finished first version ready.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;However&lt;/strong&gt; if you’re absolutely sure that your project will one day have users, and the use cases and requirements are clear, then start with TDD. It’ll save you time later on.&lt;/p&gt;

&lt;h3&gt;
  
  
  You’re working alone on a released project with users
&lt;/h3&gt;

&lt;p&gt;The moment your side project starts attracting users, then you want to start using TDD in order to save yourself from nasty regressions.&lt;/p&gt;




&lt;p&gt;I’d love to hear feedback if you agree, disagree, or if this was in any way useful. Go ahead and leave a comment below.&lt;/p&gt;

&lt;p&gt;Happy coding! 🙏&lt;/p&gt;

</description>
      <category>testing</category>
      <category>react</category>
      <category>javascript</category>
    </item>
    <item>
      <title>React developers: TDD is not dogma</title>
      <dc:creator>Daniel Irvine 🏳️‍🌈</dc:creator>
      <pubDate>Fri, 26 Jun 2020 10:55:19 +0000</pubDate>
      <link>https://dev.to/d_ir/react-developers-tdd-is-not-dogma-50oj</link>
      <guid>https://dev.to/d_ir/react-developers-tdd-is-not-dogma-50oj</guid>
      <description>&lt;p&gt;&lt;em&gt;Photo by &lt;a href="https://unsplash.com/photos/14ocb5uMI1Y"&gt;Tomek Baginski on Unsplash&lt;/a&gt;&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;——&lt;/p&gt;

&lt;p&gt;&lt;em&gt;&lt;strong&gt;Update&lt;/strong&gt;: Kent replied to this article and was kind enough to explicitly tweet the following about TDD, which then provoked a great discussion from his followers. Thank you Kent!&lt;/em&gt;&lt;/p&gt;


&lt;blockquote class="ltag__twitter-tweet"&gt;

  &lt;div class="ltag__twitter-tweet__main"&gt;
    &lt;div class="ltag__twitter-tweet__header"&gt;
      &lt;img class="ltag__twitter-tweet__profile-image" src="https://res.cloudinary.com/practicaldev/image/fetch/s--1M-657o0--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://pbs.twimg.com/profile_images/759557613445001216/6M2E1l4q_normal.jpg" alt="Kent C. Dodds 🛰️ profile image"&gt;
      &lt;div class="ltag__twitter-tweet__full-name"&gt;
        Kent C. Dodds 🛰️
      &lt;/div&gt;
      &lt;div class="ltag__twitter-tweet__username"&gt;
        &lt;a class="comment-mentioned-user" href="https://dev.to/kentcdodds"&gt;@kentcdodds&lt;/a&gt;

      &lt;/div&gt;
      &lt;div class="ltag__twitter-tweet__twitter-logo"&gt;
        &lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--P4t6ys1m--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://practicaldev-herokuapp-com.freetls.fastly.net/assets/twitter-f95605061196010f91e64806688390eb1a4dbc9e913682e043eb8b1e06ca484f.svg" alt="twitter logo"&gt;
      &lt;/div&gt;
    &lt;/div&gt;
    &lt;div class="ltag__twitter-tweet__body"&gt;
      I write tests following TDD when it makes sense
    &lt;/div&gt;
    &lt;div class="ltag__twitter-tweet__date"&gt;
      00:28 AM - 27 Jun 2020
    &lt;/div&gt;


    &lt;div class="ltag__twitter-tweet__actions"&gt;
      &lt;a href="https://twitter.com/intent/tweet?in_reply_to=1276673772251017216" class="ltag__twitter-tweet__actions__button"&gt;
        &lt;img src="/assets/twitter-reply-action.svg" alt="Twitter reply action"&gt;
      &lt;/a&gt;
      &lt;a href="https://twitter.com/intent/retweet?tweet_id=1276673772251017216" class="ltag__twitter-tweet__actions__button"&gt;
        &lt;img src="/assets/twitter-retweet-action.svg" alt="Twitter retweet action"&gt;
      &lt;/a&gt;
      16
      &lt;a href="https://twitter.com/intent/like?tweet_id=1276673772251017216" class="ltag__twitter-tweet__actions__button"&gt;
        &lt;img src="/assets/twitter-like-action.svg" alt="Twitter like action"&gt;
      &lt;/a&gt;
      138
    &lt;/div&gt;
  &lt;/div&gt;
&lt;/blockquote&gt;


&lt;p&gt;&lt;em&gt;The original article is below.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;——&lt;/p&gt;

&lt;p&gt;A couple of days ago, React’s leading educator on testing wrote a tweet about &lt;strong&gt;test-driven development (TDD)&lt;/strong&gt; that displayed a common misunderstanding:&lt;/p&gt;


&lt;blockquote class="ltag__twitter-tweet"&gt;

  &lt;div class="ltag__twitter-tweet__main"&gt;
    &lt;div class="ltag__twitter-tweet__header"&gt;
      &lt;img class="ltag__twitter-tweet__profile-image" src="https://res.cloudinary.com/practicaldev/image/fetch/s--1M-657o0--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://pbs.twimg.com/profile_images/759557613445001216/6M2E1l4q_normal.jpg" alt="Kent C. Dodds 🪐 profile image"&gt;
      &lt;div class="ltag__twitter-tweet__full-name"&gt;
        Kent C. Dodds 🪐
      &lt;/div&gt;
      &lt;div class="ltag__twitter-tweet__username"&gt;
        &lt;a class="comment-mentioned-user" href="https://dev.to/kentcdodds"&gt;@kentcdodds&lt;/a&gt;

      &lt;/div&gt;
      &lt;div class="ltag__twitter-tweet__twitter-logo"&gt;
        &lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--P4t6ys1m--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://practicaldev-herokuapp-com.freetls.fastly.net/assets/twitter-f95605061196010f91e64806688390eb1a4dbc9e913682e043eb8b1e06ca484f.svg" alt="twitter logo"&gt;
      &lt;/div&gt;
    &lt;/div&gt;
    &lt;div class="ltag__twitter-tweet__body"&gt;
      Please don't take this the wrong way. I do know the difference between BDD and TDD and I've implemented tests in both. I just don't normally think about these methodologies or dogmatically subscribe to them.
    &lt;/div&gt;
    &lt;div class="ltag__twitter-tweet__date"&gt;
      01:22 AM - 24 Jun 2020
    &lt;/div&gt;


    &lt;div class="ltag__twitter-tweet__actions"&gt;
      &lt;a href="https://twitter.com/intent/tweet?in_reply_to=1275600046508007424" class="ltag__twitter-tweet__actions__button"&gt;
        &lt;img src="/assets/twitter-reply-action.svg" alt="Twitter reply action"&gt;
      &lt;/a&gt;
      &lt;a href="https://twitter.com/intent/retweet?tweet_id=1275600046508007424" class="ltag__twitter-tweet__actions__button"&gt;
        &lt;img src="/assets/twitter-retweet-action.svg" alt="Twitter retweet action"&gt;
      &lt;/a&gt;
      1
      &lt;a href="https://twitter.com/intent/like?tweet_id=1275600046508007424" class="ltag__twitter-tweet__actions__button"&gt;
        &lt;img src="/assets/twitter-like-action.svg" alt="Twitter like action"&gt;
      &lt;/a&gt;
      26
    &lt;/div&gt;
  &lt;/div&gt;
&lt;/blockquote&gt;


&lt;p&gt;This is a back-handed compliment that relies on the belief that &lt;strong&gt;TDD is dogma&lt;/strong&gt; (and if you read the parent tweets, also on the belief that using TDD/BDD doesn’t result in valuable tests).&lt;/p&gt;

&lt;p&gt;I’m highlighting this because I worry that a whole generation of programmers will miss out on learning a valuable skill because of the strong words from their respected peers.&lt;/p&gt;

&lt;p&gt;I worry that people will read this and think “Kent doesn’t use TDD, therefore TDD must be bad.”&lt;/p&gt;

&lt;p&gt;It is also a risk to the ongoing adoption of React if the community fails to embrace an important industry technique.&lt;/p&gt;

&lt;p&gt;Personally speaking, &lt;strong&gt;using TDD gave me a huge boost in my career as a software developer.&lt;/strong&gt; Without TDD I wouldn’t be a successful independent consultant. It has helped me write high-quality Windows desktop applications in C#. It has helped me build backend monoliths and microservices in Ruby, Java and Clojure. And of course, I’ve also used it with success on the frontend: not just with React but other frameworks, with vanilla JavaScript, and with ClojureScript too.&lt;/p&gt;

&lt;h2&gt;
  
  
  TDD is here to stay
&lt;/h2&gt;

&lt;p&gt;TDD has been around &lt;strong&gt;for decades&lt;/strong&gt; and is a natural progression for developers who are interested in testing. &lt;/p&gt;

&lt;p&gt;When you start writing tests for the first time, you are bound to make mistakes. The more mistakes you make, the more you learn about “good” testing practice. Eventually you learn about TDD, and suddenly you become slightly embarrassed about everything you did before.&lt;/p&gt;

&lt;h2&gt;
  
  
  TDD is anti-dogma
&lt;/h2&gt;

&lt;p&gt;The TDD process is &lt;em&gt;not&lt;/em&gt; dogma. In fact, learning to &lt;em&gt;cheat&lt;/em&gt; at TDD is part of the fun of TDD. (But you can’t cheat until you’ve practiced the game so much that you know the rules by heart.)&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;TDD is &lt;em&gt;disciplined&lt;/em&gt;.&lt;/strong&gt; TDD says that every single line of code your write must be written with thought and care.&lt;/p&gt;

&lt;p&gt;The reason that TDD is anti-dogma is that &lt;strong&gt;if you apply TDD, then you can write your code whichever way you like. Nothing is out of bounds.&lt;/strong&gt; As long as the tests pass, you’re good.&lt;/p&gt;

&lt;p&gt;And if the tests are passing, you’re free to refactor your code however you like.&lt;/p&gt;

&lt;h2&gt;
  
  
  TDD is social
&lt;/h2&gt;

&lt;p&gt;TDD, combined with pair and team programming, offers a process that allows teams to combat some nefarious team problems: siloed development and overly-complex code are two that spring to mind.&lt;/p&gt;

&lt;p&gt;TDD helps a team perform at a consistent rate. In agile parlance, people talk about consistent velocity being a sign of a high-performing team, and that’s what TDD helps teams achieve.&lt;/p&gt;

&lt;h2&gt;
  
  
  TDD is for beginners, too
&lt;/h2&gt;

&lt;p&gt;There are coding bootcamps--like &lt;a href="https://makers.tech"&gt;Makers&lt;/a&gt; in London--that teach TDD to new recruits. Group learning combined with tuition from experts is a fabulous way to learn TDD. And developers who graduate from TDD bootcamps are highly sought after because of their skillset.&lt;/p&gt;

&lt;h2&gt;
  
  
  Our community is full of dogma. Be careful!
&lt;/h2&gt;

&lt;p&gt;The React testing community is actually very fond of dogma: &lt;em&gt;Stop mocking fetch! Don’t use nested describe blocks!&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;To be clear, &lt;strong&gt;this is what dogma looks like&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;One trick that has kept me well informed over the years is remembering that &lt;strong&gt;anytime someone tells you categorically not to do something, they are stating an opinion based on their lived experience, not fact.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Unfortunately in computing, it’s very common to see developers conflate something that is valid for their lived experience with what is valid for everyone. &lt;em&gt;“This didn’t work for me, therefore it can’t work for anyone!”&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;The best we can do as teachers is to say: &lt;strong&gt;“Here is something that works for me, and here’s how I like to do it.”&lt;/strong&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  I’m not asking you to use TDD
&lt;/h2&gt;

&lt;p&gt;Remember that in the grand scheme of things, the React community is still an infant. It’s still learning about the world. It has much to learn from the rest of the industry.&lt;/p&gt;

&lt;p&gt;I’m not asking you to use TDD. &lt;strong&gt;I’m asking you to keep an open mind.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Happy coding! 🙏&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>testing</category>
      <category>react</category>
    </item>
    <item>
      <title>Here’s how to test arrays</title>
      <dc:creator>Daniel Irvine 🏳️‍🌈</dc:creator>
      <pubDate>Thu, 05 Mar 2020 18:13:05 +0000</pubDate>
      <link>https://dev.to/d_ir/here-s-how-to-test-arrays-318b</link>
      <guid>https://dev.to/d_ir/here-s-how-to-test-arrays-318b</guid>
      <description>&lt;p&gt;If I give you a function that returns an array of objects, how do you test the returned value? How many unit tests do you need? One, two, three? What are their descriptions?&lt;/p&gt;

&lt;p&gt;In this post I’ll show you how I do it.&lt;/p&gt;

&lt;p&gt;It follows on from my post last week about how to test dependencies. It’s written in Ruby but it applies to any dynamic language, including JavaScript. (Statically typed languages don’t necessarily need these types of test).&lt;/p&gt;


&lt;div class="ltag__link"&gt;
  &lt;a href="/d_ir" class="ltag__link__link"&gt;
    &lt;div class="ltag__link__pic"&gt;
      &lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F3314%2F9755992b-b127-49e3-9c2a-54c891cb250b.jpg" alt="d_ir"&gt;
    &lt;/div&gt;
  &lt;/a&gt;
  &lt;a href="/d_ir/here-s-how-to-test-dependencies-with-mocks-ia6" class="ltag__link__link"&gt;
    &lt;div class="ltag__link__content"&gt;
      &lt;h2&gt;Here’s how to test dependencies with mocks&lt;/h2&gt;
      &lt;h3&gt;Daniel Irvine 🏳️‍🌈 ・ Feb 29 '20&lt;/h3&gt;
      &lt;div class="ltag__link__taglist"&gt;
        &lt;span class="ltag__link__tag"&gt;#ruby&lt;/span&gt;
        &lt;span class="ltag__link__tag"&gt;#testing&lt;/span&gt;
        &lt;span class="ltag__link__tag"&gt;#tdd&lt;/span&gt;
      &lt;/div&gt;
    &lt;/div&gt;
  &lt;/a&gt;
&lt;/div&gt;


&lt;blockquote&gt;
&lt;p&gt;These posts are all about &lt;strong&gt;systemizing your testing&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Testing gets kind of &lt;em&gt;boring&lt;/em&gt; once you have a system for it. But boring is good. Boring produces consistency. It means you aren’t getting stuck, or having to &lt;em&gt;think&lt;/em&gt; too much.&lt;/p&gt;

&lt;p&gt;Every time I have to test a returned array, I use this exact same approach.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  Test 1. The right number of elements are returned
&lt;/h2&gt;

&lt;p&gt;Imagine we’re testing a &lt;code&gt;filter&lt;/code&gt; function that takes a source array of objects and a string to match on. Our tests will define the behavior of the function.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;

&lt;span class="no"&gt;RSpec&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;describe&lt;/span&gt; &lt;span class="s2"&gt;"#filter"&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt;
  &lt;span class="n"&gt;let&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="ss"&gt;:source&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt;
    &lt;span class="p"&gt;[&lt;/span&gt;
      &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="ss"&gt;id: &lt;/span&gt;&lt;span class="mi"&gt;123&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;name: &lt;/span&gt;&lt;span class="s2"&gt;"Daniel"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;diet: :vegan&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;pet: :cat&lt;/span&gt; &lt;span class="p"&gt;},&lt;/span&gt;
      &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="ss"&gt;id: &lt;/span&gt;&lt;span class="mi"&gt;234&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;name: &lt;/span&gt;&lt;span class="s2"&gt;"Danny"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;diet: :anything&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;pet: :goldfish&lt;/span&gt; &lt;span class="p"&gt;},&lt;/span&gt;
      &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="ss"&gt;id: &lt;/span&gt;&lt;span class="mi"&gt;345&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;name: &lt;/span&gt;&lt;span class="s2"&gt;"Jane"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;diet: :vegetarian&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;pet: &lt;/span&gt;&lt;span class="kp"&gt;nil&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;]&lt;/span&gt;
  &lt;span class="k"&gt;end&lt;/span&gt;

  &lt;span class="n"&gt;it&lt;/span&gt; &lt;span class="s2"&gt;"returns all elements matching the name parameter"&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt;
    &lt;span class="n"&gt;result&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;filter&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;source&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"Dan"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;expect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;result&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;length&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;to&lt;/span&gt; &lt;span class="n"&gt;eq&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;
    &lt;span class="n"&gt;expect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;result&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="ss"&gt;:id&lt;/span&gt;&lt;span class="p"&gt;]).&lt;/span&gt;&lt;span class="nf"&gt;to&lt;/span&gt; &lt;span class="n"&gt;eq&lt;/span&gt; &lt;span class="mi"&gt;123&lt;/span&gt;
    &lt;span class="n"&gt;expect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;result&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="ss"&gt;:id&lt;/span&gt;&lt;span class="p"&gt;]).&lt;/span&gt;&lt;span class="nf"&gt;to&lt;/span&gt; &lt;span class="n"&gt;eq&lt;/span&gt; &lt;span class="mi"&gt;234&lt;/span&gt;
  &lt;span class="k"&gt;end&lt;/span&gt;

&lt;span class="k"&gt;end&lt;/span&gt;


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

&lt;/div&gt;

&lt;p&gt;Here my test list has 3 items: you can normally prove everything with just items. No need for a longer list.&lt;/p&gt;

&lt;p&gt;Now let’s look at how we might make that test pass.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;

&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;filter&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;source&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nb"&gt;name&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="n"&gt;source&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;select&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt;&lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="ss"&gt;:name&lt;/span&gt;&lt;span class="p"&gt;].&lt;/span&gt;&lt;span class="nf"&gt;include?&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;name&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;


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

&lt;/div&gt;
&lt;h2&gt;
  
  
  Test 2.  That it returns the correct form of object
&lt;/h2&gt;

&lt;p&gt;In the second test you want to ensure you test that you get all the properties that you’re expecting. Occassionally this test isn’t necessary: &lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;

&lt;span class="n"&gt;it&lt;/span&gt; &lt;span class="s2"&gt;"returns all the right information"&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt;
  &lt;span class="n"&gt;result&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;filter&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;source&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"Dan"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="n"&gt;expect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;result&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;first&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;to&lt;/span&gt; &lt;span class="n"&gt;eq&lt;/span&gt; &lt;span class="n"&gt;source&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="k"&gt;end&lt;/span&gt;


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

&lt;/div&gt;

&lt;p&gt;This test already passes, so we don’t need to write it. But if you aren’t returning the exact item from the source, then write out the value you expect instead:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;

&lt;span class="n"&gt;it&lt;/span&gt; &lt;span class="s2"&gt;"returns all the right information"&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt;
  &lt;span class="n"&gt;result&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;filter&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;source&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"Dan"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="n"&gt;expect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;result&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;first&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;to&lt;/span&gt; &lt;span class="n"&gt;eq&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
    &lt;span class="ss"&gt;name: &lt;/span&gt;&lt;span class="s2"&gt;"Daniel"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="ss"&gt;pet: :cat&lt;/span&gt;
  &lt;span class="p"&gt;})&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;


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

&lt;/div&gt;

&lt;p&gt;Now we actually have to write code to make that pass:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;

&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;filter&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;source&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nb"&gt;name&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="n"&gt;source&lt;/span&gt;
    &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;select&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt;&lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="ss"&gt;:name&lt;/span&gt;&lt;span class="p"&gt;].&lt;/span&gt;&lt;span class="nf"&gt;include?&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;name&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="nf"&gt;map&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt;&lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;except&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="ss"&gt;:diet&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;


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

&lt;/div&gt;
&lt;h1&gt;
  
  
  Test 3. That they’re in the right order
&lt;/h1&gt;

&lt;p&gt;Sometimes, you care about the order that elements are returned. For this we need a new source list, again with at least three elements:&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;

&lt;span class="n"&gt;let&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="ss"&gt;:unsorted_source&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt;
    &lt;span class="p"&gt;[&lt;/span&gt;
      &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="ss"&gt;id: &lt;/span&gt;&lt;span class="mi"&gt;123&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;name: &lt;/span&gt;&lt;span class="s2"&gt;"Daniel"&lt;/span&gt; &lt;span class="p"&gt;},&lt;/span&gt;
      &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="ss"&gt;id: &lt;/span&gt;&lt;span class="mi"&gt;234&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;name: &lt;/span&gt;&lt;span class="s2"&gt;"Dan"&lt;/span&gt; &lt;span class="p"&gt;},&lt;/span&gt;
      &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="ss"&gt;id: &lt;/span&gt;&lt;span class="mi"&gt;345&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;name: &lt;/span&gt;&lt;span class="s2"&gt;"Danny"&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;

&lt;span class="n"&gt;it&lt;/span&gt; &lt;span class="s2"&gt;"orders results alphabetically"&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt;
  &lt;span class="n"&gt;result&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;filter&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;unsorted_source&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"Dan"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="n"&gt;expect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;result&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="ss"&gt;:name&lt;/span&gt;&lt;span class="p"&gt;]).&lt;/span&gt;&lt;span class="nf"&gt;to&lt;/span&gt; &lt;span class="n"&gt;eq&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"Dan"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="n"&gt;expect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;result&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="ss"&gt;:name&lt;/span&gt;&lt;span class="p"&gt;]).&lt;/span&gt;&lt;span class="nf"&gt;to&lt;/span&gt; &lt;span class="n"&gt;eq&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"Daniel"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="n"&gt;expect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;result&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="ss"&gt;:name&lt;/span&gt;&lt;span class="p"&gt;]).&lt;/span&gt;&lt;span class="nf"&gt;to&lt;/span&gt; &lt;span class="n"&gt;eq&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"Danny"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;


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

&lt;/div&gt;

&lt;p&gt;The elements must be in an unsorted order: neither ascending nor descending order. Otherwise, you could make this test pass simply by returning the same array or by calling &lt;code&gt;reverse&lt;/code&gt; rather than &lt;code&gt;sort_by&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;And here’s the final result:&lt;/p&gt;

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

def filter(source, name)
  source
    .select { |s| s[:name].include?(name) }
    .map { |s| s.except(:diet) }
    .sort_by { |s| s[:name] }
end


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

&lt;/div&gt;
&lt;h2&gt;
  
  
  Modification 1: Join tests 1 and 3
&lt;/h2&gt;

&lt;p&gt;If I know I’m going to order my tests, I’ll join 1 and 3 together.&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;

&lt;span class="n"&gt;it&lt;/span&gt; &lt;span class="s2"&gt;"returns all elements matching the name, in alphabetical order"&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt;
  &lt;span class="n"&gt;result&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;filter&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;unsorted_source&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"Dan"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="n"&gt;expect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;result&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="ss"&gt;:id&lt;/span&gt;&lt;span class="p"&gt;]).&lt;/span&gt;&lt;span class="nf"&gt;to&lt;/span&gt; &lt;span class="n"&gt;eq&lt;/span&gt; &lt;span class="mi"&gt;234&lt;/span&gt;
  &lt;span class="n"&gt;expect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;result&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="ss"&gt;:id&lt;/span&gt;&lt;span class="p"&gt;]).&lt;/span&gt;&lt;span class="nf"&gt;to&lt;/span&gt; &lt;span class="n"&gt;eq&lt;/span&gt; &lt;span class="mi"&gt;123&lt;/span&gt;
  &lt;span class="n"&gt;expect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;result&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="ss"&gt;:id&lt;/span&gt;&lt;span class="p"&gt;]).&lt;/span&gt;&lt;span class="nf"&gt;to&lt;/span&gt; &lt;span class="n"&gt;eq&lt;/span&gt; &lt;span class="mi"&gt;345&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;


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

&lt;/div&gt;

&lt;p&gt;Or perhaps you’d rather use &lt;code&gt;map&lt;/code&gt;:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;

&lt;span class="n"&gt;it&lt;/span&gt; &lt;span class="s2"&gt;"returns all elements matching the name, in alphabetical order"&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt;
  &lt;span class="n"&gt;result&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;filter&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;unsorted_source&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"Dan"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="n"&gt;expect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;result&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;map&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt;&lt;span class="n"&gt;r&lt;/span&gt;&lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="n"&gt;r&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="ss"&gt;:id&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="p"&gt;}).&lt;/span&gt;&lt;span class="nf"&gt;to&lt;/span&gt; &lt;span class="n"&gt;match_array&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;234&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;123&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;345&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;


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

&lt;/div&gt;

&lt;p&gt;And that’s all there is to it! 🎉&lt;/p&gt;

</description>
      <category>ruby</category>
      <category>testing</category>
      <category>tdd</category>
    </item>
    <item>
      <title>Yet another reason to prefer unit tests 😇</title>
      <dc:creator>Daniel Irvine 🏳️‍🌈</dc:creator>
      <pubDate>Tue, 03 Mar 2020 22:24:08 +0000</pubDate>
      <link>https://dev.to/d_ir/yet-another-reason-to-prefer-unit-tests-jdh</link>
      <guid>https://dev.to/d_ir/yet-another-reason-to-prefer-unit-tests-jdh</guid>
      <description>&lt;p&gt;Have you ever gotten so frustrated when writing a unit test that you gave up and wrote an end-to-end test instead?&lt;/p&gt;

&lt;p&gt;I think this is a common occurrence in the JavaScript world. Codebases become complex very quickly, and then (unsurprisingly) unit tests become complex too.&lt;/p&gt;

&lt;p&gt;It seems to me that this is one reason why people prefer end-to-end tests over unit tests: because unit tests can be difficult and frustrating.&lt;/p&gt;

&lt;p&gt;But this is precisely why unit tests are useful: &lt;strong&gt;unit tests give you design feedback.&lt;/strong&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;If your unit test is hard to write, then that’s a good thing: the test is telling you that your design can be improved.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;When you’re faced with a hard-to-write unit test, the solution is &lt;strong&gt;not&lt;/strong&gt; to rewrite your test to be an end-to-end test. The solution is to refactor your production code until the unit test becomes easy to write.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;A unit test is like a clamp that holds your system in place while you refactor around it.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;If you’re puzzled as to what I’m talking about, then perhaps you would do well to read a book like Kent Beck’s &lt;a href="https://www.informit.com/store/implementation-patterns-9780321413093"&gt;Implementation Patterns&lt;/a&gt; or &lt;a href="https://www.sandimetz.com/99bottles"&gt;99 Bottles of OOP&lt;/a&gt; by Sandi Metz. (And if you’re a React developer, consider reading my book, &lt;a href="https://www.packtpub.com/gb/web-development/mastering-react-test-driven-development"&gt;Mastering React Test-Driven Development&lt;/a&gt;.)&lt;/p&gt;

&lt;p&gt;Most developers are well aware of how to make their own code simpler. And yet they don’t do it.&lt;/p&gt;

&lt;p&gt;Very often, developers don’t have enough trust in themselves to refactor safely. They fear causing unexpected regressions. They worry that they’ll spend all day refactoring and they won’t make progress on their assigned work.&lt;/p&gt;

&lt;p&gt;If that’s you, try giving unit testing another go. &lt;em&gt;Listen&lt;/em&gt; to the tests and let them give you the structure you need to make positive change to your codebase.&lt;/p&gt;

</description>
      <category>testing</category>
      <category>javascript</category>
      <category>react</category>
      <category>tdd</category>
    </item>
    <item>
      <title>Here’s how to test dependencies with mocks</title>
      <dc:creator>Daniel Irvine 🏳️‍🌈</dc:creator>
      <pubDate>Sat, 29 Feb 2020 14:58:16 +0000</pubDate>
      <link>https://dev.to/d_ir/here-s-how-to-test-dependencies-with-mocks-ia6</link>
      <guid>https://dev.to/d_ir/here-s-how-to-test-dependencies-with-mocks-ia6</guid>
      <description>&lt;p&gt;In this post I’ll present a straightforward process for testing dependencies. No matter which language, framework or unit testing tool you’re using, you can rely on this process to fully test your dependency use.&lt;/p&gt;

&lt;p&gt;I’m using Ruby for my code samples but they apply in just about every modern programming language.&lt;/p&gt;

&lt;p&gt;Let’s look at an example of a class with a dependency.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;FooRepository&lt;/span&gt;
  &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;save&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;docoment&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="c1"&gt;# ...&lt;/span&gt;
  &lt;span class="k"&gt;end&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;

&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Foo&lt;/span&gt;
  &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;initialize&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;repository&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="vi"&gt;@repository&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;repository&lt;/span&gt;
  &lt;span class="k"&gt;end&lt;/span&gt;

  &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;do_something&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;document&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="vi"&gt;@repository&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;save&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'Foo'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;document&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="k"&gt;end&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;In this example &lt;code&gt;repository&lt;/code&gt; is the dependency. We’re going to write tests for the &lt;code&gt;do_something&lt;/code&gt; function.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;A &lt;strong&gt;dependency&lt;/strong&gt; of a code unit (an object, or a function) is another code unit that is executed as part of the containing code unit’s execution.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;In an object-oriented environment we often see dependencies passed into the class constructor, as in the example above. But functions can have dependencies too.&lt;/p&gt;

&lt;p&gt;In this case, &lt;code&gt;Foo&lt;/code&gt; does not &lt;em&gt;create&lt;/em&gt; an instance of &lt;code&gt;repository&lt;/code&gt;. It does not control the &lt;strong&gt;lifetime&lt;/strong&gt; of the dependency. In fact, it knows nothing more about &lt;code&gt;repository&lt;/code&gt; than these two facts:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;it has a method named &lt;code&gt;save&lt;/code&gt; that takes two arguments: a string and a &lt;code&gt;document&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;it returns either nil or something else (we don’t care what else)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;And it’s these two facts that lead our testing.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Every time we test a dependency, we need at least these two tests:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;that it was invoked with the right parameters&lt;/li&gt;
&lt;li&gt;that the return value was used correctly&lt;/li&gt;
&lt;/ol&gt;
&lt;/blockquote&gt;

&lt;h1&gt;
  
  
  Test 1: the dependency is invoked with the right parameters
&lt;/h1&gt;

&lt;p&gt;This example uses some special RSpec syntax that you may not be familiar with, which I’ll explain after:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="nb"&gt;require&lt;/span&gt; &lt;span class="s1"&gt;'foo'&lt;/span&gt;

&lt;span class="no"&gt;RSpec&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;describe&lt;/span&gt; &lt;span class="no"&gt;Foo&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt;

  &lt;span class="n"&gt;let&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="ss"&gt;:repository&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;spy&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="n"&gt;subject&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt;
    &lt;span class="n"&gt;described_class&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;new&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;repository&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="k"&gt;end&lt;/span&gt;

  &lt;span class="n"&gt;it&lt;/span&gt; &lt;span class="s1"&gt;'calls repository#save with the name and document'&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt;
    &lt;span class="n"&gt;subject&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;do_something&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="ss"&gt;:document&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

    &lt;span class="n"&gt;expect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;repository&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;to&lt;/span&gt; &lt;span class="n"&gt;have_received&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="ss"&gt;:save&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
      &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;with&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'Foo'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;:document&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="k"&gt;end&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;If you’re not familiar with RSpec, here are three points that should help explain this example.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Most importantly, &lt;code&gt;spy&lt;/code&gt; is a function call that creates a new mock object.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;described_class&lt;/code&gt; and &lt;code&gt;subject&lt;/code&gt; are conventional ways to refer to the &lt;strong&gt;subject under test&lt;/strong&gt;. In this case &lt;code&gt;described_class&lt;/code&gt; is &lt;code&gt;Foo&lt;/code&gt; and &lt;code&gt;subject&lt;/code&gt; is an instance of &lt;code&gt;Foo&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;More subtly, &lt;code&gt;:document&lt;/code&gt; is a symbol that allows us to give a name to a value &lt;em&gt;but we don’t care about the value itself&lt;/em&gt;. More on this later.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Essentially what we do is place a &lt;strong&gt;spy object&lt;/strong&gt;—a type of test double which records its invocations—where the dependency should be. We then invoke the method we’re testing, and then check the spy’s records to ensure that it was indeed invoked.&lt;/p&gt;

&lt;h2&gt;
  
  
  Test 2: the dependency’s return value is used correctly
&lt;/h2&gt;

&lt;p&gt;If you’re unfamiliar with Ruby, you may look at the code above and think that we simply throw away the return value. But this isn’t true. In Ruby, the returned value of a function is the value of the last expression, which in this case is the returned value of the call to &lt;code&gt;save&lt;/code&gt;. There implicity behavior here that we’re relying on.&lt;/p&gt;

&lt;p&gt;To test, we &lt;strong&gt;stub&lt;/strong&gt; a return value using the &lt;code&gt;allow&lt;/code&gt; function.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="n"&gt;it&lt;/span&gt; &lt;span class="s1"&gt;'returns the result of calling repository#save'&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt;
  &lt;span class="n"&gt;allow&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;repository&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;to&lt;/span&gt; &lt;span class="n"&gt;receive&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="ss"&gt;:save&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;and_return&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="ss"&gt;:saved_document&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

  &lt;span class="n"&gt;expect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;subject&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;do_something&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="ss"&gt;:document&lt;/span&gt;&lt;span class="p"&gt;)).&lt;/span&gt;&lt;span class="nf"&gt;to&lt;/span&gt; &lt;span class="n"&gt;be&lt;/span&gt; &lt;span class="ss"&gt;:saved_document&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Notice again that it’s not the value of the return value that’s important, but the &lt;em&gt;identity&lt;/em&gt;. We want to check that it’s the same value we passed in. So we can use a symbol, &lt;code&gt;:saved_document&lt;/code&gt; to do that without having to define an extra variable.&lt;/p&gt;

&lt;h2&gt;
  
  
  Modification 1: Protecting against dependency signature changes in dynamic languages
&lt;/h2&gt;

&lt;p&gt;Imagine now that we rename the &lt;code&gt;save&lt;/code&gt; method of &lt;code&gt;repository&lt;/code&gt; to &lt;code&gt;save_document&lt;/code&gt;, but we forget to update &lt;code&gt;Foo&lt;/code&gt;: it still calls &lt;code&gt;save&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;FooRepository&lt;/span&gt;
  &lt;span class="c1"&gt;# renamed from save&lt;/span&gt;
  &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;save_document&lt;/span&gt;
    &lt;span class="o"&gt;...&lt;/span&gt;
  &lt;span class="k"&gt;end&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;

&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Foo&lt;/span&gt;
  &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;do_something&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;document&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="vi"&gt;@repository&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;save&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'Foo'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;document&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;# ERROR: still calls save!&lt;/span&gt;
  &lt;span class="k"&gt;end&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Because Ruby doesn’t do any static analysis of our functions, our tests will still pass even though &lt;code&gt;save&lt;/code&gt; no longer exists. This code will break at runtime.&lt;/p&gt;

&lt;p&gt;Wouldn’t it be great if we could make our tests fail when the dependency method signature changes?&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Failure/Error:
  allow(repository).to receive(:save).and_return(:saved_document)

  the FooRepository class does not implement the instance method: save
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;We can do that by replacing the call to &lt;code&gt;spy&lt;/code&gt; with &lt;code&gt;instance_spy&lt;/code&gt;, which produces a &lt;strong&gt;verifying double&lt;/strong&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="n"&gt;let&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="ss"&gt;:repository&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;instance_spy&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="no"&gt;FooRepository&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 isn’t unique to RSpec: all good mocking frameworks will support this functionality.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;A verifying double will break your test if you try to stub a method that doesn’t exist on the original class.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  Modification 2: Add a test for every new branch
&lt;/h2&gt;

&lt;p&gt;Now imagine that &lt;code&gt;do_something&lt;/code&gt; looks like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;do_something&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;document&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="n"&gt;result&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="vi"&gt;@repository&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;save&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'Foo'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;document&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="ss"&gt;:ok&lt;/span&gt; &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;result&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;This code changes the return value of &lt;code&gt;do_something&lt;/code&gt; depending on the returned value of save. If &lt;code&gt;save&lt;/code&gt; returns anything, then the result is &lt;code&gt;:ok&lt;/code&gt;, otherwise the return value is &lt;code&gt;nil&lt;/code&gt;. Because of that, we now need &lt;em&gt;two&lt;/em&gt; tests for the return value; one test for each branch.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="n"&gt;it&lt;/span&gt; &lt;span class="s1"&gt;'returns :ok when repository#save returns something'&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt;
  &lt;span class="n"&gt;allow&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;repository&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;to&lt;/span&gt; &lt;span class="n"&gt;receive&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="ss"&gt;:save&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;and_return&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="ss"&gt;:saved_document&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

  &lt;span class="n"&gt;expect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;subject&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;do_something&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="ss"&gt;:document&lt;/span&gt;&lt;span class="p"&gt;)).&lt;/span&gt;&lt;span class="nf"&gt;to&lt;/span&gt; &lt;span class="n"&gt;be&lt;/span&gt; &lt;span class="ss"&gt;:ok&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;

&lt;span class="n"&gt;it&lt;/span&gt; &lt;span class="s1"&gt;'returns nil when repository#save returns nil'&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt;
  &lt;span class="n"&gt;allow&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;repository&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;to&lt;/span&gt; &lt;span class="n"&gt;receive&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="ss"&gt;:save&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;and_return&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kp"&gt;nil&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

  &lt;span class="n"&gt;expect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;subject&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;do_something&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="ss"&gt;:document&lt;/span&gt;&lt;span class="p"&gt;)).&lt;/span&gt;&lt;span class="nf"&gt;to&lt;/span&gt; &lt;span class="n"&gt;be_nil&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h2&gt;
  
  
  Systemizing testing
&lt;/h2&gt;

&lt;p&gt;It’s important to systemize your testing. In this post I’ve shown you one way you can do that: every time you want to test a dependency, you can follow this process to ensure your code is correctly covered.&lt;/p&gt;

</description>
      <category>ruby</category>
      <category>testing</category>
      <category>tdd</category>
    </item>
    <item>
      <title>Testing Svelte async state changes</title>
      <dc:creator>Daniel Irvine 🏳️‍🌈</dc:creator>
      <pubDate>Fri, 28 Feb 2020 20:23:24 +0000</pubDate>
      <link>https://dev.to/d_ir/testing-svelte-async-state-changes-3mip</link>
      <guid>https://dev.to/d_ir/testing-svelte-async-state-changes-3mip</guid>
      <description>&lt;p&gt;Here’s a short Svelte component that displays the text &lt;code&gt;Submitting...&lt;/code&gt; when a button is clicked:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;script&amp;gt;&lt;/span&gt;
  &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;submitting&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;false&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;submit&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;async&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;submitting&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;await&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;fetch&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;/foo&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="nx"&gt;submitting&lt;/span&gt; &lt;span class="o"&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="nt"&gt;&amp;lt;/script&amp;gt;&lt;/span&gt;

&lt;span class="nt"&gt;&amp;lt;button&lt;/span&gt; &lt;span class="na"&gt;on:click=&lt;/span&gt;&lt;span class="s"&gt;"{submit}"&lt;/span&gt; &lt;span class="nt"&gt;/&amp;gt;&lt;/span&gt;
{#if submitting}
  Submitting...
{/if}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Look carefully at the definition of &lt;code&gt;submit&lt;/code&gt;. The &lt;code&gt;submitting&lt;/code&gt; variable is set to &lt;code&gt;true&lt;/code&gt; before the call to &lt;code&gt;window.fetch&lt;/code&gt; and reset to &lt;code&gt;false&lt;/code&gt; after the call returns.&lt;/p&gt;

&lt;p&gt;The text is only rendered when &lt;code&gt;submitting&lt;/code&gt; is true.&lt;/p&gt;

&lt;p&gt;In other words, the &lt;code&gt;Submitting...&lt;/code&gt; text appears after the button is clicked and disappears after the &lt;code&gt;window.fetch&lt;/code&gt; call completes.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why this is difficult to test
&lt;/h2&gt;

&lt;p&gt;This behavior is tricky because one of our tests will need to get into the state where the &lt;code&gt;Submitting...&lt;/code&gt; text is displayed, and freeze in that state while our test runs its expectations. To do that we need to use Svelte’s &lt;code&gt;tick&lt;/code&gt; function to ensure the rendered output is updatetd.&lt;/p&gt;

&lt;h2&gt;
  
  
  Writing the tests
&lt;/h2&gt;

&lt;p&gt;We require &lt;em&gt;three&lt;/em&gt; unit tests!&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;That the &lt;code&gt;Submitting...&lt;/code&gt; text appears when the button is clicked.&lt;/li&gt;
&lt;li&gt;That initially, no text is displayed.&lt;/li&gt;
&lt;li&gt;That the &lt;code&gt;Submitting...&lt;/code&gt; text disappears after the &lt;code&gt;window.fetch&lt;/code&gt; call completes.&lt;/li&gt;
&lt;/ol&gt;

&lt;h1&gt;
  
  
  Testing the text appears
&lt;/h1&gt;

&lt;p&gt;Let’s take a look at how we’d test this.&lt;/p&gt;

&lt;p&gt;The test below uses my &lt;a href="https://gist.github.com/dirv/838d6ec201d21af378154357c0e033d7"&gt;Svelte testing harness&lt;/a&gt; which is just a few dozen lines of code. I’ve saved that at &lt;code&gt;spec/svelteTestHarness.js&lt;/code&gt;, and this test exists as &lt;code&gt;spec/Foo.spec.js&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;For more information on how I’m running these tests, take a look at my &lt;a href="https://dev.to/d_ir/introduction-4cep"&gt;guide to Svelte unit testing&lt;/a&gt;.&lt;/em&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;expect&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;expect&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;Foo&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;../src/Foo.svelte&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;setDomDocument&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;mountComponent&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;click&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;./svelteTestHarness.js&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;tick&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;svelte&lt;/span&gt;&lt;span class="dl"&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="nx"&gt;Foo&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;name&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;beforeEach&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;setDomDocument&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

  &lt;span class="nx"&gt;beforeEach&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="nb"&gt;window&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;fetch&lt;/span&gt; &lt;span class="o"&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="nb"&gt;Promise&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;resolve&lt;/span&gt;&lt;span class="p"&gt;({});&lt;/span&gt;
  &lt;span class="p"&gt;});&lt;/span&gt;

  &lt;span class="nx"&gt;it&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;shows ‘Submitting...’ when the button is clicked&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;async&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;mountComponent&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;Foo&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

    &lt;span class="nx"&gt;click&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;container&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;querySelector&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;button&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
    &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;tick&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;container&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;textContent&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nx"&gt;toContain&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Submitting...&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="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Notice the use of &lt;code&gt;tick&lt;/code&gt;. Without that, this test wouldn’t pass. That’s because when our code executes &lt;code&gt;submitting = true&lt;/code&gt; it doesn’t synchronously update the rendered output. Calling &lt;code&gt;tick&lt;/code&gt; tells Svelte to go ahead and perform the update.&lt;/p&gt;

&lt;p&gt;Crucially, we haven’t yet flushed the task queue: calling &lt;strong&gt;&lt;code&gt;tick&lt;/code&gt; does not cause the &lt;code&gt;fetch&lt;/code&gt; promise to execute.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;In order to make that happen, we need to flush the task queue which we’ll do in the third test.&lt;/p&gt;

&lt;h2&gt;
  
  
  Testing initial state
&lt;/h2&gt;

&lt;p&gt;First though we have to test the initial state. Without this test, we can’t prove that it was the button click that caused the text to appear: it could have been like that from the beginning.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;it&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;initially isn’t showing the ‘Submitting’ text...&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;async&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;mountComponent&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;Foo&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;container&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;textContent&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nx"&gt;not&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;toContain&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Submitting...&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;h2&gt;
  
  
  Testing the final state
&lt;/h2&gt;

&lt;p&gt;Finally then, we check what happens &lt;em&gt;after&lt;/em&gt; the promise resolves. We need to use &lt;code&gt;await new Promise(setTimeout)&lt;/code&gt; to do this, which flushes the ask queue.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;it&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;hides the ‘Submitting...’ text when the request promise resolves&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;async&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;mountComponent&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;Foo&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="nx"&gt;click&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;container&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;querySelector&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;button&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
  &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nb"&gt;Promise&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;setTimeout&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;container&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;textContent&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nx"&gt;not&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;toContain&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Submitting...&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;And there it is. Three tests to prove a small piece of behavior. Although it might seem overkill for such a small feature, these tests are quick to write—that is, once you know how to write them 🤣&lt;/p&gt;




&lt;p&gt;&lt;strong&gt;Checkout out my &lt;a href="https://dev.to/d_ir/introduction-4cep"&gt;guide to Svelte unit testing&lt;/a&gt; for more tips on how to test Svelte.&lt;/strong&gt;&lt;/p&gt;

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