Testing React Applications

A React 7,897 test is not a language test. The function under test returns a tree of objects, and what you want to assert about — "the alert says the card was declined" — exists only after a renderer has turned that tree into DOM nodes. Testing React means assembling three pieces: a runner, a DOM implementation, and a query library that inspects the result the way a user would.

The four pieces that run a React component test
The four pieces that run a React component test

Everything here ran on Node 25.8.0 with Vitest 5.0.1 102,989 , React 19.3.0, React Testing Library 16.3.3 221,825 , jsdom 30.1.0 21,691 , user-event 14.6.7, jest-dom 7.0.1 and MSW 2.15.0 668,066 ; the output is copied from those runs.

Subsections