From 4332034434312beca2f48d3a5aac923d3e83722b Mon Sep 17 00:00:00 2001 From: Adria Navarro Date: Fri, 15 Mar 2024 09:41:32 +0100 Subject: [PATCH] Type tests --- packages/string-templates/src/index.ts | 2 +- packages/string-templates/test/hbsToJs.spec.ts | 2 +- packages/string-templates/test/helpers.spec.ts | 2 +- .../string-templates/test/javascript.spec.ts | 2 +- packages/string-templates/test/manifest.spec.ts | 8 ++++---- packages/string-templates/test/utils.ts | 16 ++++++++-------- 6 files changed, 16 insertions(+), 16 deletions(-) diff --git a/packages/string-templates/src/index.ts b/packages/string-templates/src/index.ts index dd16a0f0ac..f1a6a1b308 100644 --- a/packages/string-templates/src/index.ts +++ b/packages/string-templates/src/index.ts @@ -157,7 +157,7 @@ export function processObjectSync( */ export function processStringSync( string: string, - context: object, + context?: object, opts?: ProcessOptions ): string { // Take a copy of input in case of error diff --git a/packages/string-templates/test/hbsToJs.spec.ts b/packages/string-templates/test/hbsToJs.spec.ts index f8b4c2fca9..ea375064a1 100644 --- a/packages/string-templates/test/hbsToJs.spec.ts +++ b/packages/string-templates/test/hbsToJs.spec.ts @@ -1,6 +1,6 @@ import { convertToJS } from "../src/index" -function checkLines(response, lines) { +function checkLines(response: string, lines: string[]) { const toCheck = response.split("\n") let count = 0 for (let line of lines) { diff --git a/packages/string-templates/test/helpers.spec.ts b/packages/string-templates/test/helpers.spec.ts index f66eb9ef33..5f1855535d 100644 --- a/packages/string-templates/test/helpers.spec.ts +++ b/packages/string-templates/test/helpers.spec.ts @@ -271,7 +271,7 @@ describe("test the string helpers", () => { }) describe("test the comparison helpers", () => { - async function compare(func, a, b) { + async function compare(func: string, a: any, b: any) { const output = await processString( `{{ #${func} a b }}Success{{ else }}Fail{{ /${func} }}`, { diff --git a/packages/string-templates/test/javascript.spec.ts b/packages/string-templates/test/javascript.spec.ts index eb12320cd0..cb2f765007 100644 --- a/packages/string-templates/test/javascript.spec.ts +++ b/packages/string-templates/test/javascript.spec.ts @@ -3,7 +3,7 @@ import vm from "vm" import { processStringSync, encodeJSBinding, setJSRunner } from "../src/index" import { UUID_REGEX } from "./constants" -const processJS = (js, context?): any => { +const processJS = (js: string, context?: object): any => { return processStringSync(encodeJSBinding(js), context) } diff --git a/packages/string-templates/test/manifest.spec.ts b/packages/string-templates/test/manifest.spec.ts index 9fbf4e8019..d8fee0fb1a 100644 --- a/packages/string-templates/test/manifest.spec.ts +++ b/packages/string-templates/test/manifest.spec.ts @@ -24,7 +24,7 @@ import { getParsedManifest, runJsHelpersTests } from "./utils" tk.freeze("2021-01-21T12:00:00") -function escapeRegExp(string) { +function escapeRegExp(string: string) { return string.replace(/[.*+?^${}()|[\]\\]/g, "\\$&") // $& means the whole matched string } @@ -40,9 +40,9 @@ describe("manifest", () => { describe("examples are valid", () => { describe.each(Object.keys(manifest))("%s", collection => { it.each(manifest[collection])("%s", async (_, { hbs, js }) => { - const context = { - double: i => i * 2, - isString: x => typeof x === "string", + const context: any = { + double: (i: number) => i * 2, + isString: (x: any) => typeof x === "string", } const arrays = hbs.match(/\[[^/\]]+\]/) diff --git a/packages/string-templates/test/utils.ts b/packages/string-templates/test/utils.ts index 8c7aa43b21..c683acf5b3 100644 --- a/packages/string-templates/test/utils.ts +++ b/packages/string-templates/test/utils.ts @@ -3,7 +3,7 @@ import { getJsHelperList } from "../src/helpers" import { convertToJS, processStringSync, encodeJSBinding } from "../src/index" -function tryParseJson(str) { +function tryParseJson(str: string) { if (typeof str !== "string") { return } @@ -25,7 +25,7 @@ type ExampleType = [ ] export const getParsedManifest = () => { - const manifest = getManifest() + const manifest: any = getManifest() const collections = Object.keys(manifest) const examples = collections.reduce((acc, collection) => { @@ -73,14 +73,14 @@ export const runJsHelpersTests = ({ funcWrap?: any testsToSkip?: any } = {}) => { - funcWrap = funcWrap || (delegate => delegate()) + funcWrap = funcWrap || ((delegate: () => any) => delegate()) const manifest = getParsedManifest() - const processJS = (js, context) => { + const processJS = (js: string, context: object | undefined) => { return funcWrap(() => processStringSync(encodeJSBinding(js), context)) } - function escapeRegExp(string) { + function escapeRegExp(string: string) { return string.replace(/[.*+?^${}()|[\]\\]/g, "\\$&") // $& means the whole matched string } @@ -98,9 +98,9 @@ export const runJsHelpersTests = ({ examplesToRun.length && it.each(examplesToRun)("%s", async (_, { hbs, js }) => { - const context = { - double: i => i * 2, - isString: x => typeof x === "string", + const context: any = { + double: (i: number) => i * 2, + isString: (x: any) => typeof x === "string", } const arrays = hbs.match(/\[[^/\]]+\]/)