Your Web News in One Place

Help Webnuz

Referal links:

Sign up for GreenGeeks web hosting
April 18, 2022 06:00 pm GMT

Using testing-playground with React Testing Library

React Testing Library is a popular testing utility tool for front-end testing.

Challenges can arise using React Testing Library, when trying to work out how to target an element.
A logical option may be to add a data-testid attribute to the element you want to target. Yet, there are many downsides to this approach.

According to the React Testing Library guiding principles,

The more your tests resemble the way your software is used,
the more confidence they can give you.
-- Kent C Dodds

We can often spend time testing implementation details. Using the data-testid attribute on an element is only testing that the element exists. Our focus when testing should be on the user, for example, what the user sees, or how the user interacts with it.

It can be a struggle to work out how to target elements without the use of data-testid...

Thankfully, we have the testing-playground!

testing-playground homepage showing full interface

The testing playground is a tool which gives you a visual representation of the DOM, and helps you discover the best queries to target elements.

How to access the playground from within your tests

There are two ways you can access the playground:

  1. Logging the testing playground from within a test generates a link within your console when you run your tests, which you can open in the browser.
import { screen } from "@testing-library/react"     it('test it block', () => {       ...       screen.logTestingPlaygroundURL();       expect(...)     })
  1. Call the debug method screen.debug(), which prints out the DOM output. Copy and paste the DOM output into the testing playground directly.

Once the playground is open, you can navigate around it using the inspect-tool, much like the developer tools 'inspect'. When you inspect an element in the visual DOM, selecting it will display the suggested query to copy into your tests. The display gives you information about accessible and semantic ways to query your element. For example, buttons can be targeted using the accessible role of button.

testing-playground interface showing a query

The testing playground provides you with direct feedback when you make edits to the code. The playground displays improvements you can make to your code and queries, for example how to make your queries more specific by adding a name. Using the suggested queries is preferable to using the non-specific test-ids.

Another helpful use of the testing playground is to confirm what elements are visible in the DOM. You may be expecting to see a success message after a user clicks a button. If your test fails you can use the playground as a debugging tool to see what is visible to the user. Alternatively, you can use screen.debug() for the DOM output.

In summary, the testing playground is a great tool to use in conjunction with React Testing Library
Give it a go, and see how it can improve your testing skills.


Original Link: https://dev.to/katieraby/using-testing-playground-with-react-testing-library-26j7

Share this article:    Share on Facebook
View Full Article

Dev To

An online community for sharing and discovering great ideas, having debates, and making friends

More About this Source Visit Dev To