toMatchSnapshot in React Testing Library
toMatchSnapshot is a matcher in Jest (and Vitest). Used together with React Testing Library, it saves a component's output as a "snapshot" and detects differences in later test runs .
Basic Usage
import { render } from ' @testing-library/react ' ; import { Button } from ' ./Button ' ;
test ( ' Button renders as expected ' , () => { const { container } = render (< Button label = "Submit" />); expect ( container ). toMatchSnapshot (); });
Enter fullscreen mode
Exit fullscreen mode
How It Works
First run : The rendered result (DOM structure) is saved to a file such as __snapshots__/Button.test.tsx.snap .…