fix(testing): cause type error for async function as describe body (#5355)

This commit is contained in:
Yoshiya Hinosawa 2024-07-09 12:13:07 +09:00 committed by GitHub
parent 0d668fa01e
commit 6eeddb32d7
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 46 additions and 11 deletions

View File

@ -2,7 +2,7 @@
/** The options for creating a test suite with the describe function. */ /** The options for creating a test suite with the describe function. */
export interface DescribeDefinition<T> extends Omit<Deno.TestDefinition, "fn"> { export interface DescribeDefinition<T> extends Omit<Deno.TestDefinition, "fn"> {
/** The body of the test suite */ /** The body of the test suite */
fn?: () => void; fn?: () => void | undefined;
/** /**
* The `describe` function returns a `TestSuite` representing the group of tests. * The `describe` function returns a `TestSuite` representing the group of tests.
* If `describe` is called within another `describe` calls `fn`, the suite will default to that parent `describe` calls returned `TestSuite`. * If `describe` is called within another `describe` calls `fn`, the suite will default to that parent `describe` calls returned `TestSuite`.

View File

@ -915,20 +915,20 @@ export type DescribeArgs<T> =
name: string, name: string,
options: Omit<DescribeDefinition<T>, "name">, options: Omit<DescribeDefinition<T>, "name">,
] ]
| [name: string, fn: () => void] | [name: string, fn: () => void | undefined]
| [fn: () => void] | [fn: () => void | undefined]
| [ | [
name: string, name: string,
options: Omit<DescribeDefinition<T>, "fn" | "name">, options: Omit<DescribeDefinition<T>, "fn" | "name">,
fn: () => void, fn: () => void | undefined,
] ]
| [ | [
options: Omit<DescribeDefinition<T>, "fn">, options: Omit<DescribeDefinition<T>, "fn">,
fn: () => void, fn: () => void | undefined,
] ]
| [ | [
options: Omit<DescribeDefinition<T>, "fn" | "name">, options: Omit<DescribeDefinition<T>, "fn" | "name">,
fn: () => void, fn: () => void | undefined,
] ]
| [ | [
suite: TestSuite<T>, suite: TestSuite<T>,
@ -942,27 +942,27 @@ export type DescribeArgs<T> =
| [ | [
suite: TestSuite<T>, suite: TestSuite<T>,
name: string, name: string,
fn: () => void, fn: () => void | undefined,
] ]
| [ | [
suite: TestSuite<T>, suite: TestSuite<T>,
fn: () => void, fn: () => void | undefined,
] ]
| [ | [
suite: TestSuite<T>, suite: TestSuite<T>,
name: string, name: string,
options: Omit<DescribeDefinition<T>, "fn" | "name" | "suite">, options: Omit<DescribeDefinition<T>, "fn" | "name" | "suite">,
fn: () => void, fn: () => void | undefined,
] ]
| [ | [
suite: TestSuite<T>, suite: TestSuite<T>,
options: Omit<DescribeDefinition<T>, "fn" | "suite">, options: Omit<DescribeDefinition<T>, "fn" | "suite">,
fn: () => void, fn: () => void | undefined,
] ]
| [ | [
suite: TestSuite<T>, suite: TestSuite<T>,
options: Omit<DescribeDefinition<T>, "fn" | "name" | "suite">, options: Omit<DescribeDefinition<T>, "fn" | "name" | "suite">,
fn: () => void, fn: () => void | undefined,
]; ];
/** Generates a DescribeDefinition from DescribeArgs. */ /** Generates a DescribeDefinition from DescribeArgs. */

View File

@ -2008,4 +2008,39 @@ Deno.test("describe()", async (t) => {
} }
}, },
); );
await t.step(
"cause type error if async function is passed as describe definition",
() => {
// @ts-expect-error async function is not assignable to describe argument
describe({ name: "example", fn: async () => {} });
// @ts-expect-error async function is not assignable to describe argument
describe("example", { fn: async () => {} });
// @ts-expect-error async function is not assignable to describe argument
describe("example", async () => {});
// TODO(kt3k): This case should be type error but it's checked as
// DescribeDefinition<T> and passes the type check
// describe(async function example() {});
// @ts-expect-error async function is not assignable to describe argument
describe("example", {}, async () => {});
// @ts-expect-error async function is not assignable to describe argument
describe({ name: "example" }, async () => {});
// @ts-expect-error async function is not assignable to describe argument
describe({}, async function example() {});
const suite = describe("example");
// @ts-expect-error async function is not assignable to describe argument
describe(suite, "example", { fn: async () => {} });
// @ts-expect-error async function is not assignable to describe argument
describe(suite, "example", async () => {});
// @ts-expect-error async function is not assignable to describe argument
describe(suite, async () => {});
// @ts-expect-error async function is not assignable to describe argument
describe(suite, "example", {}, async () => {});
// @ts-expect-error async function is not assignable to describe argument
describe(suite, { name: "example" }, async () => {});
// @ts-expect-error async function is not assignable to describe argument
describe(suite, {}, async function example() {});
},
);
}); });