Getting direct access to the DOM

Sometimes despite all the helper functions from React Testing Library (like screen.getByText()), you still need to access the DOM directly.

Sometimes the only practical (or easy!) way is to select elements by their CSS class names (which RTL does not support), check computed styles, or find elements with no useful accessible roles or text content.

In this lesson we will explain how to access the DOM and use APIs such as document.querySelector() in your Jest or Vitest tests, just as you would in a real browser.

Loading full lesson content & code...

Ready to try running tests for this lesson?