diff options
author | Bartek IwaĆczuk <biwanczuk@gmail.com> | 2021-11-23 14:57:51 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-11-23 14:57:51 +0100 |
commit | d8afd5683857de83f3cc80c33322df3d65377210 (patch) | |
tree | 0ffb7f1e94994282aadc6f6c342a1884c19774ae /runtime/js/40_testing.js | |
parent | ae34f8fa10f4daddde3d32cc63773d288253d4d4 (diff) |
feat(test): Add more overloads for "Deno.test" (#12749)
This commit adds 4 more overloads to "Deno.test()" API.
```
// Deno.test(function testName() { });
export function test(fn: (t: TestContext) => void | Promise<void>): void;
// Deno.test("test name", { only: true }, function() { });
export function test(
name: string,
options: Omit<TestDefinition, "name">,
fn: (t: TestContext) => void | Promise<void>,
): void;
// Deno.test({ name: "test name" }, function() { });
export function test(
options: Omit<TestDefinition, "fn">,
fn: (t: TestContext) => void | Promise<void>,
): void;
// Deno.test({ only: true }, function testName() { });
export function test(
options: Omit<TestDefinition, "fn" | "name">,
fn: (t: TestContext) => void | Promise<void>,
): void;
```
Diffstat (limited to 'runtime/js/40_testing.js')
-rw-r--r-- | runtime/js/40_testing.js | 77 |
1 files changed, 65 insertions, 12 deletions
diff --git a/runtime/js/40_testing.js b/runtime/js/40_testing.js index e07d54a1a..053afc5da 100644 --- a/runtime/js/40_testing.js +++ b/runtime/js/40_testing.js @@ -254,8 +254,9 @@ finishing test case.`; // Main test function provided by Deno. function test( - t, - fn, + nameOrFnOrOptions, + optionsOrFn, + maybeFn, ) { let testDef; const defaults = { @@ -267,22 +268,74 @@ finishing test case.`; permissions: null, }; - if (typeof t === "string") { - if (!fn || typeof fn != "function") { - throw new TypeError("Missing test function"); - } - if (!t) { + if (typeof nameOrFnOrOptions === "string") { + if (!nameOrFnOrOptions) { throw new TypeError("The test name can't be empty"); } - testDef = { fn: fn, name: t, ...defaults }; + if (typeof optionsOrFn === "function") { + testDef = { fn: optionsOrFn, name: nameOrFnOrOptions, ...defaults }; + } else { + if (!maybeFn || typeof maybeFn !== "function") { + throw new TypeError("Missing test function"); + } + if (optionsOrFn.fn != undefined) { + throw new TypeError( + "Unexpected 'fn' field in options, test function is already provided as the third argument.", + ); + } + if (optionsOrFn.name != undefined) { + throw new TypeError( + "Unexpected 'name' field in options, test name is already provided as the first argument.", + ); + } + testDef = { + ...defaults, + ...optionsOrFn, + fn: maybeFn, + name: nameOrFnOrOptions, + }; + } + } else if (typeof nameOrFnOrOptions === "function") { + if (!nameOrFnOrOptions.name) { + throw new TypeError("The test function must have a name"); + } + if (optionsOrFn != undefined) { + throw new TypeError("Unexpected second argument to Deno.test()"); + } + if (maybeFn != undefined) { + throw new TypeError("Unexpected third argument to Deno.test()"); + } + testDef = { + ...defaults, + fn: nameOrFnOrOptions, + name: nameOrFnOrOptions.name, + }; } else { - if (!t.fn) { - throw new TypeError("Missing test function"); + let fn; + let name; + if (typeof optionsOrFn === "function") { + fn = optionsOrFn; + if (nameOrFnOrOptions.fn != undefined) { + throw new TypeError( + "Unexpected 'fn' field in options, test function is already provided as the second argument.", + ); + } + name = nameOrFnOrOptions.name ?? fn.name; + } else { + if ( + !nameOrFnOrOptions.fn || typeof nameOrFnOrOptions.fn !== "function" + ) { + throw new TypeError( + "Expected 'fn' field in the first argument to be a test function.", + ); + } + fn = nameOrFnOrOptions.fn; + name = nameOrFnOrOptions.name ?? fn.name; } - if (!t.name) { + if (!name) { throw new TypeError("The test name can't be empty"); } - testDef = { ...defaults, ...t }; + testDef = { ...defaults, ...nameOrFnOrOptions, fn, name }; } testDef.fn = wrapTestFnWithSanitizers(testDef.fn, testDef); |