diff options
author | Leo Kettmeir <crowlkats@toaxl.com> | 2023-01-24 15:41:01 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-01-24 15:41:01 +0100 |
commit | 2027d98a8e9e530277ab301841872cdea7e2d590 (patch) | |
tree | 04d541e094c28d039b7d88cf699cb01e24bf07bd /cli/js | |
parent | fc2e00152b162280e78b06028d51274e33275629 (diff) |
feat: allow first arg in test step to be a function (#17096)
Diffstat (limited to 'cli/js')
-rw-r--r-- | cli/js/40_testing.js | 31 |
1 files changed, 22 insertions, 9 deletions
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<void>} + * @param nameOrFnOrOptions {string | TestStepDefinition | ((t: TestContext) => void | Promise<void>)} + * @param maybeFn {((t: TestContext) => void | Promise<void>) | 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.", |