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
npm-debug.log
cypress/videos/
cypress/screenshots/

View file

@ -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
#

View file

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

View file

@ -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": {

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`);
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,