Search icon CANCEL
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Simplify Testing with React Testing Library

You're reading from   Simplify Testing with React Testing Library Create maintainable tests using RTL that do not break with changes

Arrow left icon
Product type Paperback
Published in May 2021
Publisher Packt
ISBN-13 9781800564459
Length 246 pages
Edition 1st Edition
Languages
Tools
Arrow right icon
Author (1):
Arrow left icon
Scottie Crump Scottie Crump
Author Profile Icon Scottie Crump
Scottie Crump
Arrow right icon
View More author details
Toc

Table of Contents (10) Chapters Close

Preface 1. Chapter 1: Exploring React Testing Library 2. Chapter 2: Working with React Testing Library FREE CHAPTER 3. Chapter 3: Testing Complex Components with React Testing Library 4. Chapter 4: Integration Testing and Third-Party Libraries in Your Application 5. Chapter 5: Refactoring Legacy Applications with React Testing Library 6. Chapter 6: Implementing Additional Tools and Plugins for Testing 7. Chapter 7: End-to-End UI Testing with Cypress 8. Answers 9. Other Books You May Enjoy

Using the debug method

The debug method, accessible from the screen object, is a helpful tool in React Testing Library's API that allows you to see the current HTML output of components as you build out your tests. In this section, we will learn how to display the resulting DOM output of an entire component or specific elements.

Debugging the entire component DOM

You can use the debug method to log the entire DOM output of a component when you run your test:

it('displays the header and paragraph text', () => {
   render(<Travel />)
   screen.debug()
})

In the preceding code, we first rendered the Travel component into the DOM. Next, we invoked the debug method. When we run our test, the following will be logged to the console:

Figure 2.6 – Travel DOM debug

Figure 2.6 – Travel DOM debug

In the previous screenshot, the entire DOM output of the Travel component is logged to the screen. Logging the whole output can help you build out your test, especially when interacting with one element in the DOM affects elements elsewhere in the current DOM. Now you know how to log the output of the entire component DOM to the screen. Next, we will learn how to log specific elements of the DOM to the screen.

Debugging specific component elements

You can use the debug method to log specific elements of the resulting component DOM to the screen:

it('displays the header and paragraph text', () => {
  render(<Travel />)
  const header = screen.getByRole('heading', { name: 
    /travel anywhere/i })
  screen.debug(header)
})

In the previous code, first, we rendered the Travel component into the DOM. Next, we used the getByRole method to query the DOM for a heading with the name travel anywhere and saved it to a variable named header. Next, we invoked the debug method and passed in the header variable to the method. When we run our test, the following will be logged to the console:

Figure 2.7 – Travel element debug

Figure 2.7 – Travel element debug

When you pass in a specific DOM node found by using one of the available query methods, the debug method only logs the HTML for the particular node. Logging the output for single elements can help you only focus on specific parts of the component. Be sure to remove any debug method code from your tests before making commits because you only need it while building out the test.

Now you know how to use the debug method to render the resulting DOM output of your components. The debug method is a great visual tool to have while writing new tests and also when troubleshooting failing tests.

You have been reading a chapter from
Simplify Testing with React Testing Library
Published in: May 2021
Publisher: Packt
ISBN-13: 9781800564459
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime