How do you test a React component?

React testing, accessibility and TypeScript · Freshers

Model answer

The common stack is Vitest or Jest with React Testing Library. Render the component, find elements the way a user would (by role, label or text), simulate actions with user-event, and assert on what appears: expect(screen.getByRole('button', { name: /save/i })).toBeInTheDocument().

Test behaviour, not implementation details like internal state.

Learn this topic

Testing React Apps with Vitest and React Testing Library in the course React.js Full Course: Beginner to Advanced.

Sign in to add your own answer.

More questions