Test React Components with Vitest and React Testing Library
Intermediate · 45 minutes · 100 points
In this lab you will scaffold a React project inside a Node 20 container, write components that fetch data from an API, and verify their behaviour using Vitest together with React Testing Library. You will render components, simulate user interactions, and assert on data returned by a mocked fetch — the same workflow used in modern CI/CD pipelines.
What you’ll learn
- Scaffold a Vite + React project and install Vitest with React Testing Library inside a Node 20 container
- Write a React component that fetches data from an external API and renders it conditionally
- Mock the global fetch function in tests, simulate user clicks, and assert on rendered output using React Testing Library queries
How this lab works
- Start the lab: One click starts a container on your computer with a real terminal: nothing to install by hand, nothing to clean up.
- Work through 3 tasks: Each task says what to build or fix. You do it yourself, in a real terminal, with hints when you get stuck.
- Check your work: Check my work runs each task’s checks in your container and says what passed and what to fix.
- Finish and keep going: 100 points, your progress saved, and the next lab to try.
What it runs on
- Runs: A container on your computer, with a real terminal
- Image: node:20
What you need
- A free LearnXops account
- The LearnXops desktop app for macOS, Windows or Linux
- Docker Desktop installed and running
Topics: react, vitest, react-testing-library, unit-testing, mocking, fetch, ci-cd, node, vite, coverage
Create a free account to start this lab.