Skip to content

Commit faa633b

Browse files
authored
fix(testing): throw error when async func is passed to describe (denoland#5385)
1 parent 8fa3522 commit faa633b

File tree

2 files changed

+52
-30
lines changed

2 files changed

+52
-30
lines changed

testing/_test_suite.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,13 @@ export class TestSuiteInternal<T> implements TestSuite<T> {
9797
const temp = TestSuiteInternal.current;
9898
TestSuiteInternal.current = this;
9999
try {
100-
fn();
100+
// deno-lint-ignore no-explicit-any
101+
const value = fn() as any;
102+
if (value instanceof Promise) {
103+
throw new Error(
104+
'Returning a Promise from "describe" is not supported. Tests must be defined synchronously.',
105+
);
106+
}
101107
} finally {
102108
TestSuiteInternal.current = temp;
103109
}

testing/bdd_test.ts

Lines changed: 45 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -2012,35 +2012,51 @@ Deno.test("describe()", async (t) => {
20122012
await t.step(
20132013
"cause type error if async function is passed as describe definition",
20142014
() => {
2015-
// @ts-expect-error async function is not assignable to describe argument
2016-
describe({ name: "example", fn: async () => {} });
2017-
// @ts-expect-error async function is not assignable to describe argument
2018-
describe("example", { fn: async () => {} });
2019-
// @ts-expect-error async function is not assignable to describe argument
2020-
describe("example", async () => {});
2021-
// TODO(kt3k): This case should be type error but it's checked as
2022-
// DescribeDefinition<T> and passes the type check
2023-
// describe(async function example() {});
2024-
// @ts-expect-error async function is not assignable to describe argument
2025-
describe("example", {}, async () => {});
2026-
// @ts-expect-error async function is not assignable to describe argument
2027-
describe({ name: "example" }, async () => {});
2028-
// @ts-expect-error async function is not assignable to describe argument
2029-
describe({}, async function example() {});
2030-
2031-
const suite = describe("example");
2032-
// @ts-expect-error async function is not assignable to describe argument
2033-
describe(suite, "example", { fn: async () => {} });
2034-
// @ts-expect-error async function is not assignable to describe argument
2035-
describe(suite, "example", async () => {});
2036-
// @ts-expect-error async function is not assignable to describe argument
2037-
describe(suite, async () => {});
2038-
// @ts-expect-error async function is not assignable to describe argument
2039-
describe(suite, "example", {}, async () => {});
2040-
// @ts-expect-error async function is not assignable to describe argument
2041-
describe(suite, { name: "example" }, async () => {});
2042-
// @ts-expect-error async function is not assignable to describe argument
2043-
describe(suite, {}, async function example() {});
2015+
try {
2016+
// @ts-expect-error async function is not assignable to describe argument
2017+
describe({ name: "example", fn: async () => {} });
2018+
// @ts-expect-error async function is not assignable to describe argument
2019+
describe("example", { fn: async () => {} });
2020+
// @ts-expect-error async function is not assignable to describe argument
2021+
describe("example", async () => {});
2022+
// TODO(kt3k): This case should be type error but it's checked as
2023+
// DescribeDefinition<T> and passes the type check
2024+
// describe(async function example() {});
2025+
// @ts-expect-error async function is not assignable to describe argument
2026+
describe("example", {}, async () => {});
2027+
// @ts-expect-error async function is not assignable to describe argument
2028+
describe({ name: "example" }, async () => {});
2029+
// @ts-expect-error async function is not assignable to describe argument
2030+
describe({}, async function example() {});
2031+
2032+
const suite = describe("example");
2033+
// @ts-expect-error async function is not assignable to describe argument
2034+
describe(suite, "example", { fn: async () => {} });
2035+
// @ts-expect-error async function is not assignable to describe argument
2036+
describe(suite, "example", async () => {});
2037+
// @ts-expect-error async function is not assignable to describe argument
2038+
describe(suite, async () => {});
2039+
// @ts-expect-error async function is not assignable to describe argument
2040+
describe(suite, "example", {}, async () => {});
2041+
// @ts-expect-error async function is not assignable to describe argument
2042+
describe(suite, { name: "example" }, async () => {});
2043+
// @ts-expect-error async function is not assignable to describe argument
2044+
describe(suite, {}, async function example() {});
2045+
} catch {
2046+
// Ignores runtime errors as this case is for static type checking
2047+
}
2048+
},
2049+
);
2050+
2051+
await t.step(
2052+
"throws runtime error if async function is passed as describe fn",
2053+
() => {
2054+
assertThrows(
2055+
// deno-lint-ignore no-explicit-any
2056+
() => describe("async describe", (async () => {}) as any),
2057+
Error,
2058+
'Returning a Promise from "describe" is not supported. Tests must be defined synchronously.',
2059+
);
20442060
},
20452061
);
20462062
});

0 commit comments

Comments
 (0)