2023-01-02 16:00:42 -05:00
|
|
|
// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license.
|
2022-04-06 10:51:38 -04:00
|
|
|
import { assertEquals, assertRejects, assertThrows } from "./test_util.ts";
|
2020-03-05 05:52:18 -05:00
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
Deno.test(function testWrongOverloads() {
|
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;
```
2021-11-23 08:57:51 -05:00
|
|
|
assertThrows(
|
|
|
|
() => {
|
|
|
|
// @ts-ignore Testing invalid overloads
|
|
|
|
Deno.test("some name", { fn: () => {} }, () => {});
|
|
|
|
},
|
|
|
|
TypeError,
|
|
|
|
"Unexpected 'fn' field in options, test function is already provided as the third argument.",
|
|
|
|
);
|
|
|
|
assertThrows(
|
|
|
|
() => {
|
|
|
|
// @ts-ignore Testing invalid overloads
|
|
|
|
Deno.test("some name", { name: "some name2" }, () => {});
|
|
|
|
},
|
|
|
|
TypeError,
|
|
|
|
"Unexpected 'name' field in options, test name is already provided as the first argument.",
|
|
|
|
);
|
|
|
|
assertThrows(
|
|
|
|
() => {
|
|
|
|
// @ts-ignore Testing invalid overloads
|
|
|
|
Deno.test(() => {});
|
|
|
|
},
|
|
|
|
TypeError,
|
|
|
|
"The test function must have a name",
|
|
|
|
);
|
|
|
|
assertThrows(
|
|
|
|
() => {
|
|
|
|
// @ts-ignore Testing invalid overloads
|
|
|
|
Deno.test(function foo() {}, {});
|
|
|
|
},
|
|
|
|
TypeError,
|
|
|
|
"Unexpected second argument to Deno.test()",
|
|
|
|
);
|
|
|
|
assertThrows(
|
|
|
|
() => {
|
|
|
|
// @ts-ignore Testing invalid overloads
|
|
|
|
Deno.test({ fn: () => {} }, function foo() {});
|
|
|
|
},
|
|
|
|
TypeError,
|
|
|
|
"Unexpected 'fn' field in options, test function is already provided as the second argument.",
|
|
|
|
);
|
|
|
|
assertThrows(
|
|
|
|
() => {
|
|
|
|
// @ts-ignore Testing invalid overloads
|
|
|
|
Deno.test({});
|
|
|
|
},
|
|
|
|
TypeError,
|
|
|
|
"Expected 'fn' field in the first argument to be a test function.",
|
|
|
|
);
|
|
|
|
assertThrows(
|
|
|
|
() => {
|
|
|
|
// @ts-ignore Testing invalid overloads
|
|
|
|
Deno.test({ fn: "boo!" });
|
|
|
|
},
|
|
|
|
TypeError,
|
|
|
|
"Expected 'fn' field in the first argument to be a test function.",
|
|
|
|
);
|
2020-03-05 05:52:18 -05:00
|
|
|
});
|
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
Deno.test(function nameOfTestCaseCantBeEmpty() {
|
2020-03-05 05:52:18 -05:00
|
|
|
assertThrows(
|
|
|
|
() => {
|
|
|
|
Deno.test("", () => {});
|
|
|
|
},
|
2020-03-15 05:34:24 -04:00
|
|
|
TypeError,
|
2020-07-14 15:24:17 -04:00
|
|
|
"The test name can't be empty",
|
2020-03-05 05:52:18 -05:00
|
|
|
);
|
|
|
|
assertThrows(
|
|
|
|
() => {
|
|
|
|
Deno.test({
|
|
|
|
name: "",
|
2020-03-28 13:03:49 -04:00
|
|
|
fn: () => {},
|
2020-03-05 05:52:18 -05:00
|
|
|
});
|
|
|
|
},
|
2020-03-15 05:34:24 -04:00
|
|
|
TypeError,
|
2020-07-14 15:24:17 -04:00
|
|
|
"The test name can't be empty",
|
2020-03-05 05:52:18 -05:00
|
|
|
);
|
|
|
|
});
|
2021-10-11 09:45:02 -04:00
|
|
|
|
2021-11-24 20:23:03 -05:00
|
|
|
Deno.test(async function invalidStepArguments(t) {
|
|
|
|
await assertRejects(
|
2021-10-11 09:45:02 -04:00
|
|
|
async () => {
|
|
|
|
// deno-lint-ignore no-explicit-any
|
|
|
|
await (t as any).step("test");
|
|
|
|
},
|
|
|
|
TypeError,
|
|
|
|
"Expected function for second argument.",
|
|
|
|
);
|
|
|
|
|
2021-11-24 20:23:03 -05:00
|
|
|
await assertRejects(
|
2021-10-11 09:45:02 -04:00
|
|
|
async () => {
|
|
|
|
// deno-lint-ignore no-explicit-any
|
|
|
|
await (t as any).step("test", "not a function");
|
|
|
|
},
|
|
|
|
TypeError,
|
|
|
|
"Expected function for second argument.",
|
|
|
|
);
|
|
|
|
|
2021-11-24 20:23:03 -05:00
|
|
|
await assertRejects(
|
2021-10-11 09:45:02 -04:00
|
|
|
async () => {
|
|
|
|
// deno-lint-ignore no-explicit-any
|
|
|
|
await (t as any).step();
|
|
|
|
},
|
|
|
|
TypeError,
|
|
|
|
"Expected a test definition or name and function.",
|
|
|
|
);
|
|
|
|
|
2021-11-24 20:23:03 -05:00
|
|
|
await assertRejects(
|
2021-10-11 09:45:02 -04:00
|
|
|
async () => {
|
|
|
|
// deno-lint-ignore no-explicit-any
|
|
|
|
await (t as any).step(() => {});
|
|
|
|
},
|
|
|
|
TypeError,
|
2023-01-24 09:41:01 -05:00
|
|
|
"The step function must have a name.",
|
2021-10-11 09:45:02 -04:00
|
|
|
);
|
|
|
|
});
|
2022-04-06 10:51:38 -04:00
|
|
|
|
|
|
|
Deno.test(async function nameOnTextContext(t1) {
|
|
|
|
await assertEquals(t1.name, "nameOnTextContext");
|
|
|
|
await t1.step("step", async (t2) => {
|
|
|
|
await assertEquals(t2.name, "step");
|
|
|
|
await t2.step("nested step", async (t3) => {
|
|
|
|
await assertEquals(t3.name, "nested step");
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
Deno.test(async function originOnTextContext(t1) {
|
|
|
|
await assertEquals(t1.origin, Deno.mainModule);
|
|
|
|
await t1.step("step", async (t2) => {
|
|
|
|
await assertEquals(t2.origin, Deno.mainModule);
|
|
|
|
await t2.step("nested step", async (t3) => {
|
|
|
|
await assertEquals(t3.origin, Deno.mainModule);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
Deno.test(async function parentOnTextContext(t1) {
|
|
|
|
await assertEquals(t1.parent, undefined);
|
|
|
|
await t1.step("step", async (t2) => {
|
|
|
|
await assertEquals(t1, t2.parent);
|
|
|
|
await t2.step("nested step", async (t3) => {
|
|
|
|
await assertEquals(t2, t3.parent);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|