Skip to content

Instantly share code, notes, and snippets.

@alexbeletsky
Last active January 26, 2018 10:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alexbeletsky/3823f434536063a7126eb4dd68fb6a58 to your computer and use it in GitHub Desktop.
Save alexbeletsky/3823f434536063a7126eb4dd68fb6a58 to your computer and use it in GitHub Desktop.
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({
  snapshotState,
  currentTestName: testTitle,
 });
// Execute the matcher
 const result = matcher(actual);
// Store the state of snapshot, depending on updateSnapshot value
 snapshotState.save();
// Return results outside
 return result;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment