mirror of
https://github.com/datashard/snapshot.git
synced 2024-11-24 06:42:29 +00:00
Support Numbers Strings and Arrays
This commit is contained in:
parent
4cb9f8ca1d
commit
f5d6b7f63d
1 changed files with 33 additions and 27 deletions
|
@ -2,13 +2,9 @@ const serializeDomElement = require("../serializers/serializeDomElement");
|
||||||
const serializeToHTML = require("../serializers/serializeToHTML");
|
const serializeToHTML = require("../serializers/serializeToHTML");
|
||||||
const compareValues = require("./compareValues");
|
const compareValues = require("./compareValues");
|
||||||
const { initStore } = require("snap-shot-store");
|
const { initStore } = require("snap-shot-store");
|
||||||
// const itsName = require("its-name");
|
|
||||||
const path = require("path");
|
const path = require("path");
|
||||||
const identity = (x) => x;
|
const identity = (x) => x;
|
||||||
|
|
||||||
// Value = the JSON we want to store/compare
|
|
||||||
// name = the Human Readable name of this Snapshot
|
|
||||||
// json = decides if we convert DOM elements into JSON when storing in the snapshot file
|
|
||||||
const pickSerializer = (asJson, value) => {
|
const pickSerializer = (asJson, value) => {
|
||||||
if (Cypress.dom.isJquery(value)) {
|
if (Cypress.dom.isJquery(value)) {
|
||||||
return asJson ? serializeDomElement : serializeToHTML;
|
return asJson ? serializeDomElement : serializeToHTML;
|
||||||
|
@ -38,28 +34,32 @@ const store_snapshot = (store, props = { value, name, path, raiser }) => {
|
||||||
.join("_")
|
.join("_")
|
||||||
.replace(/ /gi, "-")
|
.replace(/ /gi, "-")
|
||||||
.replace(/\//gi, "-");
|
.replace(/\//gi, "-");
|
||||||
const snapshotPath = props.path || Cypress.config("snapshot").snapshotPath || "cypress/snapshots"
|
const snapshotPath =
|
||||||
// console.log(snapshotPath)
|
props.path ||
|
||||||
const expectedPath = path.join(snapshotPath, `${fileName}.json`);
|
Cypress.config("snapshot").snapshotPath ||
|
||||||
// console.log("\x1b[31m%s\x1b[30m", "file: path", expectedPath);
|
"cypress/snapshots";
|
||||||
|
|
||||||
|
const expectedPath = path.join(snapshotPath, `${fileName}.json`);
|
||||||
cy.task("readFileMaybe", expectedPath).then((exist) => {
|
cy.task("readFileMaybe", expectedPath).then((exist) => {
|
||||||
// console.log("\x1b[35m%s\x1b[30m", "file: exists", exist);
|
if (exist) {
|
||||||
if(exist){
|
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));
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
const set_snapshot = (store, { snapshotName, snapshotPath, serialized, value }) => {
|
const set_snapshot = (
|
||||||
if (!store) return; // no store was initialized
|
store,
|
||||||
|
{ snapshotName, snapshotPath, serialized, value }
|
||||||
|
) => {
|
||||||
|
if (!store) return;
|
||||||
|
|
||||||
const message = Cypress._.last(snapshotName);
|
const message = Cypress._.last(snapshotName);
|
||||||
console.log("Current Snapshot name", snapshotName);
|
console.log("Current Snapshot name", snapshotName);
|
||||||
|
|
||||||
const devToolsLog = { $el: serialized };
|
const devToolsLog = { $el: serialized };
|
||||||
// console.log({snapshotName, serialized, value})
|
|
||||||
if (Cypress.dom.isJquery(value)) {
|
if (Cypress.dom.isJquery(value)) {
|
||||||
devToolsLog.$el = value;
|
devToolsLog.$el = value;
|
||||||
}
|
}
|
||||||
|
@ -98,27 +98,33 @@ const set_snapshot = (store, { snapshotName, snapshotPath, serialized, value })
|
||||||
const get_test_name = (test) => test.titlePath;
|
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 = get_test_name(test);
|
||||||
// const key = names.join(" - ");
|
|
||||||
const index = custom_name || get_snapshot_key(key);
|
const index = custom_name;
|
||||||
names.push(String(index));
|
names.push(String(index));
|
||||||
// console.log("names", names)
|
|
||||||
if(custom_name) return [custom_name]
|
if (custom_name) return [custom_name];
|
||||||
return names;
|
return names;
|
||||||
};
|
};
|
||||||
|
|
||||||
module.exports = (value, step, { snapshotName, snapshotPath, json } = {}) => {
|
module.exports = (value, step, options) => {
|
||||||
const name = get_snapshot_name(Cypress.currentTest, snapshotName || step);
|
if (typeof step === "object") options = step;
|
||||||
const serializer = pickSerializer(json, value);
|
if (typeof value !== "object" || Array.isArray(value))
|
||||||
|
value = { data: value };
|
||||||
|
console.log("value", value);
|
||||||
|
|
||||||
|
const name = get_snapshot_name(
|
||||||
|
Cypress.currentTest,
|
||||||
|
options.snapshotName || step
|
||||||
|
);
|
||||||
|
const serializer = pickSerializer(options.json, value);
|
||||||
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,
|
snapshotPath: options.snapshotPath,
|
||||||
serialized,
|
serialized,
|
||||||
value,
|
value,
|
||||||
});
|
});
|
||||||
// return undefined;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue