About this project
React Testing Library (RTL) is a lightweight testing utility for React that promotes good testing practices by encouraging tests that resemble how software is actually used. It builds on `react-dom/test-utils` and provides simple, complete DOM testing utilities. The primary guiding principle is: "The more your tests resemble the way your software is used, the more confidence they can give you."
Key features include:
- **User-centric queries**: Use `getBy`, `queryBy`, and `findBy` functions to interact with DOM elements as a user would, with support for regex selectors.
- **Encourages maintainable tests**: Focuses on functionality rather than implementation details, so refactors don't break tests.
- **Works with React Hooks and classes**: Tests are the same regardless of component implementation.
- **Lightweight**: Only exposes methods that encourage good testing practices.
Installation requires `@testing-library/react` and, starting from version 16, `@testing-library/dom`. Versions 13+ require React v18. It's recommended to also install `@testing-library/jest-dom` for custom Jest matchers.
Basic usage involves rendering components with `render`, simulating events with `fireEvent`, and querying elements via `screen`. Complex examples demonstrate mocking API calls with Mock Service Worker (MSW) for login flows and error handling.
RTL is part of the Testing Library family, which includes React Hooks Testing Library for reusable hooks. It's widely adopted and maintained by a large community of contributors.
Comments
0 Rating appears after 10 ratings
Sign in to join the discussion.