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: This module provides some configuration options:
#### snapshotPath #### snapshot.snapshotPath
Sets the default Path for saving Snapshots (default: `cypress/snapshots`) Sets the default Path for saving Snapshots (default: `cypress/snapshots`)
![Config Screenshot](./.github/assets/config.png)
#### `ENV` CYPRESS_UPDATE_SNAPSHOTS #### `ENV` CYPRESS_UPDATE_SNAPSHOTS
Lets you pass a Env Variable to update failing Tests with the new Data 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" "url": "https://github.com/datashard/snapshot.git"
}, },
"scripts": { "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}\";", "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", "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",
@ -43,15 +37,9 @@
"cypress:run": "cypress run" "cypress:run": "cypress run"
}, },
"devDependencies": { "devDependencies": {
"ban-sensitive-files": "1.9.15",
"cypress": "10.6.0", "cypress": "10.6.0",
"debug": "3.2.7", "debug": "3.2.7",
"dependency-check": "2.10.1", "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"
}, },
"dependencies": { "dependencies": {
"@wildpeaks/snapshot-dom": "1.6.0", "@wildpeaks/snapshot-dom": "1.6.0",

View file

@ -12,34 +12,17 @@ const pickSerializer = (asJson, value) => {
return identity; return identity;
}; };
let counters = {};
const newStore = (name) => { const newStore = (name) => {
return initStore(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 store_snapshot = (store, props = { value, name, path, raiser }) => {
const fileName = props.name const expectedPath = path.join(
.join("_")
.replace(/ /gi, "-")
.replace(/\//gi, "-");
const snapshotPath =
props.path || props.path ||
Cypress.config("snapshot").snapshotPath || Cypress.config("snapshot").snapshotPath ||
"cypress/snapshots"; "cypress/snapshots",
`${props.name.join("_").replace(/ /gi, "-").replace(/\//gi, "-")}.json`
const expectedPath = path.join(snapshotPath, `${fileName}.json`); );
cy.task("readFileMaybe", expectedPath).then((exist) => { cy.task("readFileMaybe", expectedPath).then((exist) => {
if (exist && !Cypress.env().SNAPSHOT_UPDATE) { if (exist && !Cypress.env().SNAPSHOT_UPDATE) {
props.raiser({ value: props.value, expected: JSON.parse(exist) }); props.raiser({ value: props.value, expected: JSON.parse(exist) });
@ -55,8 +38,6 @@ const set_snapshot = (
) => { ) => {
if (!store) return; if (!store) return;
const message = Cypress._.last(snapshotName);
const devToolsLog = { $el: serialized }; const devToolsLog = { $el: serialized };
if (Cypress.dom.isJquery(value)) { if (Cypress.dom.isJquery(value)) {
@ -65,7 +46,7 @@ const set_snapshot = (
const options = { const options = {
name: "snapshot", name: "snapshot",
message, message: Cypress._.last(snapshotName),
consoleProps: () => devToolsLog, consoleProps: () => devToolsLog,
}; };
@ -75,10 +56,12 @@ const set_snapshot = (
const result = compareValues({ expected, value }); const result = compareValues({ expected, value });
if (!Cypress.env().SNAPSHOT_UPDATE && result.value) { if (!Cypress.env().SNAPSHOT_UPDATE && result.value) {
result.orElse((json) => { result.orElse((json) => {
devToolsLog.message = json.message; devToolsLog = {
devToolsLog.expected = expected; ...devToolsLog,
delete devToolsLog.value; message: json.message,
devToolsLog.value = value; expected,
value,
};
throw new Error( throw new Error(
`Snapshot Difference.\nPlease Update the Snapshot\n\n\t${json.message}` `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)) if (typeof value !== "object" || Array.isArray(value))
value = { data: value }; value = { data: value };
const name = get_snapshot_name(
Cypress.currentTest,
options.snapshotName || step
);
const serializer = pickSerializer(options.json, value); const serializer = pickSerializer(options.json, value);
const serialized = serializer(value); const serialized = serializer(value);
const store = newStore(serialized || {}); const store = newStore(serialized || {});
set_snapshot(store, { set_snapshot(store, {
snapshotName: name, snapshotName: get_snapshot_name(
Cypress.currentTest,
options.snapshotName || step
),
snapshotPath: options.snapshotPath, snapshotPath: options.snapshotPath,
serialized, serialized,
value, value,