Run your test command, typically npm test, which runs Jest. Jest automatically picks up .test.js files. You should see your test pass or fail in the output.
How to Write Your First React Component Test
How do I run a React test?
The answer
More FAQs in How to Write Your First React Component Test
Create a .test.js file, import render and screen from RTL and your component, use Jest's test function, render the component, query an element by role and name, and assert it is in the document. Run with npm test.
Use RTL's screen queries, preferably by role and accessible name, like screen.getByRole('heading', { name: /hello/i }). These are the same queries screen readers use, so they focus on what the user experiences.
It mounts your component in a simulated DOM provided by jsdom, so you can query and interact with the rendered output. After render, you use screen queries to find elements.
Still have questions?
Browse all our FAQs or reach out to our support team
Want to upskill yourself?
Your learning library
Small steps. Big possibilities.
Find your next course. Build something great.