Testing components
Understand the idea
A good component test renders the component with known props and checks the text a person would see — not the private names of useState .
Test what a person can see
You do not need a huge app to learn component testing. Greeting from the first lesson takes a name and returns a heading. That is a clear contract: given Ada, show Hello, Ada.
A testable is one you can render by passing . If it only works when a global variable, a hidden context, and a network call are all perfect, testing becomes setting up a miniature product.
What the live example does
The example is a tiny test runner inside the preview:
- Create a temporary DOM node.
- Render Greeting with name Ada into it.
- flushSync makes sure the render finishes before we read the result.
- Read textContent.
- Check that it equals Hello, Ada.
- Log pass or fail.
Teams often use React Testing Library for the same idea: render, then find by role or text. The assertion here is that idea without a separate test runner.
| Knows | Does not need | Why |
|---|---|---|
| Props you passed | Internal useState names | The public contract |
| Visible text | How many times it rendered | People read text |
| A button’s role or label | Exact CSS class strings | Classes change in redesigns |
Arrange, act, assert
- Arrange: choose props (name set to Ada).
- Act: render the component.
- Assert: check the visible text.
When this fails in a pull request, the message should say the greeting was wrong, not dump an opaque snapshot.
Keep components easy to call
Prefer props over hidden globals. If the component truly needs context, the test can wrap it in a Provider on purpose. Make that choice visible.
What you can defend after this lesson
You can point at Greeting and at one clear assertion. That is the bar: a real component from earlier, rendered, and checked against what a person would read.
Mental model
FOLLOW THE VALUE
Arrange, act, assert.
01 / 03Trace the transformation
Live example
The example renders Greeting with name Ada into a temporary node, then logs pass when the text is Hello, Ada.
import { flushSync } from "react-dom"; // Finish the render before asserting. import { createRoot } from "react-dom/client"; import { Greeting } from "./Greeting"; // Component from the first lesson. export default function App() { const host = document.createElement("div"); // Stand-in for a test DOM node. const root = createRoot(host); flushSync(() => { root.render(<Greeting name="Ada" />); // Act: render with known props. }); const text = host.textContent; // Read what the screen shows. const ok = text === "Hello, Ada"; // Assert the visible contract. console.log(ok ? "pass" : "fail"); // Result of the assertion. console.log(text); // The text under test. console.log("greeting"); // The component under test. console.log("assert"); // This run is an assertion. console.log("tested"); // Marker that the check finished. return <p>{ok ? "pass" : "fail"}: {text}</p>; }
Guided challenge
Expect the greeting that rendered
The test logs fail. Change it to pass when the heading text is Hello, Ada.
import { flushSync } from "react-dom"; import { createRoot } from "react-dom/client"; import { Greeting } from "./Greeting"; export default function App() { const host = document.createElement("div"); const root = createRoot(host); flushSync(() => { root.render(<Greeting name="Ada" />); // Render still produces Hello, Ada. }); console.log("fail"); // Wrong - change to pass when the text matches. console.log(host.textContent); // Keep the text log. console.log("greeting"); // Keep the greeting log. console.log("assert"); // Keep the assert log. console.log("tested"); // Keep the tested log. return <p>fail</p>; }
EXPECTED CONSOLE OUTPUT
pass Hello, Ada greeting assert tested
Hints 0/2
Free sandbox
Make a variation, test an assumption, or break the example on purpose. This draft is saved separately.
import { Greeting } from "./Greeting"; export default function App() { return <Greeting name="Ada" />; // Try a different name and watch the heading. }
Quiz
A quick check for understanding. Retry as often as you like; your learning path stays open.
Ready to call this one understood?
Mark this lesson complete to earn 200 XP. You can always revisit it.