Using testing-playground with React Testing Library
Tags: testing, react, webdev
Published: 2022-04-18 18:00:33

[React Testing Library](https://testing-library.com/docs/react-testing-library/intro) 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 a few downsides to this approach.

According to the React Testing Library [guiding principles](https://testing-library.com/docs/guiding-principles/),

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

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 <a href="https://testing-playground.com/" target="_blank">testing-playground</a>!

![testing-playground homepage showing full interface](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/npm1qzgt7sbax3vdn1mu.png)

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.

```jsx
import { screen } from "@testing-library/react"

     it('test it block', () => {
       ...
       screen.logTestingPlaygroundURL();
       expect(...)
     })
```

2. 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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/764fpe4ypqiuj5fuqf4q.png)

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.