react-testing-library
Lightweight React testing library for writing maintainable, user-focused tests
Acerca 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.
Preguntas frecuentes
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.
Alternativas a considerar
Ver todas las alternativasInsignias
Promociona react-testing-library y dale más visibilidad añadiendo estas insignias a tu web, documentación o ficha de producto. Cada insignia enlaza a la página de react-testing-library en 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="Destacado en Webfolio" style="max-width: 150px" /></a>
Categorías
Reclamar esta herramienta
¿Eres el fundador? Reclama tu perfil para actualizar los detalles y rastrear visualizaciones.