<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
  <channel>
    <title>Jest on David Lang</title>
    <link>https://www.davidlang.tech/tags/jest/</link>
    <description>Recent content in Jest on David Lang</description>
    <generator>Hugo</generator>
    <language>en</language>
    <lastBuildDate>Thu, 15 Jul 2021 00:00:00 +0000</lastBuildDate>
    <atom:link href="https://www.davidlang.tech/tags/jest/index.xml" rel="self" type="application/rss+xml" />
    <item>
      <title>Testing React Components with Jest and RTL</title>
      <link>https://www.davidlang.tech/posts/testing-react-components-with-jest-and-rtl/</link>
      <pubDate>Thu, 15 Jul 2021 00:00:00 +0000</pubDate>
      <guid>https://www.davidlang.tech/posts/testing-react-components-with-jest-and-rtl/</guid>
      <description>&lt;p&gt;React Testing Library (RTL) encourages tests that resemble how users interact with your UI-queries by role, label, and text instead of implementation details.&lt;/p&gt;&#xA;&lt;h2 id=&#34;a-simple-component-test&#34;&gt;A Simple Component Test&lt;/h2&gt;&#xA;&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#93a1a1;background-color:#002b36;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;&#34;&gt;&lt;code class=&#34;language-jsx&#34; data-lang=&#34;jsx&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#268bd2&#34;&gt;import&lt;/span&gt; { render, screen, fireEvent } from &lt;span style=&#34;color:#2aa198&#34;&gt;&amp;#39;@testing-library/react&amp;#39;&lt;/span&gt;;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#268bd2&#34;&gt;import&lt;/span&gt; Counter from &lt;span style=&#34;color:#2aa198&#34;&gt;&amp;#39;./Counter&amp;#39;&lt;/span&gt;;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;test(&lt;span style=&#34;color:#2aa198&#34;&gt;&amp;#39;increments count on click&amp;#39;&lt;/span&gt;, () =&amp;gt; {&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  render(&amp;lt;&lt;span style=&#34;color:#268bd2&#34;&gt;Counter&lt;/span&gt; /&amp;gt;);&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  fireEvent.click(screen.getByRole(&lt;span style=&#34;color:#2aa198&#34;&gt;&amp;#39;button&amp;#39;&lt;/span&gt;, { name&lt;span style=&#34;color:#719e07&#34;&gt;:&lt;/span&gt; &lt;span style=&#34;color:#dc322f&#34;&gt;/increment/i&lt;/span&gt; }));&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  expect(screen.getByText(&lt;span style=&#34;color:#dc322f&#34;&gt;/count: 1/i&lt;/span&gt;)).toBeInTheDocument();&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;});&#xA;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;h2 id=&#34;what-to-test&#34;&gt;What to Test&lt;/h2&gt;&#xA;&lt;p&gt;Focus on user-visible behavior: forms submit, errors display, loading states appear. Mock network with MSW (Mock Service Worker) instead of mocking fetch in every test.&lt;/p&gt;</description>
    </item>
  </channel>
</rss>
