<?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: Shavina Chau</title>
    <description>The latest articles on DEV Community by Shavina Chau (@shavinac).</description>
    <link>https://dev.to/shavinac</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%2F961358%2Fe3ed33dd-73f5-4f98-b369-c07343101978.jpeg</url>
      <title>DEV Community: Shavina Chau</title>
      <link>https://dev.to/shavinac</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/shavinac"/>
    <language>en</language>
    <item>
      <title>Testing Library - Queries</title>
      <dc:creator>Shavina Chau</dc:creator>
      <pubDate>Wed, 02 Nov 2022 14:54:39 +0000</pubDate>
      <link>https://dev.to/focused_dot_io/testing-library-queries-1hc</link>
      <guid>https://dev.to/focused_dot_io/testing-library-queries-1hc</guid>
      <description>&lt;p&gt;The &lt;a href="https://testing-library.com/" rel="noopener noreferrer"&gt;Testing Library family&lt;/a&gt; (including &lt;strong&gt;React Testing Library&lt;/strong&gt;) is a great tool to help you write comprehensive, maintainable UI tests. &lt;/p&gt;

&lt;p&gt;The two primary functions it offers are &lt;em&gt;queries&lt;/em&gt;, and &lt;em&gt;user actions&lt;/em&gt;. Queries are the methods that Testing Library gives you to find elements on the page [&lt;a href="https://testing-library.com/docs/queries/about" rel="noopener noreferrer"&gt;6&lt;/a&gt;].&lt;/p&gt;

&lt;p&gt;This article explains the three types of queries ("getBy", "findBy", "queryBy") and how to use each one in different scenarios with code samples. [Note A]&lt;/p&gt;

&lt;p&gt;&lt;a href="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%2Farticles%2Fo82trjoxtqncpy86dqgz.png" class="article-body-image-wrapper"&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%2Farticles%2Fo82trjoxtqncpy86dqgz.png" alt="Flowchart describing when to use queryBy vs. getBy vs. findBy. Do you expect the element to be there? If no - use queryBy. If yes - should the element be there right away? If yes - use getBy; if no, use findBy."&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  queryBy
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;queryBy&lt;/strong&gt; should only be used if you want to assert that an element is not present [&lt;a href="https://kentcdodds.com/blog/common-mistakes-with-react-testing-library#using-query-variants-for-anything-except-checking-for-non-existence" rel="noopener noreferrer"&gt;1&lt;/a&gt;].&lt;/p&gt;

&lt;p&gt;Example use cases:&lt;/p&gt;

&lt;h3&gt;
  
  
  Asserting an element should not be present
&lt;/h3&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;queryByText&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Error: Page Not Found&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="nx"&gt;not&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;h3&gt;
  
  
  Asserting an element only appears under certain conditions
&lt;/h3&gt;

&lt;p&gt;Example: a form's Submit button does not appear until all input fields are filled.&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;submitButton&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;queryByText&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;submit&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;submitButton&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="nf"&gt;toBeInTheDocument&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

&lt;span class="nx"&gt;fireEvent&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;change&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="na"&gt;target&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;value&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Jane Smith&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;expect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;submitButton&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;h3&gt;
  
  
  Asserting an element is in the correct initial state
&lt;/h3&gt;

&lt;p&gt;Example: a button that is initially "On", not "Off".&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="c1"&gt;// notice getBy can be used here as we expect this to appear&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;getByRole&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="sr"&gt;/On/i&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;toBeInTheDocument&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

&lt;span class="c1"&gt;// while queryBy is used here as we expect this *not* to appear&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;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="sr"&gt;/Off/i&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;not&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;h3&gt;
  
  
  Asserting a list of elements is empty
&lt;/h3&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;projectCards&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;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;project-card&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;projectCards&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;0&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Asserting an element disappears or is removed
&lt;/h3&gt;

&lt;p&gt;This example is taken from the Testing Library documentation on disappearance [&lt;a href="https://testing-library.com/docs/guide-disappearance/#waiting-for-disappearance" rel="noopener noreferrer"&gt;2&lt;/a&gt;]. &lt;/p&gt;

&lt;p&gt;The &lt;strong&gt;waitFor&lt;/strong&gt; async helper function retries until the wrapped function stops throwing an error. This can be used to assert that an element disappears from the page.&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;test&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;movie title goes away&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="c1"&gt;// element is initially present...&lt;/span&gt;
  &lt;span class="c1"&gt;// note use of queryBy instead of getBy to return null&lt;/span&gt;
  &lt;span class="c1"&gt;// instead of throwing in the query itself&lt;/span&gt;
  &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nf"&gt;waitFor&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;expect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;queryByText&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;i, robot&lt;/span&gt;&lt;span class="dl"&gt;'&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="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;There is also a &lt;strong&gt;waitForElementToBeRemoved&lt;/strong&gt; helper, but in my experience I find &lt;strong&gt;await waitFor&lt;/strong&gt; + &lt;strong&gt;queryBy&lt;/strong&gt; to be more reliable, especially if expecting an element to disappear after a &lt;strong&gt;fireEvent&lt;/strong&gt; or &lt;strong&gt;userEvent&lt;/strong&gt; action. [Note B]&lt;/p&gt;

&lt;h2&gt;
  
  
  getBy
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;getBy&lt;/strong&gt; is used to get an element on the page, and will throw an error if no matching element is immediately found.&lt;/p&gt;

&lt;p&gt;Two common use cases are:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Asserting an element is present on the page, or asserting particular properties about that element.&lt;/li&gt;
&lt;li&gt;Selecting an element on the page to simulate user interaction with it, such as clicking it, entering text, or selecting an input.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;While &lt;strong&gt;queryBy&lt;/strong&gt; can also be used for those purposes, the main reason to prefer &lt;strong&gt;getBy&lt;/strong&gt; over &lt;strong&gt;queryBy&lt;/strong&gt; is that &lt;strong&gt;getBy&lt;/strong&gt; will generally give you more helpful error messages when the element is not found.&lt;/p&gt;

&lt;p&gt;Consider the following examples:&lt;/p&gt;

&lt;h3&gt;
  
  
  Example 1 - asserting an element is present
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// getBy&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;getByText&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Username&lt;/span&gt;&lt;span class="dl"&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 this assertion fails, the error message is &lt;code&gt;Unable to find an element with the text: Username.&lt;/code&gt; The error message clearly describes the desired element. It will also automatically print out the DOM so you can inspect the page yourself.&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="c1"&gt;// queryBy&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;queryByText&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Username&lt;/span&gt;&lt;span class="dl"&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 this assertion fails, the error message may look something like "null is not in the document", or, even more confusingly, "received value must be an HTMLElement or an SVGElement. Received has value: null". There's not much information about what went wrong in these error messages, since the assertion can only see &lt;code&gt;null&lt;/code&gt; as the actual value.&lt;/p&gt;

&lt;h3&gt;
  
  
  Example 2 - interacting with an element
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// getBy&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;next&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;getByRole&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="sr"&gt;/Next/i&lt;/span&gt; &lt;span class="p"&gt;});&lt;/span&gt;
&lt;span class="nx"&gt;fireEvent&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;click&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;next&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If the button is not on the page, &lt;strong&gt;getBy&lt;/strong&gt; will throw an error with message "Unable to find an accessible element with the role "button" and name &lt;code&gt;/Next/i&lt;/code&gt;", which clearly describes the expected element. As mentioned in Example 1, it will also automatically print out the DOM to help in debugging.&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="c1"&gt;// queryBy&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;next&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="sr"&gt;/Next/i&lt;/span&gt; &lt;span class="p"&gt;});&lt;/span&gt;
&lt;span class="nx"&gt;fireEvent&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;click&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;next&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If the button is not on the page, &lt;strong&gt;queryBy&lt;/strong&gt; will not throw - instead, it returns &lt;code&gt;null&lt;/code&gt;. The test will instead fail on the next line, when the &lt;strong&gt;fireEvent&lt;/strong&gt; call throws with the error: &lt;code&gt;Error: Unable to fire a "click" event - please provide a DOM element.&lt;/code&gt; This error message is less obvious since &lt;strong&gt;queryBy&lt;/strong&gt; quietly returned &lt;code&gt;null&lt;/code&gt;, and does not provide any information about the missing element - this requires more time and effort to debug.&lt;/p&gt;

&lt;h2&gt;
  
  
  findBy
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;findBy&lt;/strong&gt; is used to get an element that may not appear on the page immediately. For example, expecting a modal to appear after a button click, or expecting new content to appear after an asynchronous API call returns.&lt;/p&gt;

&lt;p&gt;findBy will return a Promise, so you must use &lt;code&gt;await&lt;/code&gt; or &lt;code&gt;.then()&lt;/code&gt; [&lt;a href="https://testing-library.com/docs/dom-testing-library/api-async/#findby-queries" rel="noopener noreferrer"&gt;3&lt;/a&gt;]. It will throw an error if the element does not appear after a specified timeout. [Note C]&lt;/p&gt;

&lt;p&gt;Use cases for &lt;strong&gt;findBy&lt;/strong&gt; are the same as &lt;strong&gt;getBy&lt;/strong&gt;, but in situations where you may need to wait for the desired element to be on the page.&lt;/p&gt;




&lt;h3&gt;
  
  
  Notes
&lt;/h3&gt;

&lt;p&gt;&lt;u&gt;A.&lt;/u&gt; The code examples shown here use Jest and jest-dom, which provide custom matchers like &lt;code&gt;toBeInTheDocument()&lt;/code&gt; [&lt;a href="https://testing-library.com/docs/ecosystem-jest-dom/" rel="noopener noreferrer"&gt;4&lt;/a&gt;].&lt;/p&gt;

&lt;p&gt;&lt;u&gt;B.&lt;/u&gt; Because &lt;strong&gt;fireEvent&lt;/strong&gt; and &lt;strong&gt;userEvent&lt;/strong&gt; are automatically wrapped in &lt;strong&gt;act&lt;/strong&gt;, all React state updates are completed before the next line is executed. This means if an element is rendered (or not) based on simple state changes, attempting to use &lt;strong&gt;waitForElementToBeRemoved&lt;/strong&gt; results in "Error: The element(s) given to waitForElementToBeRemoved are already removed. waitForElementToBeRemoved requires that the element(s) exist(s) before waiting for removal." since the element was already removed during the state update. &lt;/p&gt;

&lt;p&gt;According to [&lt;a href="https://github.com/testing-library/react-testing-library/issues/1033#issuecomment-1107477476" rel="noopener noreferrer"&gt;5&lt;/a&gt;], "waitForElementToBeRemoved is meant to await for async operations and in the example attached, you don't have anything async but just a simple react state change. In that scenario, there's no need to wait for anything."&lt;/p&gt;

&lt;p&gt;&lt;u&gt;C.&lt;/u&gt; Behind the scenes, &lt;strong&gt;findBy&lt;/strong&gt; is a combination of &lt;strong&gt;getBy&lt;/strong&gt; and &lt;strong&gt;waitFor&lt;/strong&gt;, and accepts &lt;strong&gt;waitFor&lt;/strong&gt; options as the second parameter, allowing you to customize values like the timeout.&lt;/p&gt;

&lt;h3&gt;
  
  
  Resources
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;&lt;a href="https://kentcdodds.com/blog/common-mistakes-with-react-testing-library#using-query-variants-for-anything-except-checking-for-non-existence" rel="noopener noreferrer"&gt;https://kentcdodds.com/blog/common-mistakes-with-react-testing-library#using-query-variants-for-anything-except-checking-for-non-existence&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://testing-library.com/docs/guide-disappearance/#waiting-for-disappearance" rel="noopener noreferrer"&gt;https://testing-library.com/docs/guide-disappearance/#waiting-for-disappearance&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://testing-library.com/docs/dom-testing-library/api-async/#findby-queries" rel="noopener noreferrer"&gt;https://testing-library.com/docs/dom-testing-library/api-async/#findby-queries&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://testing-library.com/docs/ecosystem-jest-dom/" rel="noopener noreferrer"&gt;https://testing-library.com/docs/ecosystem-jest-dom/&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://github.com/testing-library/react-testing-library/issues/1033#issuecomment-1107477476" rel="noopener noreferrer"&gt;https://github.com/testing-library/react-testing-library/issues/1033#issuecomment-1107477476&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://testing-library.com/docs/queries/about" rel="noopener noreferrer"&gt;https://testing-library.com/docs/queries/about&lt;/a&gt; &lt;/li&gt;
&lt;li&gt;&lt;a href="https://testing-library.com/docs/dom-testing-library/cheatsheet/" rel="noopener noreferrer"&gt;https://testing-library.com/docs/dom-testing-library/cheatsheet/&lt;/a&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Topics: Testing Library, React Testing Library, getBy vs findBy vs queryBy, JavaScript, Jest&lt;/p&gt;

</description>
      <category>testing</category>
      <category>testinglibrary</category>
      <category>javascript</category>
    </item>
  </channel>
</rss>
