diff --git a/.github/assets/config.png b/.github/assets/config.png new file mode 100644 index 0000000..ede80f5 Binary files /dev/null and b/.github/assets/config.png differ diff --git a/.npmignore b/.npmignore new file mode 100644 index 0000000..1d58452 --- /dev/null +++ b/.npmignore @@ -0,0 +1,2 @@ +cypress/ +cypress.config.js \ No newline at end of file diff --git a/README.md b/README.md index 33459ba..1b33e11 100644 --- a/README.md +++ b/README.md @@ -115,9 +115,12 @@ cy.get(...).snapshot("Intercepted API Request", { This module provides some configuration options: -#### snapshotPath +#### snapshot.snapshotPath Sets the default Path for saving Snapshots (default: `cypress/snapshots`) + +![Config Screenshot](./.github/assets/config.png) + #### `ENV` CYPRESS_UPDATE_SNAPSHOTS Lets you pass a Env Variable to update failing Tests with the new Data diff --git a/package.json b/package.json index 45fcd61..c0204ee 100644 --- a/package.json +++ b/package.json @@ -29,13 +29,7 @@ "url": "https://github.com/datashard/snapshot.git" }, "scripts": { - "ban": "ban", - "deps": "deps-ok && dependency-check --no-dev .", - "issues": "git-issues", - "license": "license-checker --production --onlyunknown --csv", "size": "t=\"$(npm pack .)\"; wc -c \"${t}\"; tar tvf \"${t}\"; rm \"${t}\";", - "test": "npm run unit", - "unit": "mocha src/*-spec.js", "unused-deps": "dependency-check --unused --no-dev . --entry src/index.js", "semantic-release": "semantic-release", "cypress:open": "cypress open", @@ -43,15 +37,9 @@ "cypress:run": "cypress run" }, "devDependencies": { - "ban-sensitive-files": "1.9.15", "cypress": "10.6.0", "debug": "3.2.7", - "dependency-check": "2.10.1", - "deps-ok": "1.4.1", - "git-issues": "1.3.1", - "license-checker": "15.0.0", - "mocha": "6.2.3", - "semantic-release": "17.4.3" + "dependency-check": "2.10.1" }, "dependencies": { "@wildpeaks/snapshot-dom": "1.6.0", diff --git a/src/utils/snapshots/snapshot.js b/src/utils/snapshots/snapshot.js index 4c71221..104ceaa 100644 --- a/src/utils/snapshots/snapshot.js +++ b/src/utils/snapshots/snapshot.js @@ -12,34 +12,17 @@ const pickSerializer = (asJson, value) => { return identity; }; -let counters = {}; - const newStore = (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 fileName = props.name - .join("_") - .replace(/ /gi, "-") - .replace(/\//gi, "-"); - const snapshotPath = + const expectedPath = path.join( props.path || - Cypress.config("snapshot").snapshotPath || - "cypress/snapshots"; - - const expectedPath = path.join(snapshotPath, `${fileName}.json`); + 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) }); @@ -55,8 +38,6 @@ const set_snapshot = ( ) => { if (!store) return; - const message = Cypress._.last(snapshotName); - const devToolsLog = { $el: serialized }; if (Cypress.dom.isJquery(value)) { @@ -65,7 +46,7 @@ const set_snapshot = ( const options = { name: "snapshot", - message, + message: Cypress._.last(snapshotName), consoleProps: () => devToolsLog, }; @@ -75,10 +56,12 @@ const set_snapshot = ( const result = compareValues({ expected, value }); if (!Cypress.env().SNAPSHOT_UPDATE && result.value) { result.orElse((json) => { - devToolsLog.message = json.message; - devToolsLog.expected = expected; - delete devToolsLog.value; - devToolsLog.value = value; + devToolsLog = { + ...devToolsLog, + message: json.message, + expected, + value, + }; throw new Error( `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)) value = { data: value }; - const name = get_snapshot_name( - Cypress.currentTest, - options.snapshotName || step - ); const serializer = pickSerializer(options.json, value); const serialized = serializer(value); const store = newStore(serialized || {}); set_snapshot(store, { - snapshotName: name, + snapshotName: get_snapshot_name( + Cypress.currentTest, + options.snapshotName || step + ), snapshotPath: options.snapshotPath, serialized, value,