diff --git a/src/utils/snapshots/snapshot.js b/src/utils/snapshots/snapshot.js index 4c71221..104ceaa 100644 --- a/src/utils/snapshots/snapshot.js +++ b/src/utils/snapshots/snapshot.js @@ -12,34 +12,17 @@ const pickSerializer = (asJson, value) => { return identity; }; -let counters = {}; - const newStore = (name) => { return initStore(name); }; -const get_snapshot_key = (key) => { - if (key in counters) { - // eslint-disable-next-line immutable/no-mutation - counters[key] += 1; - } else { - // eslint-disable-next-line immutable/no-mutation - counters[key] = 1; - } - return counters[key]; -}; - const store_snapshot = (store, props = { value, name, path, raiser }) => { - const fileName = props.name - .join("_") - .replace(/ /gi, "-") - .replace(/\//gi, "-"); - const snapshotPath = + const expectedPath = path.join( props.path || - Cypress.config("snapshot").snapshotPath || - "cypress/snapshots"; - - const expectedPath = path.join(snapshotPath, `${fileName}.json`); + Cypress.config("snapshot").snapshotPath || + "cypress/snapshots", + `${props.name.join("_").replace(/ /gi, "-").replace(/\//gi, "-")}.json` + ); cy.task("readFileMaybe", expectedPath).then((exist) => { if (exist && !Cypress.env().SNAPSHOT_UPDATE) { props.raiser({ value: props.value, expected: JSON.parse(exist) }); @@ -55,8 +38,6 @@ const set_snapshot = ( ) => { if (!store) return; - const message = Cypress._.last(snapshotName); - const devToolsLog = { $el: serialized }; if (Cypress.dom.isJquery(value)) { @@ -65,7 +46,7 @@ const set_snapshot = ( const options = { name: "snapshot", - message, + message: Cypress._.last(snapshotName), consoleProps: () => devToolsLog, }; @@ -75,10 +56,12 @@ const set_snapshot = ( const result = compareValues({ expected, value }); if (!Cypress.env().SNAPSHOT_UPDATE && result.value) { result.orElse((json) => { - devToolsLog.message = json.message; - devToolsLog.expected = expected; - delete devToolsLog.value; - devToolsLog.value = value; + devToolsLog = { + ...devToolsLog, + message: json.message, + expected, + value, + }; throw new Error( `Snapshot Difference.\nPlease Update the Snapshot\n\n\t${json.message}` @@ -111,16 +94,15 @@ module.exports = (value, step, options) => { if (typeof value !== "object" || Array.isArray(value)) value = { data: value }; - const name = get_snapshot_name( - Cypress.currentTest, - options.snapshotName || step - ); const serializer = pickSerializer(options.json, value); const serialized = serializer(value); const store = newStore(serialized || {}); set_snapshot(store, { - snapshotName: name, + snapshotName: get_snapshot_name( + Cypress.currentTest, + options.snapshotName || step + ), snapshotPath: options.snapshotPath, serialized, value,