Skip to content
BENIBIX Learn

Testing Library

Development🧪 Testing & QualityTest Frameworks

Also known as React Testing Library, RTL, DOM Testing Library, @testing-library

Tools that test screens the way a person uses them, by labels and text.

Think of it like

Think of it like testing a toy the way a child plays with it. You press the buttons a child would press.

Example

Lucy's test finds the button named Log in, clicks it and checks a message appears. It does not care how the button is built.

Why it matters

Tests keep passing when you rebuild a part, as long as users see the same thing.

Definition

Testing Library is a set of tools for testing user interfaces the way people use them. Its rule is that tests should look like how the software is used. So it finds things by their role, label or text, not by hidden code names. There are versions for React, Vue, Angular and plain web pages. It runs inside test runners like Jest and Vitest.

Example code

jsx
render(<LoginForm />);
await userEvent.click(screen.getByRole("button", { name: "Log in" }));
expect(screen.getByText("Enter your email")).toBeInTheDocument();
Find the button by its name, click it, and look for the message.
Testing Library checks UI parts the way a user finds them.

Common mistake

Finding elements by class names or test IDs first. Use roles and labels, the way users find them.

Key takeaways

  1. 1
    Like a user. Find things by role and text.
  2. 2
    Not the insides. Ignore how a part is built.
  3. 3
    Many frameworks. React, Vue, Angular and more.