From ec75280f0f67fa3dc61df32cc4af2577d3656943 Mon Sep 17 00:00:00 2001 From: Victor Villas Date: Tue, 8 Sep 2020 18:53:54 -0300 Subject: [PATCH] Fix lifecycle hook function types (#10480) --- CHANGELOG.md | 2 ++ packages/jest-types/src/Global.ts | 12 ++++++++---- test-types/top-level-globals.test.ts | 25 +++++++++++++++++++++++++ 3 files changed, 35 insertions(+), 4 deletions(-) create mode 100644 test-types/top-level-globals.test.ts diff --git a/CHANGELOG.md b/CHANGELOG.md index 8951b25c58ee..b9aaf5641d85 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,8 @@ ### Fixes +- Fix lifecycle hook function types ([#10480](https://github.com/facebook/jest/pull/10480)) + ### Chore & Maintenance ### Performance diff --git a/packages/jest-types/src/Global.ts b/packages/jest-types/src/Global.ts index 2102d0c0e4b4..e4b9d128d203 100644 --- a/packages/jest-types/src/Global.ts +++ b/packages/jest-types/src/Global.ts @@ -47,6 +47,10 @@ type Each = ) => void) | (() => void); +export interface HookBase { + (fn: HookFn, timeout?: number): void; +} + export interface ItBase { (testName: TestName, fn: TestFn, timeout?: number): void; each: Each; @@ -91,10 +95,10 @@ export interface TestFrameworkGlobals { describe: Describe; xdescribe: DescribeBase; fdescribe: DescribeBase; - beforeAll: HookFn; - beforeEach: HookFn; - afterEach: HookFn; - afterAll: HookFn; + beforeAll: HookBase; + beforeEach: HookBase; + afterEach: HookBase; + afterAll: HookBase; } export interface GlobalAdditions extends TestFrameworkGlobals { diff --git a/test-types/top-level-globals.test.ts b/test-types/top-level-globals.test.ts new file mode 100644 index 000000000000..d79fb931dd2b --- /dev/null +++ b/test-types/top-level-globals.test.ts @@ -0,0 +1,25 @@ +/** + * Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved. + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + * + * @type ./empty.d.ts + */ + +import {expectType} from 'mlh-tsd'; +//eslint-disable-next-line import/no-extraneous-dependencies +import {afterAll, afterEach, beforeAll, beforeEach} from '@jest/globals'; + +const fn = () => {}; +const timeout = 5; + +// https://jestjs.io/docs/en/api#methods +expectType(afterAll(fn)); +expectType(afterAll(fn, timeout)); +expectType(afterEach(fn)); +expectType(afterEach(fn, timeout)); +expectType(beforeAll(fn)); +expectType(beforeAll(fn, timeout)); +expectType(beforeEach(fn)); +expectType(beforeEach(fn, timeout));