Course contentsShow
← React
Lesson 2044 of 2,39646. Testing React with RTLFree lesson

The screen Object: Your Query Gateway

Using screen to access queries without destructuring from render, the modern RTL pattern.

The screen Object: Your Query Gateway

You'll learn: Why screen is the modern, cleaner way to access RTL queries instead of destructuring from render().

The Old vs Modern Pattern

When RTL first emerged, developers destructured queries directly from render():

const { getByRole, getByText } = render(<MyComponent />);
const button = getByRole('button');

This works, but it's verbose and creates unnecessary variable assignments. The screen object changed everything.

Why screen Exists

Think of screen as a permanent window into your rendered component. Instead of destructuring queries every time, RTL provides a global screen object that automatically has access to all queries after you call render():

import { render, screen } from '@testing-library/react';

render(<MyComponent />);
const button = screen.getByRole('button');
const heading = screen.getByText('Hello');

The Benefits

  1. Less boilerplate — No destructuring needed
  2. Consistency — Every test uses the same screen import
  3. Cleaner diffs — Adding queries doesn't change the render() line
  4. Better autocomplete — IDEs can provide better suggestions with a stable object

The Mental Model

Imagine screen as your query control panel. After render() paints your component into the test DOM, screen lights up with all the query methods you need. It always points to the current document, so you query what the user sees.

render(<LoginForm />);
// screen is now your gateway:
screen.getByLabelText('Email');
screen.getByRole('button', { name: /submit/i });

Key Takeaway: Use screen instead of destructuring queries from render() — it's the modern, cleaner RTL pattern that reduces boilerplate and improves test readability.