UNPKG

earljs

Version:

Ergonomic, modern and type-safe assertion library

32 lines (31 loc) 1.4 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.compareSnapshotUsingJest = void 0; const jest_snapshot_1 = require("jest-snapshot"); // it is not only a optimization to cache jestSnapshotState per snapshot file but it's actually required so SnapshotState updates snapshot counter for the same test automatically // we do very basic caching here which should be fine as tests are run file by file let jestSnapshotState; let jestSnapshotStateForFile; const compareSnapshotUsingJest = ({ actual, updateSnapshotMode, snapshotFilePath, name }) => { if (jestSnapshotStateForFile !== snapshotFilePath) { jestSnapshotState = new jest_snapshot_1.SnapshotState(snapshotFilePath, { updateSnapshot: updateSnapshotMode, // these are only required for inline mocks so we ignore them getBabelTraverse: undefined, getPrettier: undefined, }); jestSnapshotStateForFile = snapshotFilePath; } const result = jest_snapshot_1.toMatchSnapshot.bind({ snapshotState: jestSnapshotState, currentTestName: name, })(actual); jestSnapshotState.save(); if (result.pass) { return { success: true }; } else { return { success: false, actual: result.actual, expected: result.expected }; } }; exports.compareSnapshotUsingJest = compareSnapshotUsingJest;