From c6fc74d0e3ebcbb080534f96e4f626b3ddd15d8c Mon Sep 17 00:00:00 2001 From: Julian Kobrynski Date: Wed, 7 Feb 2024 15:01:41 +0100 Subject: [PATCH 1/2] migrate sanitizeStringForJSONParseTest to TypeScript --- ...orJSONParseTest.js => sanitizeStringForJSONParseTest.ts} | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) rename tests/unit/{sanitizeStringForJSONParseTest.js => sanitizeStringForJSONParseTest.ts} (86%) diff --git a/tests/unit/sanitizeStringForJSONParseTest.js b/tests/unit/sanitizeStringForJSONParseTest.ts similarity index 86% rename from tests/unit/sanitizeStringForJSONParseTest.js rename to tests/unit/sanitizeStringForJSONParseTest.ts index a3a3219ed576..1a65040316c9 100644 --- a/tests/unit/sanitizeStringForJSONParseTest.js +++ b/tests/unit/sanitizeStringForJSONParseTest.ts @@ -4,7 +4,7 @@ import sanitizeStringForJSONParse from '../../.github/libs/sanitizeStringForJSON const badInputs = [null, undefined, 42, true]; // Invalid JSON Data should be able to get parsed and the parsed result should match the input text. -const invalidJSONData = [ +const invalidJSONData: string[][] = [ ['Hello \t world!', 'Hello \t world!'], ['Hello \n world!', 'Hello \n world!'], ['Hello \n\tworld!', 'Hello \n\tworld!'], @@ -21,7 +21,7 @@ const invalidJSONData = [ ]; // Valid JSON Data should be able to get parsed and the input text should be unmodified. -const validJSONData = [ +const validJSONData: string[][] = [ ['', ''], ['Hello world!', 'Hello world!'], ['Hello\\\\world!', 'Hello\\\\world!'], @@ -30,6 +30,7 @@ const validJSONData = [ describe('santizeStringForJSONParse', () => { describe.each(badInputs)('willDetectBadInputs', (input) => { test('sanitizeStringForJSONParse', () => { + // @ts-expect-error TODO: Remove this once sanitizeStringForJSONParse (https://github.com/Expensify/App/issues/25360) is migrated to TypeScript. expect(() => sanitizeStringForJSONParse(input)).toThrow(); }); }); @@ -37,6 +38,7 @@ describe('santizeStringForJSONParse', () => { describe.each(invalidJSONData)('canHandleInvalidJSON', (input, expectedOutput) => { test('sanitizeStringForJSONParse', () => { const badJSON = `{"key": "${input}"}`; + // eslint-disable-next-line @typescript-eslint/no-unsafe-return -- it's supposed to throw an error expect(() => JSON.parse(badJSON)).toThrow(); const goodJSON = JSON.parse(`{"key": "${sanitizeStringForJSONParse(input)}"}`); expect(goodJSON.key).toStrictEqual(expectedOutput); From 309335e269522644feb99d1381bc5b089ff397e0 Mon Sep 17 00:00:00 2001 From: Julian Kobrynski Date: Tue, 20 Feb 2024 15:11:33 +0100 Subject: [PATCH 2/2] apply suggested changes --- tests/unit/sanitizeStringForJSONParseTest.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/unit/sanitizeStringForJSONParseTest.ts b/tests/unit/sanitizeStringForJSONParseTest.ts index 1a65040316c9..9d39685eb1f7 100644 --- a/tests/unit/sanitizeStringForJSONParseTest.ts +++ b/tests/unit/sanitizeStringForJSONParseTest.ts @@ -1,10 +1,10 @@ import sanitizeStringForJSONParse from '../../.github/libs/sanitizeStringForJSONParse'; // Bad inputs should cause an error to be thrown -const badInputs = [null, undefined, 42, true]; +const badInputs: Array = [null, undefined, 42, true]; // Invalid JSON Data should be able to get parsed and the parsed result should match the input text. -const invalidJSONData: string[][] = [ +const invalidJSONData: Array<[string, string]> = [ ['Hello \t world!', 'Hello \t world!'], ['Hello \n world!', 'Hello \n world!'], ['Hello \n\tworld!', 'Hello \n\tworld!'], @@ -21,7 +21,7 @@ const invalidJSONData: string[][] = [ ]; // Valid JSON Data should be able to get parsed and the input text should be unmodified. -const validJSONData: string[][] = [ +const validJSONData: Array<[string, string]> = [ ['', ''], ['Hello world!', 'Hello world!'], ['Hello\\\\world!', 'Hello\\\\world!'],