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
- Less boilerplate — No destructuring needed
- Consistency — Every test uses the same
screenimport - Cleaner diffs — Adding queries doesn't change the
render()line - 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.