🧪Testing Library
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
Example code
jsx
render(<LoginForm />);
await userEvent.click(screen.getByRole("button", { name: "Log in" }));
expect(screen.getByText("Enter your email")).toBeInTheDocument();Common mistake
Key takeaways
- 1Like a user. Find things by role and text.
- 2Not the insides. Ignore how a part is built.
- 3Many frameworks. React, Vue, Angular and more.