Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
import { SnapshotState, toMatchSnapshot } from ‘jest-snapshot’;
export function toMatchSnapshot(actual, testFile, testTitle) {
// Intilize the SnapshotState, it’s responsible for actually matching
 // actual snapshot with expected one and storing results to `__snapshots__` folder
 const snapshotState = new SnapshotState(testFile, {
  updateSnapshot: process.env.SNAPSHOT_UPDATE ? ‘all’ : ‘new’,
// Bind the `toMatchSnapshot` to the object with snapshotState and
 // currentTest name, as `toMatchSnapshot` expects it as it’s `this`
 // object members
 const matcher = toMatchSnapshot.bind({
  currentTestName: testTitle,
// Execute the matcher
 const result = matcher(actual);
// Store the state of snapshot, depending on updateSnapshot value;
// Return results outside
 return result;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment