From 2027d98a8e9e530277ab301841872cdea7e2d590 Mon Sep 17 00:00:00 2001 From: Leo Kettmeir Date: Tue, 24 Jan 2023 15:41:01 +0100 Subject: feat: allow first arg in test step to be a function (#17096) --- cli/js/40_testing.js | 31 ++++++++++++++++++++++--------- 1 file changed, 22 insertions(+), 9 deletions(-) (limited to 'cli/js/40_testing.js') diff --git a/cli/js/40_testing.js b/cli/js/40_testing.js index d71609dd1..0693fba1a 100644 --- a/cli/js/40_testing.js +++ b/cli/js/40_testing.js @@ -1265,10 +1265,10 @@ */ origin: desc.origin, /** - * @param nameOrTestDefinition {string | TestStepDefinition} - * @param fn {(t: TestContext) => void | Promise} + * @param nameOrFnOrOptions {string | TestStepDefinition | ((t: TestContext) => void | Promise)} + * @param maybeFn {((t: TestContext) => void | Promise) | undefined} */ - async step(nameOrTestDefinition, fn) { + async step(nameOrFnOrOptions, maybeFn) { if (MapPrototypeGet(testStates, desc.id).finalized) { throw new Error( "Cannot run test step after parent scope has finished execution. " + @@ -1277,16 +1277,29 @@ } let stepDesc; - if (typeof nameOrTestDefinition === "string") { - if (!(ObjectPrototypeIsPrototypeOf(FunctionPrototype, fn))) { + if (typeof nameOrFnOrOptions === "string") { + if (!(ObjectPrototypeIsPrototypeOf(FunctionPrototype, maybeFn))) { throw new TypeError("Expected function for second argument."); } stepDesc = { - name: nameOrTestDefinition, - fn, + name: nameOrFnOrOptions, + fn: maybeFn, }; - } else if (typeof nameOrTestDefinition === "object") { - stepDesc = nameOrTestDefinition; + } else if (typeof nameOrFnOrOptions === "function") { + if (!nameOrFnOrOptions.name) { + throw new TypeError("The step function must have a name."); + } + if (maybeFn != undefined) { + throw new TypeError( + "Unexpected second argument to TestContext.step()", + ); + } + stepDesc = { + name: nameOrFnOrOptions.name, + fn: nameOrFnOrOptions, + }; + } else if (typeof nameOrFnOrOptions === "object") { + stepDesc = nameOrFnOrOptions; } else { throw new TypeError( "Expected a test definition or name and function.", -- cgit v1.2.3