mass changes

This commit is contained in:
Joshua 2024-01-29 19:08:06 +01:00
parent b61e5c0552
commit 5db74ebcb6
23 changed files with 356 additions and 358 deletions

2
.vscode/settings.json vendored Normal file
View file

@ -0,0 +1,2 @@
{
}

View file

@ -1,14 +1,15 @@
const { defineConfig } = require("cypress");
const { functions } = require("./src/utils");
module.exports = defineConfig({
snapshot: {
// snapshotPath: "cypress/snapshots/",
SNAPSHOT_UPDATE: true
// SNAPSHOT_UPDATE: true,
useFolders: true,
},
e2e: {
setupNodeEvents(on, config) {
functions.tasks(on, config);
// implement node event listeners here
},
},
});

View file

@ -1,75 +1,78 @@
/* eslint-env mocha */
/* global cy */
describe("@datashard/snapshot", () => {
context("simple types", () => {
it("works with objects", () => {
cy.wrap({
"foo": "bar",
"Fizzy Drink": "Pop"
}).snapshot(
// "Filename", {
// snapshotPath: "asdasd"
// }
describe("datashard/snapshot", () => {
context("simple types"
// , { env: { SNAPSHOT_UPDATE: true } }
, () => {
it("works with objects", () => {
cy.wrap({
"foo": "bar",
"Fizzy Drink": "Pop"
}).snapshot();
});
it("works with numbers", () => {
cy.wrap(42).snapshot({
snapshotPath: "cypress/fixtures/snapshots",
snapshotName: "Numbers",
});
});
it("works with strings", () => {
cy.wrap("foo-bar").snapshot({
snapshotPath: "cypress/fixtures/snapshots",
snapshotName: "Strings",
});
});
it(
"works with arrays",
{
env: {
SNAPSHOT_UPDATE: true,
},
},
() => {
cy.wrap([1, 2, 3, 4]).snapshot({
snapshotPath: "cypress/fixtures/snapshots",
snapshotName: "Arrays",
});
}
);
// cypress/fixtures/@datashard-snapshot/simple-types/works-with-objects/asdjskadhasj.json
});
// it("works with numbers", () => {
// cy.wrap(42).snapshot({
// snapshotPath: "cypress/fixtures/snapshots",
// snapshotName: "Numbers",
// });
// });
// it("works with strings", () => {
// cy.wrap("foo-bar").snapshot({
// snapshotPath: "cypress/fixtures/snapshots",
// snapshotName: "Strings",
// });
// });
// it(
// "works with arrays",
// {
// env: {
// SNAPSHOT_UPDATE: true,
// },
// },
// () => {
// cy.wrap([1, 2, 3, 4]).snapshot({
// snapshotPath: "cypress/fixtures/snapshots",
// snapshotName: "Arrays",
// });
// }
// );
// it('works with more "complicated" Objects', () => {
// cy.fixture("Complex").snapshot({
// snapshotPath: 'cypress/fixtures/snapshots',
// snapshotName: "Complex"
// })
// })
// it.only("works based on fixtures", () => {
// cy
// .wrap({
// "jsonapi": {
// "version": "2.0"
// },
// "included": [
// {
// "type": "users",
// "id": "2",
// "attributes": {
// "name": "Test"
// }
// }
// ]
// })
// .snapshot({
// snapshotFixture: "generated",
// // snapshotPath: "cypress/fixtures/snapshots",
// // snapshotName: "generated",
// });
// });
});
context("complex types"
// , { env: { SNAPSHOT_UPDATE: true } }
, () => {
it('works with more "complicated" Objects', () => {
cy.wrap({
"status": 200,
"response": {
"array": [0, 1, 2, "Three"],
"object": {
"with": "more details"
}
}
}
).snapshot()
})
it("works based on fixtures", () => {
cy
.wrap({
"jsonapi": {
"version": "2.0"
},
"included": [
{
"type": "users",
"id": "2",
"attributes": {
"name": "Test"
}
}
]
})
.snapshot();
});
})
});

View file

@ -1,4 +0,0 @@
{
"foo": "bar",
"Fizzy Drink": "Soda"
}

View file

@ -1,4 +0,0 @@
{
"foo": "bar",
"Fizzy Drink": "Pop"
}

View file

@ -0,0 +1,14 @@
{
"jsonapi": {
"version": "2.0"
},
"included": [
{
"type": "users",
"id": "2",
"attributes": {
"name": "Test"
}
}
]
}

View file

@ -0,0 +1,14 @@
{
"jsonapi": {
"version": "2.0"
},
"included": [
{
"type": "users",
"id": "2",
"attributes": {
"name": "Test"
}
}
]
}

View file

@ -1,7 +1,12 @@
{
"status": 200,
"response": {
"array": [0, 1, 2, "Three"],
"array": [
0,
1,
2,
"Three"
],
"object": {
"with": "more details"
}

View file

@ -1 +0,0 @@
{"foo":"bar","Fizzy Drink":"Soda"}

View file

@ -1,4 +0,0 @@
{
"foo": "bar",
"Fizzy Drink": "Soda"
}

View file

@ -1,2 +1,2 @@
// register .snapshot() command
require('../..').register()
require('../../src/index').register()

View file

@ -1,7 +1,6 @@
// global cy, Cypress
const { functions } = require("./utils/index");
const register = require("./register");
module.exports = {
register: functions.register,
tasks: functions.tasks,
register
};

View file

@ -1,6 +1,6 @@
const lazy = require("lazy-ass");
const is = require("check-more-types");
const snapshot = require("../snapshots/snapshot");
const snapshot = require("./snapshot");
module.exports = () => {
lazy(is.fn(global.before), "Missing global before function");

View file

@ -1,6 +1,6 @@
const serializeDomElement = require("../serializers/serializeDomElement");
const serializeToHTML = require("../serializers/serializeToHTML");
const compareValues = require("./compareValues");
const serializeDomElement = require("./utils/serializers/serializeDomElement");
const serializeToHTML = require("./utils/serializers/serializeToHTML");
const compareValues = require("./utils/compareValues");
const path = require("path");
const identity = (x) => x;
@ -12,24 +12,15 @@ const pickSerializer = (asJson, value) => {
};
const store_snapshot = (props = { value, name, raiser }) => {
if (!Cypress.env().SNAPSHOT_UPDATE) {
cy.fixture(props.name).then(content => props.raiser({ value: props.value, expected: content }))
if (Cypress.env().SNAPSHOT_UPDATE || Cypress.config('snapshot').SNAPSHOT_UPDATE) {
cy.log(props.name)
console.log(props.name)
cy.writeFile(`${props.name}.json`, JSON.stringify(props.value, null, 2))
} else {
cy.writeFile(`${props.name}.json`, JSON.stringify(props.value.null, 2))
// TODO: Figure out how to replace the fixture folder name if people move it
const fixtureName = props.name.replace("cypress/fixtures", "")
cy.fixture(fixtureName).then(content => props.raiser({ value: props.value, expected: content }))
}
// cy.fixture(props.name)
// .then(exist => {
// cy.log('fixture 2')
// if (exist && !Cypress.env().SNAPSHOT_UPDATE) {
// cy.log(`fixture exists and doesn't update`)
// props.raiser({ value: props.value, expected: exist, type: "fixture" });
// } else {
// cy.log(`fixture exists and updates`)
// cy.writeFile(expectedPath, JSON.stringify(props.value, null, 2));
// }
// })
};
const set_snapshot = ({ snapshotName, serialized, value }) => {
@ -73,23 +64,47 @@ const set_snapshot = ({ snapshotName, serialized, value }) => {
});
};
function replaceCharacters(str, asFolder, sep) {
if (asFolder) {
if (!sep) throw new Error("Separator not Passed.")
return str
.replace(/ /gi, '-')
.replace(/\//gi, "-")
.replaceAll('"', '')
.replaceAll(sep, '/')
} else {
return str
.replaceAll(' ', '-')
.replaceAll('/', '-')
.replaceAll('"', '')
}
}
const get_snapshot_name = (asFolder, stepName) => {
const names = Cypress.currentTest.titlePath;
const sep = ">>datashard.work<<"
if (stepName) names.push(stepName)
if (asFolder) return names.join(sep).replace(/ /gi, "-").replace(/\//gi, "-").replaceAll(sep, '/')
else return names.join('__').replaceAll(" ", "-").replaceAll("/", "-")
if (stepName && typeof stepName !== 'object') {
names.push(stepName)
}
if (asFolder) return replaceCharacters(names.join(sep), true, sep)
else return replaceCharacters(names.join('__'), false)
};
module.exports = (value, stepName, options = { json: true, asFolder: false }) => {
module.exports = (value, stepName, options = { json: true }) => {
if (typeof stepName === 'object') options = { ...options, ...stepName }
if (typeof value !== "object" || Array.isArray(value))
value = { data: value };
const serializer = pickSerializer(options.json, value);
const serialized = serializer(value);
options.asFolder = Cypress.config('snapshot').useFolders || false
set_snapshot({
snapshotName: path.join(
options.snapshotPath || Cypress.config('snapshot').snapshotPath || 'snapshots',
options.snapshotPath || Cypress.config('snapshot').snapshotPath || 'cypress/fixtures/snapshots',
`/${get_snapshot_name(options.asFolder, stepName)}`),
serialized,
value,

View file

@ -1,5 +0,0 @@
const readFileMaybe = require("../tasks/readFileMaybe");
module.exports = (on, config) => {
on("task", { readFileMaybe });
};

View file

@ -1,28 +0,0 @@
const serializeToHTML = require("./serializers/serializeToHTML");
const serializeDomElement = require("./serializers/serializeDomElement");
const compareValues = require("./snapshots/compareValues");
const readFileMaybe = require("./tasks/readFileMaybe");
const identity = (x) => x;
const publicProps = (name) => !name.startsWith("__");
const countSnapshots = (snapshots) =>
Object.keys(snapshots).filter(publicProps).length;
module.exports = {
serializers: {
serializeDomElement,
serializeToHTML,
identity,
countSnapshots,
},
snapshots: {
compareValues,
},
functions: {
register: require("./functions/register"),
tasks: require("./functions/addTasks"),
},
tasks: {
readFileMaybe,
},
};

View file

@ -1,9 +0,0 @@
const fs = require("fs");
module.exports = (filename) => {
if (fs.existsSync(filename)) {
return fs.readFileSync(filename, "utf8");
}
return false;
};