Last active
January 26, 2018 10:09
-
-
Save alexbeletsky/3823f434536063a7126eb4dd68fb6a58 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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