react-testing-library
Lightweight React testing library for writing maintainable, user-focused tests
À propos de react-testing-library
React Testing Library provides simple utilities to test React components by focusing on how users interact with your application. It encourages accessible testing practices and helps ensure your tests remain reliable even as your components evolve.
FAQ
You can test input onChange handlers using the fireEvent.change method. Pass the value as part of the change method call. For example, fireEvent.change(input, {target: {value: 'a'}}).
To test if an error boundary successfully catches an error, ensure that the fallback of the boundary is displayed when a child throws. For example, expect(container.textContent).toEqual('Something went wrong.').
Yes, you can write unit and integration tests with React Testing Library. However, it intentionally does not support shallow rendering. You can mock dependencies to unit test high-level components.
If a component throws during render, you can wrap it in a try-catch or use dedicated matchers if your test runner supports these. For example, in Jest, you can use expect(() => render( )).toThrow('I throw').
You can use Jest's mocking feature to mock out components. For example, jest.mock('react-transition-group', () => { ... }). This is useful for animation libraries or other complex components.
Snapshot diffing may not work out of the box because React Testing Library uses the DOM, which is mutable. To fix this, clone the DOM when passing it into snapshot-diff. For example, const firstVersion = container.cloneNode(true).
This warning is usually caused by an async operation causing an update after the test has already finished. You can resolve it by waiting for the result of the operation in your test using utilities like waitFor or a find* query.
It is often preferable to test high enough up the component tree to simulate realistic user interactions. Testing every component individually can cause issues, so focus on testing how users interact with your application.
Alternatives à considérer
Voir toutes les alternativesBadges
Faites la promotion de react-testing-library et donnez-lui plus de visibilité en ajoutant ces badges à votre site web, votre documentation ou votre fiche produit. Chaque badge renvoie vers la page de react-testing-library sur Webfolio.
<a href="https://www.webfolio.to/tools/react-testing-library?utm_source=badge&utm_campaign=badge" target="_blank" rel="noopener noreferrer"><img src="https://www.webfolio.to/badges/featured_color.svg" alt="Mis en avant sur Webfolio" style="max-width: 150px" /></a>
Catégories
Revendiquer cet outil
Vous êtes le fondateur ? Revendiquez votre profil pour mettre à jour les détails et suivre les vues.