No Login Data Private Local Save

React Testing Library Skeleton - Online Test Boilerplate

7
0
0
0
Component Name
Use PascalCase for best results
Component Type Preset
Auto-selects relevant test scenarios
Language
JS TS
Test Scenarios
Basic Render Props Test Click Events Input / Change Async / Fetch Mock Functions Conditional Render Form Submit Error State Snapshot
MyComponent.test.jsx
Suggested path: src/components/MyComponent/__tests__/MyComponent.test.jsx

Frequently Asked Questions

What is React Testing Library?
React Testing Library (RTL) is a lightweight testing utility built on top of DOM Testing Library. It encourages testing React components from the user's perspective — focusing on behavior rather than implementation details. Instead of testing component state or props directly, RTL guides you to query the DOM like a real user would (by text, role, label, etc.) and interact with elements naturally. It works seamlessly with Jest and is the officially recommended testing library for React by the React team.
Why use React Testing Library over Enzyme?
RTL focuses on testing behavior from the user's perspective, making tests more resilient to refactoring. Enzyme allows direct access to component internals (state, props, instance methods), which often leads to brittle tests that break when implementation changes. RTL's philosophy is "the more your tests resemble the way your software is used, the more confidence they can give you." Additionally, Enzyme has struggled to keep pace with React 18+ features like concurrent rendering and Suspense, while RTL has first-class support for modern React patterns.
How do I test asynchronous components with React Testing Library?
Use waitFor for assertions that need to wait, and findBy queries (which return promises) for elements that appear asynchronously. The waitFor utility repeatedly runs your callback until it passes or times out (default 1000ms). For testing loading states, use queryBy to assert that a loading indicator is present, then findBy to wait for content to appear. Always wrap async interactions in act() or use RTL's built-in async utilities which handle this automatically.
What's the difference between fireEvent and userEvent?
fireEvent dispatches a single DOM event (like a click), but real user interactions trigger multiple events. For example, a user typing in an input triggers focus, keyDown, keyUp, and change events. userEvent (from @testing-library/user-event) simulates full, realistic user interactions — a userEvent.click() triggers hover, pointerDown, pointerUp, and click events in the correct order. The React Testing Library team strongly recommends userEvent for more realistic and reliable tests. It's available as a separate package: @testing-library/user-event.
How do I mock API calls in React Testing Library tests?
You can mock API calls using jest.fn() to mock fetch or axios, or use libraries like MSW (Mock Service Worker) for more robust API mocking. Jest's built-in mocking allows you to mock modules: jest.mock('../api'). For fetch specifically, you can mock global.fetch with jest.fn() and control its resolved values. MSW is increasingly popular as it intercepts requests at the network level, giving you more realistic testing conditions without mocking implementation details.
What are the best practices for writing maintainable RTL tests?
1. Use semantic queries: prefer getByRole, getByLabelText, getByText over getByTestId.
2. Test behavior, not implementation: don't test state values or internal methods.
3. Use screen instead of destructuring render's return value.
4. Write accessible components — this makes them easier to test with RTL.
5. Keep tests focused: one behavior per test block.
6. Use userEvent over fireEvent for realistic interactions.
7. Avoid testing third-party library internals.
How do I test components that use React Router?
Wrap your component in a MemoryRouter (from react-router-dom) in your test render. This provides routing context without a real browser. You can set the initial route using initialEntries prop. To test navigation, use userEvent.click() on links and then assert that the expected content appears. For testing route changes, you can check that window.location.pathname has changed or assert that components rendered at the target route are now visible.
What is jest-dom and why is it useful?
@testing-library/jest-dom extends Jest's matchers with DOM-specific assertions like toBeInTheDocument(), toHaveTextContent(), toBeDisabled(), toBeVisible(), and many more. These custom matchers make test assertions more readable and expressive. It's typically imported once in a setup file (like jest.setup.js) so the matchers are available in all test files without explicit imports.