chore: commit

This commit is contained in:
Joshua 2023-12-05 10:18:21 +01:00
parent 1fe4562b9f
commit ce42d28dd1
5 changed files with 48 additions and 28 deletions

View file

@ -43,5 +43,15 @@ describe("@datashard/snapshot", () => {
snapshotName: "Complex" snapshotName: "Complex"
}) })
}) })
it('random test string',{
env: {
SNAPSHOT_UPDATE: true,
},
}, () => {
cy.wrap([1, 2, 3, 4]).snapshot({
snapshotPath: 'cypress/snapshots',
snapshotName: "rndmtst"
})
})
}); });
}); });

View file

@ -1,7 +1,12 @@
{ {
"status": 200, "status": 200,
"response": { "response": {
"array": [0, 1, 2, "Three"], "array": [
0,
1,
2,
"Three"
],
"object": { "object": {
"with": "more details" "with": "more details"
} }

View file

@ -45,7 +45,6 @@ function containsDiffChars(str) {
const compare = (expected, value) => { const compare = (expected, value) => {
let compareResult = ""; let compareResult = "";
let compareSuccess = true;
if (isNestedData(expected, value)) { if (isNestedData(expected, value)) {
if (Array.isArray(expected)) { if (Array.isArray(expected)) {
@ -62,7 +61,6 @@ const compare = (expected, value) => {
dataX.forEach(function (item, index) { dataX.forEach(function (item, index) {
const resultset = compare(item, dataY[index]); const resultset = compare(item, dataY[index]);
compareSuccess = resultset.success;
compareResult += resultset.result; compareResult += resultset.result;
}); });
compareResult += `],`; compareResult += `],`;
@ -79,14 +77,11 @@ const compare = (expected, value) => {
Object.keys(dataX).forEach((key) => { Object.keys(dataX).forEach((key) => {
const resultset = compare(dataX[key], dataY[key]); const resultset = compare(dataX[key], dataY[key]);
compareSuccess = resultset.success;
compareResult += `"${key}": ${resultset.result}`; compareResult += `"${key}": ${resultset.result}`;
}); });
compareResult += `}`; compareResult += `}`;
} }
} else { } else {
compareSuccess = false;
compareResult = checkDataState(expected, value); compareResult = checkDataState(expected, value);
} }
let result = parseTextToJSON(compareResult); let result = parseTextToJSON(compareResult);

View file

@ -16,20 +16,40 @@ const newStore = (name) => {
return initStore(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 store_snapshot = (store, props = { value, name, path, raiser }) => {
const err = {
mes: null
}
const expectedPath = path.join( const expectedPath = path.join(
props.path || props.path ||
Cypress.config("snapshot").snapshotPath || Cypress.config("snapshot").snapshotPath ||
"cypress/snapshots", "cypress/snapshots",
`${props.name.join("_").replace(/ /gi, "-").replace(/\//gi, "-")}.json` `${props.name.join("_").replace(/ /gi, "-").replace(/\//gi, "-")}.json`
); );
cy.task("readFileMaybe", expectedPath).then((exist) => {
if (exist && !Cypress.env().SNAPSHOT_UPDATE) { // cy.on('fail', (error, runnable) => {
props.raiser({ value: props.value, expected: JSON.parse(exist) }); // if (error.message.includes('failed because the file does not exist at the following path')) {
} else { // err.mes = 'didNotExist'
cy.writeFile(expectedPath, JSON.stringify(props.value, null, 2)); // }
} // 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 = ( const set_snapshot = (
@ -63,10 +83,6 @@ const set_snapshot = (
value, value,
}; };
// ╺
// ┿
//
throw new Error( throw new Error(
`Snapshot Difference found.\nPlease Update the Snapshot\n\n${JSON.stringify( `Snapshot Difference found.\nPlease Update the Snapshot\n\n${JSON.stringify(
JSON.parse(result.result), JSON.parse(result.result),

View file

@ -1,9 +1,3 @@
const fs = require("fs"); module.exports = () => {
return console.log("This file is not needed anymore, you can take the .tasks() function out of your Configs")
module.exports = (filename) => {
if (fs.existsSync(filename)) {
return fs.readFileSync(filename, "utf8");
}
return false;
}; };