Menu

React.js ~Testing For SnapShot~
📰

React.js ~Testing For SnapShot~

DEV Community: webdev·Ogasawara Kakeru·22 days ago
#H49w4y84
#devtowebdev#devto#dev#test#button
Reading 0:00
15s threshold

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 .…

Continue reading — create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.