Automatically update Snapshots using Env Variable (#6)

This commit is contained in:
Joshua 2023-03-23 17:07:42 +01:00 committed by GitHub
parent 5a1a5981d8
commit 0d303b9da5
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 33 additions and 35 deletions

1
.gitignore vendored
View file

@ -2,3 +2,4 @@ node_modules/
.DS_Store .DS_Store
npm-debug.log npm-debug.log
cypress/videos/ cypress/videos/
cypress/screenshots/

View file

@ -118,6 +118,9 @@ This module provides some configuration options:
#### snapshotPath #### snapshotPath
Sets the default Path for saving Snapshots (default: `cypress/snapshots`) 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
# #

View file

@ -1,6 +1,6 @@
/* eslint-env mocha */ /* eslint-env mocha */
/* global cy */ /* global cy */
describe("@cypress/snapshot", () => { describe("@datashard/snapshot", () => {
context("simple types", () => { context("simple types", () => {
it("works with objects", () => { it("works with objects", () => {
cy.fixture("File2").snapshot({ cy.fixture("File2").snapshot({
@ -23,11 +23,19 @@ describe("@cypress/snapshot", () => {
}); });
}); });
it("works with arrays", () => { it(
cy.wrap([1, 2, 3]).snapshot({ "works with arrays",
{
env: {
SNAPSHOT_UPDATE: true,
},
},
() => {
cy.wrap([1, 2, 3, 4]).snapshot({
snapshotPath: "cypress/snapshots", snapshotPath: "cypress/snapshots",
snapshotName: "Arrays", snapshotName: "Arrays",
}); });
}); }
);
}); });
}); });

View file

@ -39,6 +39,7 @@
"unused-deps": "dependency-check --unused --no-dev . --entry src/index.js", "unused-deps": "dependency-check --unused --no-dev . --entry src/index.js",
"semantic-release": "semantic-release", "semantic-release": "semantic-release",
"cypress:open": "cypress open", "cypress:open": "cypress open",
"cypress:update": "cypress run --env SNAPSHOT_UPDATE=true",
"cypress:run": "cypress run" "cypress:run": "cypress run"
}, },
"devDependencies": { "devDependencies": {

View file

@ -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));
});
});

View file

@ -41,7 +41,7 @@ const store_snapshot = (store, props = { value, name, path, raiser }) => {
const expectedPath = path.join(snapshotPath, `${fileName}.json`); const expectedPath = path.join(snapshotPath, `${fileName}.json`);
cy.task("readFileMaybe", expectedPath).then((exist) => { cy.task("readFileMaybe", expectedPath).then((exist) => {
if (exist) { if (exist && !Cypress.env().SNAPSHOT_UPDATE) {
props.raiser({ value: props.value, expected: JSON.parse(exist) }); props.raiser({ value: props.value, expected: JSON.parse(exist) });
} else { } else {
cy.writeFile(expectedPath, JSON.stringify(props.value)); cy.writeFile(expectedPath, JSON.stringify(props.value));
@ -56,7 +56,6 @@ const set_snapshot = (
if (!store) return; if (!store) return;
const message = Cypress._.last(snapshotName); const message = Cypress._.last(snapshotName);
console.log("Current Snapshot name", snapshotName);
const devToolsLog = { $el: serialized }; const devToolsLog = { $el: serialized };
@ -74,6 +73,7 @@ const set_snapshot = (
const raiser = ({ value, expected }) => { const raiser = ({ value, expected }) => {
const result = compareValues({ expected, value }); const result = compareValues({ expected, value });
if (!Cypress.env().SNAPSHOT_UPDATE && result.value) {
result.orElse((json) => { result.orElse((json) => {
devToolsLog.message = json.message; devToolsLog.message = json.message;
devToolsLog.expected = expected; devToolsLog.expected = expected;
@ -81,9 +81,10 @@ const set_snapshot = (
devToolsLog.value = value; devToolsLog.value = value;
throw new Error( throw new Error(
`Snapshot Difference. To update, delete snapshot file and rerun test.\n${json.message}` `Snapshot Difference.\nPlease Update the Snapshot\n\n\t${json.message}`
); );
}); });
}
}; };
Cypress.log(options); 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 get_snapshot_name = (test, custom_name) => {
const names = get_test_name(test); const names = test.titlePath;
const index = custom_name; const index = custom_name;
names.push(String(index)); names.push(String(index));
@ -110,7 +110,6 @@ module.exports = (value, step, options) => {
if (typeof step === "object") options = step; if (typeof step === "object") options = step;
if (typeof value !== "object" || Array.isArray(value)) if (typeof value !== "object" || Array.isArray(value))
value = { data: value }; value = { data: value };
console.log("value", value);
const name = get_snapshot_name( const name = get_snapshot_name(
Cypress.currentTest, Cypress.currentTest,
@ -120,7 +119,6 @@ module.exports = (value, step, options) => {
const serialized = serializer(value); const serialized = serializer(value);
const store = newStore(serialized || {}); const store = newStore(serialized || {});
console.log({ step, options });
set_snapshot(store, { set_snapshot(store, {
snapshotName: name, snapshotName: name,
snapshotPath: options.snapshotPath, snapshotPath: options.snapshotPath,