From d8afd5683857de83f3cc80c33322df3d65377210 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bartek=20Iwa=C5=84czuk?= Date: Tue, 23 Nov 2021 14:57:51 +0100 Subject: 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; // Deno.test("test name", { only: true }, function() { }); export function test( name: string, options: Omit, fn: (t: TestContext) => void | Promise, ): void; // Deno.test({ name: "test name" }, function() { }); export function test( options: Omit, fn: (t: TestContext) => void | Promise, ): void; // Deno.test({ only: true }, function testName() { }); export function test( options: Omit, fn: (t: TestContext) => void | Promise, ): void; ``` --- runtime/js/40_testing.js | 77 ++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 65 insertions(+), 12 deletions(-) (limited to 'runtime/js/40_testing.js') 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); -- cgit v1.2.3