From fe144ea7e3cab875a1300b3a4aac30950fdc19ac Mon Sep 17 00:00:00 2001 From: Victor Villas Date: Fri, 4 Dec 2020 08:59:03 -0300 Subject: [PATCH] Add generator function to accepted hook types (#10836) --- packages/jest-circus/src/utils.ts | 14 +++++++++++--- packages/jest-types/src/Global.ts | 9 ++++++--- test-types/top-level-globals.test.ts | 8 ++++++++ 3 files changed, 25 insertions(+), 6 deletions(-) diff --git a/packages/jest-circus/src/utils.ts b/packages/jest-circus/src/utils.ts index 311308f2ce25..85a4f906c589 100644 --- a/packages/jest-circus/src/utils.ts +++ b/packages/jest-circus/src/utils.ts @@ -216,17 +216,25 @@ export const callAsyncCircusFn = ( }); }; - returnedValue = fn.call(testContext, done); + returnedValue = fn.call< + Circus.TestContext | undefined, + Array, + void | Promise | Generator | undefined + >(testContext, done); return; } - let returnedValue; + let returnedValue: any; if (isGeneratorFn(fn)) { returnedValue = co.wrap(fn).call({}); } else { try { - returnedValue = fn.call(testContext); + returnedValue = fn.call< + Circus.TestContext | undefined, + [], + void | Promise | Generator | undefined + >(testContext); } catch (error) { reject(error); return; diff --git a/packages/jest-types/src/Global.ts b/packages/jest-types/src/Global.ts index 411e61191d23..99346e953588 100644 --- a/packages/jest-types/src/Global.ts +++ b/packages/jest-types/src/Global.ts @@ -7,11 +7,14 @@ import type {CoverageMapData} from 'istanbul-lib-coverage'; +export type GeneratorFn = (...args: Array) => Generator; export type DoneFn = (reason?: string | Error) => void; -export type TestName = string; -export type TestFn = ( +export type CallbackFn = ( done?: DoneFn, -) => Promise | void | undefined; +) => void | undefined | Promise; + +export type TestName = string; +export type TestFn = GeneratorFn | CallbackFn; export type ConcurrentTestFn = ( done?: DoneFn, ) => Promise; diff --git a/test-types/top-level-globals.test.ts b/test-types/top-level-globals.test.ts index 8d3827b76e22..03ee7d8b9c89 100644 --- a/test-types/top-level-globals.test.ts +++ b/test-types/top-level-globals.test.ts @@ -19,6 +19,7 @@ import { const fn = () => {}; const asyncFn = async () => {}; +const genFn = function* () {}; const timeout = 5; const testName = 'Test name'; const testTable = [[1, 2]]; @@ -26,17 +27,24 @@ const testTable = [[1, 2]]; // https://jestjs.io/docs/en/api#methods expectType(afterAll(fn)); expectType(afterAll(asyncFn)); +expectType(afterAll(genFn)); expectType(afterAll(fn, timeout)); expectType(afterEach(fn)); expectType(afterEach(asyncFn)); +expectType(afterEach(genFn)); expectType(afterEach(fn, timeout)); expectType(beforeAll(fn)); expectType(beforeAll(asyncFn)); +expectType(beforeAll(genFn)); expectType(beforeAll(fn, timeout)); expectType(beforeEach(fn)); expectType(beforeEach(asyncFn)); +expectType(beforeEach(genFn)); expectType(beforeEach(fn, timeout)); +expectType(test(testName, fn)); +expectType(test(testName, asyncFn)); +expectType(test(testName, genFn)); expectType(test.each(testTable)(testName, fn)); expectType(test.each(testTable)(testName, fn, timeout)); expectType(test.only.each(testTable)(testName, fn));