Refactor for smaller Size (#8)

* remove unneeded packages and remove cypress tests from final package

* remove unused code and overall clean up a bit

* adds example to config in readme

* fix image

* fix config.png and extend npmignore
This commit is contained in:
Joshua 2023-03-27 16:05:37 +02:00 committed by GitHub
parent 0d303b9da5
commit fea6332aa8
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 23 additions and 48 deletions

BIN
.github/assets/config.png vendored Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 55 KiB

2
.npmignore Normal file
View file

@ -0,0 +1,2 @@
cypress/
cypress.config.js

View file

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

View file

@ -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",

View file

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