From 52fca8714d88123121d794700efa586282a42dac Mon Sep 17 00:00:00 2001 From: Adria Navarro Date: Tue, 23 Jan 2024 19:40:31 +0100 Subject: [PATCH] More verbose test code --- .../string-templates/test/manifest.spec.js | 21 +++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) diff --git a/packages/string-templates/test/manifest.spec.js b/packages/string-templates/test/manifest.spec.js index ccfb14b4e0..506f2eb6f7 100644 --- a/packages/string-templates/test/manifest.spec.js +++ b/packages/string-templates/test/manifest.spec.js @@ -40,6 +40,18 @@ function escapeRegExp(string) { return string.replace(/[.*+?^${}()|[\]\\]/g, "\\$&") // $& means the whole matched string } +function tryParseJson(str) { + if (typeof str !== "string") { + return + } + + try { + return JSON.parse(str.replace(/\'/g, '"')) + } catch (e) { + return + } +} + describe("manifest", () => { describe("examples are valid", () => { describe.each(Object.keys(examples))("%s", collection => { @@ -67,18 +79,15 @@ describe("manifest", () => { let result = await processString(hbs, context) // Trim 's js = js.replace(/^\'|\'$/g, "") - try { - let parsedExpected - if ( - Array.isArray((parsedExpected = JSON.parse(js.replace(/\'/g, '"')))) - ) { + if ((parsedExpected = tryParseJson(js))) { + if (Array.isArray(parsedExpected)) { if (typeof parsedExpected[0] === "object") { js = JSON.stringify(parsedExpected) } else { js = parsedExpected.join(",") } } - } catch {} + } result = result.replace(/ /g, " ") expect(result).toEqual(js) })