top of page
Logo der Online Agentur mdwp


Snapshots in ReactJS refers to a feature that is typically used in combination with Jest, a testing framework for JavaScript. These snapshots are like a picture of a component's output at a particular point in time. They help check whether a component's output changes unexpectedly when updates are made to the code.

Jest's snapshot feature allows you to compare your component's current rendered output with previously saved snapshots. If the two mismatch, Jest will flag the test as a failure. This makes it easy to detect unintended changes in the component's behavior.

If the changes are intentional, you can update the snapshot and Jest will use the updated version for subsequent tests. This ensures that snapshot tests stay relevant and accurate as the component evolves over time.

Here's a small code snippet that illustrates snapshot testing in ReactJS:

import React from 'react';
import renderer from 'react-test-renderer';
import Link from '../Link.react';

it('renders correctly', () => {
const tree = renderer
.create(<Link page="">Facebook</Link>)

This code imports a React component named 'Link' and creates a snapshot test for it. The `renderer.create` function is used to render the 'Link' component and the resulting tree is converted to JSON.

The `toMatchSnapshot` matcher function is then used in a Jest expectation to compare this JSON representation of the component's output to the stored snapshot. If the JSON doesn't match the snapshot (because the component's output has changed), Jest will trigger an error and the test will fail.

bottom of page