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 2 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.

egghead
egghead
~ 8 minutes ago

Member comments are a way for members to communicate, interact, and ask questions about a lesson.

The instructor or someone from the community might respond to your question Here are a few basic guidelines to commenting on egghead.io

Be on-Topic

Comments are for discussing a lesson. If you're having a general issue with the website functionality, please contact us at support@egghead.io.

Avoid meta-discussion

  • This was great!
  • This was horrible!
  • I didn't like this because it didn't match my skill level.
  • +1 It will likely be deleted as spam.

Code Problems?

Should be accompanied by code! Codesandbox or Stackblitz provide a way to share code and discuss it in context

Details and Context

Vague question? Vague answer. Any details and context you can provide will lure more interesting answers!

Markdown supported.
Become a member to join the discussionEnroll Today