Refactor tests

This commit is contained in:
Adria Navarro 2024-01-22 22:06:40 +01:00
parent b66fac87cc
commit 603943d7cf
1 changed files with 45 additions and 29 deletions

View File

@ -17,8 +17,15 @@ const manifest = JSON.parse(
fs.readFileSync(require.resolve("../manifest.json"), "utf8")
)
const examples = Object.keys(manifest).flatMap(collection =>
Object.keys(manifest[collection]).map(fnc => [collection, fnc])
const functionsToExclude = { string: ["raw"] }
const collections = Object.keys(manifest)
const examples = collections.reduce(
(acc, collection) => ({
...acc,
[collection]: manifest[collection],
}),
{}
)
function escapeRegExp(string) {
@ -27,7 +34,12 @@ function escapeRegExp(string) {
describe("manifest", () => {
describe("examples are valid", () => {
it.each(examples)("%s - %s", async (collection, func) => {
describe.each(collections)("%s", collection => {
it.each(
Object.keys(examples[collection]).filter(
fnc => !functionsToExclude[collection]?.includes(fnc)
)
)("%s", async func => {
const example = manifest[collection][func].example
let [hbs, js] = example.split("->").map(x => x.trim())
@ -37,7 +49,7 @@ describe("manifest", () => {
}
const arrays = hbs.match(/\[[^/\]]+\]/)
arrays.forEach((arrayString, i) => {
arrays?.forEach((arrayString, i) => {
hbs = hbs.replace(new RegExp(escapeRegExp(arrayString)), `array${i}`)
context[`array${i}`] = JSON.parse(arrayString.replace(/\'/g, '"'))
})
@ -47,7 +59,9 @@ describe("manifest", () => {
return
}
const result = await processString(hbs, context)
let result = await processString(hbs, context)
// Trim 's
js = js.replace(/^\'|\'$/g, "")
try {
let parsedExpected
if (
@ -56,7 +70,9 @@ describe("manifest", () => {
js = parsedExpected.join(",")
}
} catch {}
result = result.replace(/ /g, " ")
expect(result).toEqual(js)
})
})
})
})