0
Challenge · 85Tooling & Quality

Component Testing

Testingused in ~50% of apps

You ship a refactor, every test stays green, and the Like button is silently broken in production. Tests that assert implementation details give false confidence. Component testing with React Testing Library asks the only question that matters: does the user still see the right thing happen?

Core idea. Render a component, interact with it the way a user would (find by role, click, type), and assert on what the user can see — not on internal state, props, or class names. RTL + Vitest (or Jest) is the 2026 default.
Playground, this isn't a screenshot
LikeButton.tsxclick it yourself
function LikeButton() {
const [n, setN] = useState(0)
onClick={() => setN(n + 1)}
return <button>Like · {n}</button>
}
LikeButton.test.tsxvitest + RTL
test('counts up when clicked', async () => {
const user = userEvent.setup()
render(<LikeButton />)
const btn = screen.getByRole('button', { name: /like/i })
expect(btn).toHaveTextContent('Like · 0')
await user.click(btn)
expect(btn).toHaveTextContent('Like · 1')
})

The test finds the button by its role + accessible name and asserts the visible label — so it survives a refactor of the internals, yet still fails the moment real behaviour breaks.

↑ play a little first, then dig in