2024-01-01 14:58:21 -05:00
|
|
|
// Copyright 2018-2024 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,
|
2024-09-04 03:19:55 -04:00
|
|
|
"Unexpected 'fn' field in options, test function is already provided as the third argument",
|
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", { name: "some name2" }, () => {});
|
|
|
|
},
|
|
|
|
TypeError,
|
2024-09-04 03:19:55 -04:00
|
|
|
"Unexpected 'name' field in options, test name is already provided as the first argument",
|
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(() => {});
|
|
|
|
},
|
|
|
|
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,
|
2024-09-04 03:19:55 -04:00
|
|
|
"Unexpected 'fn' field in options, test function is already provided as the second argument",
|
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({});
|
|
|
|
},
|
|
|
|
TypeError,
|
2024-09-04 03:19:55 -04:00
|
|
|
"Expected 'fn' field in the first argument to be a test function",
|
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({ fn: "boo!" });
|
|
|
|
},
|
|
|
|
TypeError,
|
2024-09-04 03:19:55 -04:00
|
|
|
"Expected 'fn' field in the first argument to be a test function",
|
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
|
|
|
);
|
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,
|
2024-09-04 03:19:55 -04:00
|
|
|
"Expected function for second argument",
|
2021-10-11 09:45:02 -04:00
|
|
|
);
|
|
|
|
|
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,
|
2024-09-04 03:19:55 -04:00
|
|
|
"Expected function for second argument",
|
2021-10-11 09:45:02 -04:00
|
|
|
);
|
|
|
|
|
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,
|
2024-09-04 03:19:55 -04:00
|
|
|
"Expected a test definition or name and function",
|
2021-10-11 09:45:02 -04:00
|
|
|
);
|
|
|
|
|
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,
|
2024-09-04 03:19:55 -04: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);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2023-04-21 09:32:37 -04:00
|
|
|
|
|
|
|
Deno.test("explicit undefined for boolean options", {
|
|
|
|
ignore: undefined,
|
|
|
|
only: undefined,
|
|
|
|
}, () => {});
|