diff --git a/CHANGELOG.md b/CHANGELOG.md index b9aaf5641d85..6a48f9d9f0c2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,7 @@ ### Features - `[jest-circus, jest-config, jest-runtime]` Add new `injectGlobals` config and CLI option to disable injecting global variables into the runtime ([#10484](https://github.com/facebook/jest/pull/10484)) +- `[jest-each]` Fixes `.each` type to always be callable ([#10447](https://github.com/facebook/jest/pull/10447)) ### Fixes diff --git a/packages/jest-jasmine2/src/jasmineAsyncInstall.ts b/packages/jest-jasmine2/src/jasmineAsyncInstall.ts index f17288875fd7..db15f338ed75 100644 --- a/packages/jest-jasmine2/src/jasmineAsyncInstall.ts +++ b/packages/jest-jasmine2/src/jasmineAsyncInstall.ts @@ -192,7 +192,7 @@ function makeConcurrent( return spec; }; // each is binded after the function is made concurrent, so for now it is made noop - concurrentFn.each = () => {}; + concurrentFn.each = () => () => {}; return concurrentFn; } diff --git a/packages/jest-types/src/Global.ts b/packages/jest-types/src/Global.ts index e4b9d128d203..3e0354ec3403 100644 --- a/packages/jest-types/src/Global.ts +++ b/packages/jest-types/src/Global.ts @@ -45,7 +45,7 @@ type Each = test: EachTestFn, timeout?: number, ) => void) - | (() => void); + | (() => () => void); export interface HookBase { (fn: HookFn, timeout?: number): void; diff --git a/test-types/top-level-globals.test.ts b/test-types/top-level-globals.test.ts index d79fb931dd2b..9b6499411362 100644 --- a/test-types/top-level-globals.test.ts +++ b/test-types/top-level-globals.test.ts @@ -8,18 +8,55 @@ */ import {expectType} from 'mlh-tsd'; -//eslint-disable-next-line import/no-extraneous-dependencies -import {afterAll, afterEach, beforeAll, beforeEach} from '@jest/globals'; +import { + afterAll, + afterEach, + beforeAll, + beforeEach, + describe, + test, + //eslint-disable-next-line import/no-extraneous-dependencies +} from '@jest/globals'; const fn = () => {}; +const asyncFn = async () => {}; const timeout = 5; +const testName = 'Test name'; +const testTable = [[1, 2]]; // https://jestjs.io/docs/en/api#methods expectType(afterAll(fn)); +expectType(afterAll(asyncFn)); expectType(afterAll(fn, timeout)); expectType(afterEach(fn)); +expectType(afterEach(asyncFn)); expectType(afterEach(fn, timeout)); expectType(beforeAll(fn)); +expectType(beforeAll(asyncFn)); expectType(beforeAll(fn, timeout)); expectType(beforeEach(fn)); +expectType(beforeEach(asyncFn)); expectType(beforeEach(fn, timeout)); + +expectType(test.each(testTable)(testName, fn)); +expectType(test.each(testTable)(testName, fn, timeout)); +expectType(test.only.each(testTable)(testName, fn)); +expectType(test.only.each(testTable)(testName, fn, timeout)); +expectType(test.skip.each(testTable)(testName, fn)); +expectType(test.skip.each(testTable)(testName, fn, timeout)); +expectType(test.concurrent.each(testTable)(testName, asyncFn)); +expectType(test.concurrent.each(testTable)(testName, asyncFn, timeout)); +expectType(test.concurrent.only.each(testTable)(testName, asyncFn)); +expectType( + test.concurrent.only.each(testTable)(testName, asyncFn, timeout), +); +expectType(test.concurrent.skip.each(testTable)(testName, asyncFn)); +expectType( + test.concurrent.skip.each(testTable)(testName, asyncFn, timeout), +); +expectType(describe.each(testTable)(testName, fn)); +expectType(describe.each(testTable)(testName, fn, timeout)); +expectType(describe.only.each(testTable)(testName, fn)); +expectType(describe.only.each(testTable)(testName, fn, timeout)); +expectType(describe.skip.each(testTable)(testName, fn)); +expectType(describe.skip.each(testTable)(testName, fn, timeout));