1. 21
    Troubleshooting Tests with React Testing Library's Debug Helper
    53s

Troubleshooting Tests with React Testing Library's Debug Helper

Jamund Ferguson
InstructorJamund Ferguson
Share this video with your friends

Social Share Links

Send Tweet
Published 3 years ago
Updated 3 years ago

When you render a component with the Redux Testing Library you get back a result object with numerous properties. One of those properties is debug which will log the HTML for whatever component you just rendered. It might look something like this:

const { debug } = render(<h1>Hello World</h1>);
debug(); // logs <h1>Hello World</h1>

There's also another approach to logging HTML using screen.debug, which takes an an argument any queried element. One example would be:

screen.debug(screen.getByRole("table"));

Just remember that whether you use debug() or screen.debug() you don't need to wrap it in a console.log()!

Instructor: [0:00] Open up your IDE and in the products folder, create a new file called products.test.TSX. In that file, we're going to import react, and we'll also import render context test details, and we're going to import products from ./products.

[0:16] For our first test, we're going to test the products component. For this one type const {debug} = render with context products. Now type debug, followed by (). This debug utility is not something that we wrote.

[0:30] If we go into render with context, you can see that we're returning all the utils provided by the testing library react render function. One of those utils is called debug. If we open up our terminal window and type NPX.JS-- products.test.

[0:46] That filters on this and not product slice. We can see the debug will log all the HTML for a given component.