1
0
Fork 0
mirror of https://github.com/datashard/snapshot.git synced 2025-05-16 20:27:22 +00:00

remove unused code and overall clean up a bit

This commit is contained in:
Joshua 2023-03-24 14:40:01 +01:00
parent c7187cb05e
commit 0d27b308ef

View file

@ -12,34 +12,17 @@ const pickSerializer = (asJson, value) => {
return identity; return identity;
}; };
let counters = {};
const newStore = (name) => { const newStore = (name) => {
return initStore(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 store_snapshot = (store, props = { value, name, path, raiser }) => {
const fileName = props.name const expectedPath = path.join(
.join("_")
.replace(/ /gi, "-")
.replace(/\//gi, "-");
const snapshotPath =
props.path || props.path ||
Cypress.config("snapshot").snapshotPath || Cypress.config("snapshot").snapshotPath ||
"cypress/snapshots"; "cypress/snapshots",
`${props.name.join("_").replace(/ /gi, "-").replace(/\//gi, "-")}.json`
const expectedPath = path.join(snapshotPath, `${fileName}.json`); );
cy.task("readFileMaybe", expectedPath).then((exist) => { cy.task("readFileMaybe", expectedPath).then((exist) => {
if (exist && !Cypress.env().SNAPSHOT_UPDATE) { if (exist && !Cypress.env().SNAPSHOT_UPDATE) {
props.raiser({ value: props.value, expected: JSON.parse(exist) }); props.raiser({ value: props.value, expected: JSON.parse(exist) });
@ -55,8 +38,6 @@ const set_snapshot = (
) => { ) => {
if (!store) return; if (!store) return;
const message = Cypress._.last(snapshotName);
const devToolsLog = { $el: serialized }; const devToolsLog = { $el: serialized };
if (Cypress.dom.isJquery(value)) { if (Cypress.dom.isJquery(value)) {
@ -65,7 +46,7 @@ const set_snapshot = (
const options = { const options = {
name: "snapshot", name: "snapshot",
message, message: Cypress._.last(snapshotName),
consoleProps: () => devToolsLog, consoleProps: () => devToolsLog,
}; };
@ -75,10 +56,12 @@ const set_snapshot = (
const result = compareValues({ expected, value }); const result = compareValues({ expected, value });
if (!Cypress.env().SNAPSHOT_UPDATE && result.value) { if (!Cypress.env().SNAPSHOT_UPDATE && result.value) {
result.orElse((json) => { result.orElse((json) => {
devToolsLog.message = json.message; devToolsLog = {
devToolsLog.expected = expected; ...devToolsLog,
delete devToolsLog.value; message: json.message,
devToolsLog.value = value; expected,
value,
};
throw new Error( throw new Error(
`Snapshot Difference.\nPlease Update the Snapshot\n\n\t${json.message}` `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)) if (typeof value !== "object" || Array.isArray(value))
value = { data: value }; value = { data: value };
const name = get_snapshot_name(
Cypress.currentTest,
options.snapshotName || step
);
const serializer = pickSerializer(options.json, value); const serializer = pickSerializer(options.json, value);
const serialized = serializer(value); const serialized = serializer(value);
const store = newStore(serialized || {}); const store = newStore(serialized || {});
set_snapshot(store, { set_snapshot(store, {
snapshotName: name, snapshotName: get_snapshot_name(
Cypress.currentTest,
options.snapshotName || step
),
snapshotPath: options.snapshotPath, snapshotPath: options.snapshotPath,
serialized, serialized,
value, value,