From ce42d28dd16c3d03717e8b0813c1896dc8b7b0dd Mon Sep 17 00:00:00 2001 From: Joshua Date: Tue, 5 Dec 2023 10:18:21 +0100 Subject: [PATCH] chore: commit --- cypress/e2e/1.cy.js | 10 +++++++ cypress/snapshots/Complex.json | 9 ++++-- src/utils/snapshots/compareValues.js | 5 ---- src/utils/snapshots/snapshot.js | 42 +++++++++++++++++++--------- src/utils/tasks/readFileMaybe.js | 10 ++----- 5 files changed, 48 insertions(+), 28 deletions(-) diff --git a/cypress/e2e/1.cy.js b/cypress/e2e/1.cy.js index d1dc177..d75be23 100644 --- a/cypress/e2e/1.cy.js +++ b/cypress/e2e/1.cy.js @@ -43,5 +43,15 @@ describe("@datashard/snapshot", () => { snapshotName: "Complex" }) }) + it('random test string',{ + env: { + SNAPSHOT_UPDATE: true, + }, + }, () => { + cy.wrap([1, 2, 3, 4]).snapshot({ + snapshotPath: 'cypress/snapshots', + snapshotName: "rndmtst" + }) + }) }); }); diff --git a/cypress/snapshots/Complex.json b/cypress/snapshots/Complex.json index 25a8f3b..1de1100 100644 --- a/cypress/snapshots/Complex.json +++ b/cypress/snapshots/Complex.json @@ -1,9 +1,14 @@ { "status": 200, "response": { - "array": [0, 1, 2, "Three"], + "array": [ + 0, + 1, + 2, + "Three" + ], "object": { "with": "more details" } } -} +} \ No newline at end of file diff --git a/src/utils/snapshots/compareValues.js b/src/utils/snapshots/compareValues.js index bd05fef..47081c4 100644 --- a/src/utils/snapshots/compareValues.js +++ b/src/utils/snapshots/compareValues.js @@ -45,7 +45,6 @@ function containsDiffChars(str) { const compare = (expected, value) => { let compareResult = ""; - let compareSuccess = true; if (isNestedData(expected, value)) { if (Array.isArray(expected)) { @@ -62,7 +61,6 @@ const compare = (expected, value) => { dataX.forEach(function (item, index) { const resultset = compare(item, dataY[index]); - compareSuccess = resultset.success; compareResult += resultset.result; }); compareResult += `],`; @@ -79,14 +77,11 @@ const compare = (expected, value) => { Object.keys(dataX).forEach((key) => { const resultset = compare(dataX[key], dataY[key]); - - compareSuccess = resultset.success; compareResult += `"${key}": ${resultset.result}`; }); compareResult += `}`; } } else { - compareSuccess = false; compareResult = checkDataState(expected, value); } let result = parseTextToJSON(compareResult); diff --git a/src/utils/snapshots/snapshot.js b/src/utils/snapshots/snapshot.js index b937bf4..2c478ae 100644 --- a/src/utils/snapshots/snapshot.js +++ b/src/utils/snapshots/snapshot.js @@ -16,20 +16,40 @@ const newStore = (name) => { return initStore(name); }; +const writeSnapshot = (path, value) => { + return cy.writeFile(path, JSON.stringify(value, null, 2)); +} + const store_snapshot = (store, props = { value, name, path, raiser }) => { + const err = { + mes: null + } const expectedPath = path.join( props.path || - Cypress.config("snapshot").snapshotPath || - "cypress/snapshots", + 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) }); - } else { - cy.writeFile(expectedPath, JSON.stringify(props.value, null, 2)); - } - }); + + // cy.on('fail', (error, runnable) => { + // if (error.message.includes('failed because the file does not exist at the following path')) { + // err.mes = 'didNotExist' + // } + // writeSnapshot(expectedPath, props.value) + // }) + + try { + cy.readFile(expectedPath).then((file) => { + if (file && !Cypress.env().SNAPSHOT_UPDATE) { + props.raiser({ value: props.value, expected: file }); + } else { + writeSnapshot(expectedPath, props.value) + } + }) + + } catch (error) { + writeSnapshot(expectedPath, props.value) + } }; const set_snapshot = ( @@ -63,10 +83,6 @@ const set_snapshot = ( value, }; - // ╺ - // ┿ - // ╳ - throw new Error( `Snapshot Difference found.\nPlease Update the Snapshot\n\n${JSON.stringify( JSON.parse(result.result), diff --git a/src/utils/tasks/readFileMaybe.js b/src/utils/tasks/readFileMaybe.js index 192c432..226924c 100644 --- a/src/utils/tasks/readFileMaybe.js +++ b/src/utils/tasks/readFileMaybe.js @@ -1,9 +1,3 @@ -const fs = require("fs"); - -module.exports = (filename) => { - if (fs.existsSync(filename)) { - return fs.readFileSync(filename, "utf8"); - } - - return false; +module.exports = () => { +return console.log("This file is not needed anymore, you can take the .tasks() function out of your Configs") };