0
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2025-03-03 17:34:47 -05:00
deno/cli/tests/unit/testing_test.ts
Bartek Iwańczuk d8afd56838
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 14:57:51 +01:00

119 lines
2.8 KiB
TypeScript

// Copyright 2018-2021 the Deno authors. All rights reserved. MIT license.
import { assertRejects, assertThrows, unitTest } from "./test_util.ts";
unitTest(function testWrongOverloads() {
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.",
);
});
unitTest(function nameOfTestCaseCantBeEmpty() {
assertThrows(
() => {
Deno.test("", () => {});
},
TypeError,
"The test name can't be empty",
);
assertThrows(
() => {
Deno.test({
name: "",
fn: () => {},
});
},
TypeError,
"The test name can't be empty",
);
});
unitTest(function invalidStepArguments(t) {
assertRejects(
async () => {
// deno-lint-ignore no-explicit-any
await (t as any).step("test");
},
TypeError,
"Expected function for second argument.",
);
assertRejects(
async () => {
// deno-lint-ignore no-explicit-any
await (t as any).step("test", "not a function");
},
TypeError,
"Expected function for second argument.",
);
assertRejects(
async () => {
// deno-lint-ignore no-explicit-any
await (t as any).step();
},
TypeError,
"Expected a test definition or name and function.",
);
assertRejects(
async () => {
// deno-lint-ignore no-explicit-any
await (t as any).step(() => {});
},
TypeError,
"Expected a test definition or name and function.",
);
});