mirror of
https://github.com/datashard/snapshot.git
synced 2024-12-22 01:47:35 +00:00
Find out a Way to redo the success check
This commit is contained in:
parent
f9f95315fe
commit
e0e49ea0de
7 changed files with 399 additions and 18268 deletions
|
@ -37,5 +37,11 @@ describe("@datashard/snapshot", () => {
|
|||
});
|
||||
}
|
||||
);
|
||||
it('works with more "complicated" Objects', () => {
|
||||
cy.fixture("Complex").snapshot({
|
||||
snapshotPath: 'cypress/snapshots',
|
||||
snapshotName: "Complex"
|
||||
})
|
||||
})
|
||||
});
|
||||
});
|
||||
|
|
14
cypress/fixtures/Complex.json
Normal file
14
cypress/fixtures/Complex.json
Normal file
|
@ -0,0 +1,14 @@
|
|||
{
|
||||
"status": 200,
|
||||
"response": {
|
||||
"array": [
|
||||
0,
|
||||
1,
|
||||
2,
|
||||
"Three"
|
||||
],
|
||||
"object": {
|
||||
"with": "more details"
|
||||
}
|
||||
}
|
||||
}
|
9
cypress/snapshots/Complex.json
Normal file
9
cypress/snapshots/Complex.json
Normal file
|
@ -0,0 +1,9 @@
|
|||
{
|
||||
"status": 200,
|
||||
"response": {
|
||||
"array": [0, 1, 2, "Three"],
|
||||
"object": {
|
||||
"with": "more details"
|
||||
}
|
||||
}
|
||||
}
|
18481
package-lock.json
generated
18481
package-lock.json
generated
File diff suppressed because it is too large
Load diff
|
@ -43,8 +43,10 @@
|
|||
},
|
||||
"dependencies": {
|
||||
"@wildpeaks/snapshot-dom": "1.6.0",
|
||||
"chai": "4.3.10",
|
||||
"check-more-types": "2.24.0",
|
||||
"js-beautify": "1.13.13",
|
||||
"jsondiffpatch": "0.5.0",
|
||||
"lazy-ass": "1.6.0",
|
||||
"snap-shot-compare": "3.0.0",
|
||||
"snap-shot-store": "1.2.3"
|
||||
|
|
|
@ -1,6 +1,115 @@
|
|||
const compare = require("snap-shot-compare");
|
||||
module.exports = function compareValues({ expected, value }) {
|
||||
const noColor = false;
|
||||
const json = true;
|
||||
return compare({ expected, value, noColor, json });
|
||||
// const { expect: chaiExpect } = require("chai");
|
||||
|
||||
const isNestedData = (expected, value) => {
|
||||
return (
|
||||
expected && value && typeof expected == `object` && typeof value == `object`
|
||||
);
|
||||
};
|
||||
|
||||
const checkDataState = (expected, value) => {
|
||||
let result;
|
||||
|
||||
if (expected === value) {
|
||||
result = `| ✅ "${value}",`;
|
||||
} else if (expected == undefined) {
|
||||
result = `| ➖ "➖╺ ${expected}|${value}",`;
|
||||
} else if (value == undefined) {
|
||||
result = `| ➕ "➕┿ ${expected}|${value}",`;
|
||||
} else {
|
||||
result = `| ⭕ "⭕╳ ${expected}|${value}",`;
|
||||
}
|
||||
|
||||
return result;
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
*
|
||||
* @param {string} text
|
||||
* @returns {string}
|
||||
*/
|
||||
|
||||
function parseTextToJSON(text) {
|
||||
const lines =
|
||||
text
|
||||
.replace(/\| [✅➖➕⭕]/g, "").trim()
|
||||
.replace(/(.*?),\s*(\}|])/g, "$1$2");
|
||||
return lines;
|
||||
// return JSON.stringify(lines, null, 2);
|
||||
}
|
||||
|
||||
function containsDiffChars(str) {
|
||||
const emojis = ["╺", "┿", "╳"];
|
||||
return emojis.some(emoji => str.includes(emoji));
|
||||
}
|
||||
|
||||
const compare = (expected, value) => {
|
||||
let compareResult = "";
|
||||
let compareSuccess = true;
|
||||
|
||||
if (isNestedData(expected, value)) {
|
||||
if (Array.isArray(expected)) {
|
||||
compareResult += `[`;
|
||||
let dataX, dataY;
|
||||
|
||||
if (expected.length >= value.length) {
|
||||
dataX = expected;
|
||||
dataY = value;
|
||||
} else {
|
||||
dataX = value;
|
||||
dataY = expected;
|
||||
}
|
||||
|
||||
dataX.forEach(function (item, index) {
|
||||
const resultset = compare(item, dataY[index]);
|
||||
compareSuccess = resultset.success;
|
||||
compareResult += resultset.result;
|
||||
});
|
||||
compareResult += `],`;
|
||||
} else {
|
||||
let dataX, dataY;
|
||||
compareResult += `{`;
|
||||
if (Object.keys(expected).length >= Object.keys(value).length) {
|
||||
dataX = expected;
|
||||
dataY = value;
|
||||
} else {
|
||||
dataX = value;
|
||||
dataY = expected;
|
||||
}
|
||||
|
||||
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);
|
||||
// let result = compareResult;
|
||||
|
||||
try {
|
||||
return {
|
||||
success: !containsDiffChars(result),
|
||||
result,
|
||||
};
|
||||
} catch (error) {
|
||||
return { success: false, result };
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
*
|
||||
* @param {{
|
||||
* expected: { [k:string]: any},
|
||||
* value: {[k:string]:any}
|
||||
* }} values
|
||||
*/
|
||||
|
||||
module.exports = function compareValues(values) {
|
||||
return compare(values.expected, values.value);
|
||||
};
|
||||
|
|
|
@ -16,7 +16,7 @@ const newStore = (name) => {
|
|||
return initStore(name);
|
||||
};
|
||||
|
||||
const store_snapshot = (store, props = { value, name, path, raiser}) => {
|
||||
const store_snapshot = (store, props = { value, name, path, raiser }) => {
|
||||
const expectedPath = path.join(
|
||||
props.path ||
|
||||
Cypress.config("snapshot").snapshotPath ||
|
||||
|
@ -54,19 +54,29 @@ const set_snapshot = (
|
|||
|
||||
const raiser = ({ value, expected }) => {
|
||||
const result = compareValues({ expected, value });
|
||||
if (!Cypress.env().SNAPSHOT_UPDATE && result.value) {
|
||||
result.orElse((json) => {
|
||||
devToolsLog = {
|
||||
...devToolsLog,
|
||||
message: json.message,
|
||||
expected,
|
||||
value,
|
||||
};
|
||||
|
||||
throw new Error(
|
||||
`Snapshot Difference found.\nPlease Update the Snapshot\n\n${json.message.replaceAll(' ', ' ')}`
|
||||
);
|
||||
});
|
||||
if (!Cypress.env().SNAPSHOT_UPDATE && !result.success) {
|
||||
devToolsLog = {
|
||||
...devToolsLog,
|
||||
message: result,
|
||||
expected,
|
||||
value,
|
||||
};
|
||||
|
||||
// ╺
|
||||
// ┿
|
||||
// ╳
|
||||
|
||||
throw new Error(
|
||||
`Snapshot Difference found.\nPlease Update the Snapshot\n\n${JSON.stringify(
|
||||
JSON.parse(result.result),
|
||||
null,
|
||||
2
|
||||
)
|
||||
.replaceAll(" ", " ")
|
||||
.replaceAll(/[╺┿╳]/g, "")}`
|
||||
// `Snapshot Difference found.\nPlease Update the Snapshot\n\n${result.result}`
|
||||
);
|
||||
}
|
||||
};
|
||||
Cypress.log(options);
|
||||
|
|
Loading…
Reference in a new issue