diff --git a/.gitignore b/.gitignore index 1416c6b..2a2fd2e 100644 --- a/.gitignore +++ b/.gitignore @@ -2,3 +2,4 @@ node_modules/ .DS_Store npm-debug.log cypress/videos/ +cypress/screenshots/ diff --git a/README.md b/README.md index 34a9a6b..33459ba 100644 --- a/README.md +++ b/README.md @@ -118,6 +118,9 @@ This module provides some configuration options: #### snapshotPath Sets the default Path for saving Snapshots (default: `cypress/snapshots`) +#### `ENV` CYPRESS_UPDATE_SNAPSHOTS + +Lets you pass a Env Variable to update failing Tests with the new Data # diff --git a/cypress/e2e/spec.cy.js b/cypress/e2e/1.cy.js similarity index 62% rename from cypress/e2e/spec.cy.js rename to cypress/e2e/1.cy.js index 9d2c9c1..f702c84 100644 --- a/cypress/e2e/spec.cy.js +++ b/cypress/e2e/1.cy.js @@ -1,6 +1,6 @@ /* eslint-env mocha */ /* global cy */ -describe("@cypress/snapshot", () => { +describe("@datashard/snapshot", () => { context("simple types", () => { it("works with objects", () => { cy.fixture("File2").snapshot({ @@ -23,11 +23,19 @@ describe("@cypress/snapshot", () => { }); }); - it("works with arrays", () => { - cy.wrap([1, 2, 3]).snapshot({ - snapshotPath: "cypress/snapshots", - snapshotName: "Arrays", - }); - }); + it( + "works with arrays", + { + env: { + SNAPSHOT_UPDATE: true, + }, + }, + () => { + cy.wrap([1, 2, 3, 4]).snapshot({ + snapshotPath: "cypress/snapshots", + snapshotName: "Arrays", + }); + } + ); }); }); diff --git a/cypress/e2e/Second Spec.cy.js b/cypress/e2e/2.cy.js similarity index 100% rename from cypress/e2e/Second Spec.cy.js rename to cypress/e2e/2.cy.js diff --git a/package.json b/package.json index 30e37d9..45fcd61 100644 --- a/package.json +++ b/package.json @@ -39,6 +39,7 @@ "unused-deps": "dependency-check --unused --no-dev . --entry src/index.js", "semantic-release": "semantic-release", "cypress:open": "cypress open", + "cypress:update": "cypress run --env SNAPSHOT_UPDATE=true", "cypress:run": "cypress run" }, "devDependencies": { diff --git a/src/snapshot-spec.js b/src/snapshot-spec.js deleted file mode 100644 index 947de69..0000000 --- a/src/snapshot-spec.js +++ /dev/null @@ -1,13 +0,0 @@ -const api = require("."); -const la = require("lazy-ass"); -const is = require("check-more-types"); - -describe("@datashard/snapshot", () => { - it("is an object", () => { - la(is.object(api)); - }); - - it("has register", () => { - la(is.fn(api.register)); - }); -}); diff --git a/src/utils/snapshots/snapshot.js b/src/utils/snapshots/snapshot.js index 9d31428..4c71221 100644 --- a/src/utils/snapshots/snapshot.js +++ b/src/utils/snapshots/snapshot.js @@ -41,7 +41,7 @@ const store_snapshot = (store, props = { value, name, path, raiser }) => { const expectedPath = path.join(snapshotPath, `${fileName}.json`); cy.task("readFileMaybe", expectedPath).then((exist) => { - if (exist) { + if (exist && !Cypress.env().SNAPSHOT_UPDATE) { props.raiser({ value: props.value, expected: JSON.parse(exist) }); } else { cy.writeFile(expectedPath, JSON.stringify(props.value)); @@ -56,7 +56,6 @@ const set_snapshot = ( if (!store) return; const message = Cypress._.last(snapshotName); - console.log("Current Snapshot name", snapshotName); const devToolsLog = { $el: serialized }; @@ -74,16 +73,18 @@ const set_snapshot = ( const raiser = ({ value, expected }) => { const result = compareValues({ expected, value }); - result.orElse((json) => { - devToolsLog.message = json.message; - devToolsLog.expected = expected; - delete devToolsLog.value; - devToolsLog.value = value; + if (!Cypress.env().SNAPSHOT_UPDATE && result.value) { + result.orElse((json) => { + devToolsLog.message = json.message; + devToolsLog.expected = expected; + delete devToolsLog.value; + devToolsLog.value = value; - throw new Error( - `Snapshot Difference. To update, delete snapshot file and rerun test.\n${json.message}` - ); - }); + throw new Error( + `Snapshot Difference.\nPlease Update the Snapshot\n\n\t${json.message}` + ); + }); + } }; Cypress.log(options); @@ -95,9 +96,8 @@ const set_snapshot = ( }); }; -const get_test_name = (test) => test.titlePath; const get_snapshot_name = (test, custom_name) => { - const names = get_test_name(test); + const names = test.titlePath; const index = custom_name; names.push(String(index)); @@ -110,7 +110,6 @@ module.exports = (value, step, options) => { if (typeof step === "object") options = step; if (typeof value !== "object" || Array.isArray(value)) value = { data: value }; - console.log("value", value); const name = get_snapshot_name( Cypress.currentTest, @@ -120,7 +119,6 @@ module.exports = (value, step, options) => { const serialized = serializer(value); const store = newStore(serialized || {}); - console.log({ step, options }); set_snapshot(store, { snapshotName: name, snapshotPath: options.snapshotPath,