<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Ron Merkin on Medium]]></title>
        <description><![CDATA[Stories by Ron Merkin on Medium]]></description>
        <link>https://medium.com/@ron.merkin?source=rss-8b49ac302996------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*sjqmaHPXIeUAU_KL</url>
            <title>Stories by Ron Merkin on Medium</title>
            <link>https://medium.com/@ron.merkin?source=rss-8b49ac302996------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Fri, 04 Dec 2026 09:33:10 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@ron.merkin/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[My recommendations for React Testing Library]]></title>
            <link>https://medium.com/@ron.merkin/my-recommendations-for-react-testing-library-5eed1cdd1ddf?source=rss-8b49ac302996------2</link>
            <guid isPermaLink="false">https://medium.com/p/5eed1cdd1ddf</guid>
            <category><![CDATA[jest]]></category>
            <category><![CDATA[react-testing-library]]></category>
            <category><![CDATA[react]]></category>
            <dc:creator><![CDATA[Ron Merkin]]></dc:creator>
            <pubDate>Wed, 08 Feb 2023 13:31:13 GMT</pubDate>
            <atom:updated>2023-02-08T13:31:13.044Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*ds4sK-BR8luIKjGR" /><figcaption>Photo by <a href="https://unsplash.com/@inakihxz?utm_source=medium&amp;utm_medium=referral">Iñaki del Olmo</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p>I started my new job a year ago. From the beginning, I was told that we had a big testing debt, so I took it upon myself to understand the situation. Then I rolled up my sleeves and got to work promoting testing within our team in order to push us toward company-wide goals.</p><p>This included a couple of steps. First, I started mapping our repositories and identifying each one’s status. Second, I had to involve the whole team in assisting and writing tests for code that was previously written by different people. In this article, I’m going to focus on the second step.</p><p>React Testing Library is the library used by all of Intuit for frontend unit tests. I had to create some documentation to align the team and ensure we were working in parallel using the same practices. The alternative was chaos, and no one wants that, right?</p><p>The most important thing in writing a test is making sure we don’t only write the test for code coverage. Tests should build confidence so that we don’t need to wake up in the middle of the night for on-call issues. We want to test it as if we are the user, examining the issue from every possible use case.</p><h3>Finding elements</h3><p>A test is mostly based on finding elements on the screen and validating them. React Testing Library exposes a render function and the screen object. The render is used to render our component into a container. With the screen object, we have a few methods for finding elements within that container.</p><p>The first option we have is <strong>GetBy</strong>. It returns a node element if found. If a node element is not found or multiple node elements are found it throws an error.</p><pre>render(&lt;OurTestedComponent /&gt;);<br>const getByElement = screen.getByText(&quot;some text&quot;);<br>expect(getByElement).toBeInTheDocument();</pre><p>The second option is <strong>QueryBy</strong>, which<strong> </strong>returns a node element. If a node element is not found, it will return null. If multiple node elements are found it throws an error.</p><pre>render(&lt;OurTestedComponent /&gt;);<br>const queryByEelement = screen.queryByText(&quot;some text&quot;);<br>expect(queryByEelement).toBeInTheDocument();<br>const nonExistingText = screen.queryByText(&quot;non existing text&quot;)<br>expect(nonExistingText).toBeNull();</pre><p>The third option is <strong>FindBy</strong>. This returns a promise that contains the node element. If a node element is not found, or multiple elements are found, it will throw an error. Using this method is highly recommended for elements that might be displayed asynchronously.</p><pre>render(&lt;OurTestedComponent /&gt;);<br>const findByEelement = await screen.findByByText(&quot;some text&quot;);<br>expect(findByEelement).toBeInTheDocument();</pre><p>In most cases, I prefer using the findBy method. The main advantage is that it gives the screen the chance to render an element and uses retry to fetch the element. With the other two options, the library will immediately throw an error if the elements are not found.</p><h3>Event handling</h3><p>In many cases, our screen is not only displaying data. We have inputs, buttons, select, and so on. When testing, we need to validate that the functionality we add to them works as expected. We have two options:</p><ol><li>fireEvent which is located within React testing library.</li></ol><pre>import { fireEvent } from &quot;@testing-library/react&quot;;</pre><p>2. userEvent which is an additional library to install as your project dependency.</p><pre>import userEvent from &quot;@testing-library/user-event&quot;;</pre><p>The main difference between the two is that fireEvent does not exactly like how a user interacts with the application but, rather, fires events on the DOM. UserEvent dispatches the events like they would if a user interacted with the application.</p><p>Let’s say we want to test the behavior of an input element and see the difference between how the two work. When users normally type into a field, multiple events are happening on the element, such as focusing on the element and, changing the value.</p><p>With fireEvent, we only use the change event on an input element and not the entire interaction as the user sees it:</p><pre>const inputElement = await screen.findByLabel(&quot;some label&quot;);<br>fireEvent.change(inputElement, { target: { value: &quot;some value&quot;} });<br>expect(await screen.findByText(&quot;some value&quot;)).toBeInTheDocument();</pre><p>Whereas in userEvent we use the type functionality which will include all relevant events:</p><pre>const inputElement = await screen.findByLabel(&quot;some label&quot;);<br>await userEvent.type(inputElement, &quot;some value&quot;);<br>expect(await screen.findByText(&quot;some value&quot;)).toBeInTheDocument();</pre><p>According to the official documentation, userEvent is the recommended way to go, so we try to use it in most cases.</p><h3>Inspecting current status</h3><p>In this library, there is no visualization of a test run, so it’s sometimes unclear why a test fails.</p><p>The screen object exposes a function that outputs into your terminal a link to a browser where you can view the current status of the screen at a certain point of your test. This function is called logTestingPlaygroundURL.</p><p>Once you add screen.logTestingPlaygroundURL() to your test, an example of a page output can be found <a href="https://testing-playground.com/">here</a>. Within the page, there is a section that shows you the recommended query for each visible element.</p><h3>Final thoughts</h3><p>I hope this information gives you and your team a better understanding of the basic functionalities that React Testing Library exposes so that you’ll know which to use for your unit tests. Happy testing!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=5eed1cdd1ddf" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>