From 5db74ebcb6352af3ad148cfe64a2ba939718ee54 Mon Sep 17 00:00:00 2001 From: Joshua Date: Mon, 29 Jan 2024 19:08:06 +0100 Subject: [PATCH] mass changes --- .vscode/settings.json | 2 + cypress.config.js | 7 +- cypress/e2e/1.cy.js | 141 +++++------ cypress/fixtures/File.json | 4 - cypress/fixtures/File2.json | 4 - .../works-based-on-fixtures.json | 14 ++ .../works-with-more-complicated-Objects.json} | 0 .../simple-types/works-based-on-fixtures.json | 14 ++ .../simple-types/works-with-arrays.json} | 0 .../works-with-more-complicated-Objects.json} | 9 +- .../simple-types/works-with-numbers.json} | 0 .../simple-types/works-with-objects.json} | 0 .../simple-types/works-with-strings.json} | 0 cypress/snapshots/Fixture-File.json | 1 - cypress/snapshots/Random-Fixture-File.json | 4 - cypress/support/commands.js | 2 +- src/index.js | 5 +- src/{utils/functions => }/register.js | 26 +- src/{utils/snapshots => }/snapshot.js | 209 ++++++++-------- src/utils/{snapshots => }/compareValues.js | 230 +++++++++--------- src/utils/functions/addTasks.js | 5 - src/utils/index.js | 28 --- src/utils/tasks/readFileMaybe.js | 9 - 23 files changed, 356 insertions(+), 358 deletions(-) create mode 100644 .vscode/settings.json delete mode 100644 cypress/fixtures/File.json delete mode 100644 cypress/fixtures/File2.json create mode 100644 cypress/fixtures/snapshots/datashard-snapshot/complex-types/works-based-on-fixtures.json rename cypress/fixtures/{Complex.json => snapshots/datashard-snapshot/complex-types/works-with-more-complicated-Objects.json} (100%) create mode 100644 cypress/fixtures/snapshots/datashard-snapshot/simple-types/works-based-on-fixtures.json rename cypress/{snapshots/Arrays.json => fixtures/snapshots/datashard-snapshot/simple-types/works-with-arrays.json} (100%) rename cypress/{snapshots/Complex.json => fixtures/snapshots/datashard-snapshot/simple-types/works-with-more-complicated-Objects.json} (58%) rename cypress/{snapshots/Numbers.json => fixtures/snapshots/datashard-snapshot/simple-types/works-with-numbers.json} (100%) rename cypress/{snapshots/Objects.json => fixtures/snapshots/datashard-snapshot/simple-types/works-with-objects.json} (100%) rename cypress/{snapshots/Strings.json => fixtures/snapshots/datashard-snapshot/simple-types/works-with-strings.json} (100%) delete mode 100644 cypress/snapshots/Fixture-File.json delete mode 100644 cypress/snapshots/Random-Fixture-File.json rename src/{utils/functions => }/register.js (85%) rename src/{utils/snapshots => }/snapshot.js (55%) rename src/utils/{snapshots => }/compareValues.js (96%) delete mode 100644 src/utils/functions/addTasks.js delete mode 100644 src/utils/index.js delete mode 100644 src/utils/tasks/readFileMaybe.js diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..7a73a41 --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,2 @@ +{ +} \ No newline at end of file diff --git a/cypress.config.js b/cypress.config.js index 890cf53..94f9cd0 100644 --- a/cypress.config.js +++ b/cypress.config.js @@ -1,14 +1,15 @@ const { defineConfig } = require("cypress"); -const { functions } = require("./src/utils"); module.exports = defineConfig({ snapshot: { // snapshotPath: "cypress/snapshots/", - SNAPSHOT_UPDATE: true + // SNAPSHOT_UPDATE: true, + useFolders: true, }, + e2e: { setupNodeEvents(on, config) { - functions.tasks(on, config); + // implement node event listeners here }, }, }); diff --git a/cypress/e2e/1.cy.js b/cypress/e2e/1.cy.js index c7f0a09..65e8b3c 100644 --- a/cypress/e2e/1.cy.js +++ b/cypress/e2e/1.cy.js @@ -1,75 +1,78 @@ /* eslint-env mocha */ /* global cy */ -describe("@datashard/snapshot", () => { - context("simple types", () => { - it("works with objects", () => { - cy.wrap({ - "foo": "bar", - "Fizzy Drink": "Pop" - }).snapshot( - // "Filename", { - // snapshotPath: "asdasd" - // } +describe("datashard/snapshot", () => { + context("simple types" + // , { env: { SNAPSHOT_UPDATE: true } } + , () => { + it("works with objects", () => { + cy.wrap({ + "foo": "bar", + "Fizzy Drink": "Pop" + }).snapshot(); + }); + + it("works with numbers", () => { + cy.wrap(42).snapshot({ + snapshotPath: "cypress/fixtures/snapshots", + snapshotName: "Numbers", + }); + }); + + it("works with strings", () => { + cy.wrap("foo-bar").snapshot({ + snapshotPath: "cypress/fixtures/snapshots", + snapshotName: "Strings", + }); + }); + + it( + "works with arrays", + { + env: { + SNAPSHOT_UPDATE: true, + }, + }, + () => { + cy.wrap([1, 2, 3, 4]).snapshot({ + snapshotPath: "cypress/fixtures/snapshots", + snapshotName: "Arrays", + }); + } ); - // cypress/fixtures/@datashard-snapshot/simple-types/works-with-objects/asdjskadhasj.json }); - - // it("works with numbers", () => { - // cy.wrap(42).snapshot({ - // snapshotPath: "cypress/fixtures/snapshots", - // snapshotName: "Numbers", - // }); - // }); - - // it("works with strings", () => { - // cy.wrap("foo-bar").snapshot({ - // snapshotPath: "cypress/fixtures/snapshots", - // snapshotName: "Strings", - // }); - // }); - - // it( - // "works with arrays", - // { - // env: { - // SNAPSHOT_UPDATE: true, - // }, - // }, - // () => { - // cy.wrap([1, 2, 3, 4]).snapshot({ - // snapshotPath: "cypress/fixtures/snapshots", - // snapshotName: "Arrays", - // }); - // } - // ); - // it('works with more "complicated" Objects', () => { - // cy.fixture("Complex").snapshot({ - // snapshotPath: 'cypress/fixtures/snapshots', - // snapshotName: "Complex" - // }) - // }) - // it.only("works based on fixtures", () => { - // cy - // .wrap({ - // "jsonapi": { - // "version": "2.0" - // }, - // "included": [ - // { - // "type": "users", - // "id": "2", - // "attributes": { - // "name": "Test" - // } - // } - // ] - // }) - // .snapshot({ - // snapshotFixture: "generated", - // // snapshotPath: "cypress/fixtures/snapshots", - // // snapshotName: "generated", - // }); - // }); - }); + context("complex types" + // , { env: { SNAPSHOT_UPDATE: true } } + , () => { + it('works with more "complicated" Objects', () => { + cy.wrap({ + "status": 200, + "response": { + "array": [0, 1, 2, "Three"], + "object": { + "with": "more details" + } + } + } + ).snapshot() + }) + it("works based on fixtures", () => { + cy + .wrap({ + "jsonapi": { + "version": "2.0" + }, + "included": [ + { + "type": "users", + "id": "2", + "attributes": { + "name": "Test" + } + } + ] + }) + .snapshot(); + }); + }) }); diff --git a/cypress/fixtures/File.json b/cypress/fixtures/File.json deleted file mode 100644 index fe41954..0000000 --- a/cypress/fixtures/File.json +++ /dev/null @@ -1,4 +0,0 @@ -{ - "foo": "bar", - "Fizzy Drink": "Soda" -} diff --git a/cypress/fixtures/File2.json b/cypress/fixtures/File2.json deleted file mode 100644 index effe26b..0000000 --- a/cypress/fixtures/File2.json +++ /dev/null @@ -1,4 +0,0 @@ -{ - "foo": "bar", - "Fizzy Drink": "Pop" -} diff --git a/cypress/fixtures/snapshots/datashard-snapshot/complex-types/works-based-on-fixtures.json b/cypress/fixtures/snapshots/datashard-snapshot/complex-types/works-based-on-fixtures.json new file mode 100644 index 0000000..ac6239a --- /dev/null +++ b/cypress/fixtures/snapshots/datashard-snapshot/complex-types/works-based-on-fixtures.json @@ -0,0 +1,14 @@ +{ + "jsonapi": { + "version": "2.0" + }, + "included": [ + { + "type": "users", + "id": "2", + "attributes": { + "name": "Test" + } + } + ] +} \ No newline at end of file diff --git a/cypress/fixtures/Complex.json b/cypress/fixtures/snapshots/datashard-snapshot/complex-types/works-with-more-complicated-Objects.json similarity index 100% rename from cypress/fixtures/Complex.json rename to cypress/fixtures/snapshots/datashard-snapshot/complex-types/works-with-more-complicated-Objects.json diff --git a/cypress/fixtures/snapshots/datashard-snapshot/simple-types/works-based-on-fixtures.json b/cypress/fixtures/snapshots/datashard-snapshot/simple-types/works-based-on-fixtures.json new file mode 100644 index 0000000..ac6239a --- /dev/null +++ b/cypress/fixtures/snapshots/datashard-snapshot/simple-types/works-based-on-fixtures.json @@ -0,0 +1,14 @@ +{ + "jsonapi": { + "version": "2.0" + }, + "included": [ + { + "type": "users", + "id": "2", + "attributes": { + "name": "Test" + } + } + ] +} \ No newline at end of file diff --git a/cypress/snapshots/Arrays.json b/cypress/fixtures/snapshots/datashard-snapshot/simple-types/works-with-arrays.json similarity index 100% rename from cypress/snapshots/Arrays.json rename to cypress/fixtures/snapshots/datashard-snapshot/simple-types/works-with-arrays.json diff --git a/cypress/snapshots/Complex.json b/cypress/fixtures/snapshots/datashard-snapshot/simple-types/works-with-more-complicated-Objects.json similarity index 58% rename from cypress/snapshots/Complex.json rename to cypress/fixtures/snapshots/datashard-snapshot/simple-types/works-with-more-complicated-Objects.json index 25a8f3b..1de1100 100644 --- a/cypress/snapshots/Complex.json +++ b/cypress/fixtures/snapshots/datashard-snapshot/simple-types/works-with-more-complicated-Objects.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/cypress/snapshots/Numbers.json b/cypress/fixtures/snapshots/datashard-snapshot/simple-types/works-with-numbers.json similarity index 100% rename from cypress/snapshots/Numbers.json rename to cypress/fixtures/snapshots/datashard-snapshot/simple-types/works-with-numbers.json diff --git a/cypress/snapshots/Objects.json b/cypress/fixtures/snapshots/datashard-snapshot/simple-types/works-with-objects.json similarity index 100% rename from cypress/snapshots/Objects.json rename to cypress/fixtures/snapshots/datashard-snapshot/simple-types/works-with-objects.json diff --git a/cypress/snapshots/Strings.json b/cypress/fixtures/snapshots/datashard-snapshot/simple-types/works-with-strings.json similarity index 100% rename from cypress/snapshots/Strings.json rename to cypress/fixtures/snapshots/datashard-snapshot/simple-types/works-with-strings.json diff --git a/cypress/snapshots/Fixture-File.json b/cypress/snapshots/Fixture-File.json deleted file mode 100644 index 86ac8e3..0000000 --- a/cypress/snapshots/Fixture-File.json +++ /dev/null @@ -1 +0,0 @@ -{"foo":"bar","Fizzy Drink":"Soda"} \ No newline at end of file diff --git a/cypress/snapshots/Random-Fixture-File.json b/cypress/snapshots/Random-Fixture-File.json deleted file mode 100644 index cf78053..0000000 --- a/cypress/snapshots/Random-Fixture-File.json +++ /dev/null @@ -1,4 +0,0 @@ -{ - "foo": "bar", - "Fizzy Drink": "Soda" -} \ No newline at end of file diff --git a/cypress/support/commands.js b/cypress/support/commands.js index 56daf1d..4bea380 100644 --- a/cypress/support/commands.js +++ b/cypress/support/commands.js @@ -1,2 +1,2 @@ // register .snapshot() command -require('../..').register() +require('../../src/index').register() diff --git a/src/index.js b/src/index.js index c1cc876..763a201 100644 --- a/src/index.js +++ b/src/index.js @@ -1,7 +1,6 @@ // global cy, Cypress -const { functions } = require("./utils/index"); +const register = require("./register"); module.exports = { - register: functions.register, - tasks: functions.tasks, + register }; diff --git a/src/utils/functions/register.js b/src/register.js similarity index 85% rename from src/utils/functions/register.js rename to src/register.js index e1d8d0a..e1b1fa0 100644 --- a/src/utils/functions/register.js +++ b/src/register.js @@ -1,13 +1,13 @@ -const lazy = require("lazy-ass"); -const is = require("check-more-types"); -const snapshot = require("../snapshots/snapshot"); - -module.exports = () => { - lazy(is.fn(global.before), "Missing global before function"); - lazy(is.fn(global.after), "Missing global after function"); - lazy(is.object(global.Cypress), "Missing Cypress object"); - - Cypress.Commands.add("snapshot", { prevSubject: "optional" }, snapshot); - - return snapshot; -}; +const lazy = require("lazy-ass"); +const is = require("check-more-types"); +const snapshot = require("./snapshot"); + +module.exports = () => { + lazy(is.fn(global.before), "Missing global before function"); + lazy(is.fn(global.after), "Missing global after function"); + lazy(is.object(global.Cypress), "Missing Cypress object"); + + Cypress.Commands.add("snapshot", { prevSubject: "optional" }, snapshot); + + return snapshot; +}; diff --git a/src/utils/snapshots/snapshot.js b/src/snapshot.js similarity index 55% rename from src/utils/snapshots/snapshot.js rename to src/snapshot.js index 925a1dd..6679381 100644 --- a/src/utils/snapshots/snapshot.js +++ b/src/snapshot.js @@ -1,97 +1,112 @@ -const serializeDomElement = require("../serializers/serializeDomElement"); -const serializeToHTML = require("../serializers/serializeToHTML"); -const compareValues = require("./compareValues"); -const path = require("path"); -const identity = (x) => x; - -const pickSerializer = (asJson, value) => { - if (Cypress.dom.isJquery(value)) { - return asJson ? serializeDomElement : serializeToHTML; - } - return identity; -}; - -const store_snapshot = (props = { value, name, raiser }) => { - if (!Cypress.env().SNAPSHOT_UPDATE) { - cy.fixture(props.name).then(content => props.raiser({ value: props.value, expected: content })) - } else { - cy.writeFile(`${props.name}.json`, JSON.stringify(props.value.null, 2)) - } - - - // cy.fixture(props.name) - // .then(exist => { - // cy.log('fixture 2') - // if (exist && !Cypress.env().SNAPSHOT_UPDATE) { - // cy.log(`fixture exists and doesn't update`) - // props.raiser({ value: props.value, expected: exist, type: "fixture" }); - // } else { - // cy.log(`fixture exists and updates`) - // cy.writeFile(expectedPath, JSON.stringify(props.value, null, 2)); - // } - // }) -}; - -const set_snapshot = ({ snapshotName, serialized, value }) => { - let devToolsLog = { $el: serialized }; - if (Cypress.dom.isJquery(value)) { - devToolsLog.$el = value; - } - - const options = { - name: "snapshot", - message: snapshotName, - consoleProps: () => devToolsLog, - }; - - if (value) options.$el = value; - - const raiser = ({ value, expected }) => { - const result = compareValues({ expected, value }); - if (!Cypress.env().SNAPSHOT_UPDATE && !result.success) { - devToolsLog = { - ...devToolsLog, - message: result, - expected, - value, - }; - - throw new Error( - `Snapshot Difference found.\nPlease Update the Snapshot\n - - - ${JSON.stringify(result.result.replaceAll(/[╺┿╳]/g, ""), null, 2)}` - ); - } - }; - Cypress.log(options); - - store_snapshot({ - value, - name: snapshotName, - raiser, - }); -}; - -const get_snapshot_name = (asFolder, stepName) => { - const names = Cypress.currentTest.titlePath; - const sep = ">>datashard.work<<" - if (stepName) names.push(stepName) - - if (asFolder) return names.join(sep).replace(/ /gi, "-").replace(/\//gi, "-").replaceAll(sep, '/') - else return names.join('__').replaceAll(" ", "-").replaceAll("/", "-") -}; - -module.exports = (value, stepName, options = { json: true, asFolder: false }) => { - if (typeof value !== "object" || Array.isArray(value)) - value = { data: value }; - const serializer = pickSerializer(options.json, value); - const serialized = serializer(value); - set_snapshot({ - snapshotName: path.join( - options.snapshotPath || Cypress.config('snapshot').snapshotPath || 'snapshots', - `/${get_snapshot_name(options.asFolder, stepName)}`), - serialized, - value, - }); -}; +const serializeDomElement = require("./utils/serializers/serializeDomElement"); +const serializeToHTML = require("./utils/serializers/serializeToHTML"); +const compareValues = require("./utils/compareValues"); +const path = require("path"); +const identity = (x) => x; + +const pickSerializer = (asJson, value) => { + if (Cypress.dom.isJquery(value)) { + return asJson ? serializeDomElement : serializeToHTML; + } + return identity; +}; + +const store_snapshot = (props = { value, name, raiser }) => { + if (Cypress.env().SNAPSHOT_UPDATE || Cypress.config('snapshot').SNAPSHOT_UPDATE) { + cy.log(props.name) + console.log(props.name) + cy.writeFile(`${props.name}.json`, JSON.stringify(props.value, null, 2)) + } else { + // TODO: Figure out how to replace the fixture folder name if people move it + const fixtureName = props.name.replace("cypress/fixtures", "") + cy.fixture(fixtureName).then(content => props.raiser({ value: props.value, expected: content })) + } +}; + +const set_snapshot = ({ snapshotName, serialized, value }) => { + let devToolsLog = { $el: serialized }; + if (Cypress.dom.isJquery(value)) { + devToolsLog.$el = value; + } + + const options = { + name: "snapshot", + message: snapshotName, + consoleProps: () => devToolsLog, + }; + + if (value) options.$el = value; + + const raiser = ({ value, expected }) => { + const result = compareValues({ expected, value }); + if (!Cypress.env().SNAPSHOT_UPDATE && !result.success) { + devToolsLog = { + ...devToolsLog, + message: result, + expected, + value, + }; + + throw new Error( + `Snapshot Difference found.\nPlease Update the Snapshot\n + + + ${JSON.stringify(result.result.replaceAll(/[╺┿╳]/g, ""), null, 2)}` + ); + } + }; + Cypress.log(options); + + store_snapshot({ + value, + name: snapshotName, + raiser, + }); +}; + +function replaceCharacters(str, asFolder, sep) { + if (asFolder) { + if (!sep) throw new Error("Separator not Passed.") + return str + .replace(/ /gi, '-') + .replace(/\//gi, "-") + .replaceAll('"', '') + .replaceAll(sep, '/') + } else { + return str + .replaceAll(' ', '-') + .replaceAll('/', '-') + .replaceAll('"', '') + } +} + +const get_snapshot_name = (asFolder, stepName) => { + const names = Cypress.currentTest.titlePath; + const sep = ">>datashard.work<<" + + + if (stepName && typeof stepName !== 'object') { + names.push(stepName) + } + + + if (asFolder) return replaceCharacters(names.join(sep), true, sep) + else return replaceCharacters(names.join('__'), false) +}; + +module.exports = (value, stepName, options = { json: true }) => { + if (typeof stepName === 'object') options = { ...options, ...stepName } + if (typeof value !== "object" || Array.isArray(value)) + value = { data: value }; + const serializer = pickSerializer(options.json, value); + const serialized = serializer(value); + options.asFolder = Cypress.config('snapshot').useFolders || false + + set_snapshot({ + snapshotName: path.join( + options.snapshotPath || Cypress.config('snapshot').snapshotPath || 'cypress/fixtures/snapshots', + `/${get_snapshot_name(options.asFolder, stepName)}`), + serialized, + value, + }); +}; diff --git a/src/utils/snapshots/compareValues.js b/src/utils/compareValues.js similarity index 96% rename from src/utils/snapshots/compareValues.js rename to src/utils/compareValues.js index 510ef71..e9b0cf5 100644 --- a/src/utils/snapshots/compareValues.js +++ b/src/utils/compareValues.js @@ -1,115 +1,115 @@ -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 = `| ➖ "➖╺ ${JSON.stringify(expected)?.replaceAll('"', "'")}|${value?.replaceAll('"', "'")}",`; - } else if (value == undefined) { - result = `| ➕ "➕┿ ${JSON.stringify(expected)?.replaceAll('"', "'")}|${value?.replaceAll('"', "'")}",`; - } else { - result = `| ⭕ "⭕╳ ${JSON.stringify(expected)?.replaceAll('"', "'")}|${value?.replaceAll('"', "'")}",`; - } - - return result; -}; - - -/** - * - * @param {string} text - * @returns {string} - */ - -function parseTextToJSON(text) { - const lines = - // JSON.stringify( - 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) => { - if(value === undefined){ - throw new Error("Please provide Data to compare against.") - } - let compareResult = ""; - - 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]); - 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]); - compareResult += `"${key}": ${resultset.result}`; - }); - compareResult += `},`; - } - } else { - compareResult = checkDataState(expected, value); - } - let result = parseTextToJSON(compareResult).replace(/(},)$/g, `}`); - console.log("compareResult",compareResult) - console.log("result", result.replace(/(},)$/g, `}`)) - // 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); -}; +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 = `| ➖ "➖╺ ${JSON.stringify(expected)?.replaceAll('"', "'")}|${value?.replaceAll('"', "'")}",`; + } else if (value == undefined) { + result = `| ➕ "➕┿ ${JSON.stringify(expected)?.replaceAll('"', "'")}|${value?.replaceAll('"', "'")}",`; + } else { + result = `| ⭕ "⭕╳ ${JSON.stringify(expected)?.replaceAll('"', "'")}|${value?.replaceAll('"', "'")}",`; + } + + return result; +}; + + +/** + * + * @param {string} text + * @returns {string} + */ + +function parseTextToJSON(text) { + const lines = + // JSON.stringify( + 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) => { + if(value === undefined){ + throw new Error("Please provide Data to compare against.") + } + let compareResult = ""; + + 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]); + 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]); + compareResult += `"${key}": ${resultset.result}`; + }); + compareResult += `},`; + } + } else { + compareResult = checkDataState(expected, value); + } + let result = parseTextToJSON(compareResult).replace(/(},)$/g, `}`); + console.log("compareResult",compareResult) + console.log("result", result.replace(/(},)$/g, `}`)) + // 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); +}; diff --git a/src/utils/functions/addTasks.js b/src/utils/functions/addTasks.js deleted file mode 100644 index 4d01f92..0000000 --- a/src/utils/functions/addTasks.js +++ /dev/null @@ -1,5 +0,0 @@ -const readFileMaybe = require("../tasks/readFileMaybe"); - -module.exports = (on, config) => { - on("task", { readFileMaybe }); -}; diff --git a/src/utils/index.js b/src/utils/index.js deleted file mode 100644 index ae0b254..0000000 --- a/src/utils/index.js +++ /dev/null @@ -1,28 +0,0 @@ -const serializeToHTML = require("./serializers/serializeToHTML"); -const serializeDomElement = require("./serializers/serializeDomElement"); -const compareValues = require("./snapshots/compareValues"); -const readFileMaybe = require("./tasks/readFileMaybe"); -const identity = (x) => x; -const publicProps = (name) => !name.startsWith("__"); -const countSnapshots = (snapshots) => - Object.keys(snapshots).filter(publicProps).length; - -module.exports = { - serializers: { - serializeDomElement, - serializeToHTML, - identity, - countSnapshots, - }, - snapshots: { - compareValues, - }, - - functions: { - register: require("./functions/register"), - tasks: require("./functions/addTasks"), - }, - tasks: { - readFileMaybe, - }, -}; diff --git a/src/utils/tasks/readFileMaybe.js b/src/utils/tasks/readFileMaybe.js deleted file mode 100644 index 192c432..0000000 --- a/src/utils/tasks/readFileMaybe.js +++ /dev/null @@ -1,9 +0,0 @@ -const fs = require("fs"); - -module.exports = (filename) => { - if (fs.existsSync(filename)) { - return fs.readFileSync(filename, "utf8"); - } - - return false; -};