Skip to content

Commit

Permalink
fix: test.each type always return a callable function (#10488)
Browse files Browse the repository at this point in the history
  • Loading branch information
scttcper committed Sep 13, 2020
1 parent aa02c2d commit a79c34b
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 4 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion packages/jest-jasmine2/src/jasmineAsyncInstall.ts
Expand Up @@ -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;
}

Expand Down
2 changes: 1 addition & 1 deletion packages/jest-types/src/Global.ts
Expand Up @@ -45,7 +45,7 @@ type Each<EachCallback extends TestCallback> =
test: EachTestFn<EachCallback>,
timeout?: number,
) => void)
| (() => void);
| (() => () => void);

export interface HookBase {
(fn: HookFn, timeout?: number): void;
Expand Down
41 changes: 39 additions & 2 deletions test-types/top-level-globals.test.ts
Expand Up @@ -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<void>(afterAll(fn));
expectType<void>(afterAll(asyncFn));
expectType<void>(afterAll(fn, timeout));
expectType<void>(afterEach(fn));
expectType<void>(afterEach(asyncFn));
expectType<void>(afterEach(fn, timeout));
expectType<void>(beforeAll(fn));
expectType<void>(beforeAll(asyncFn));
expectType<void>(beforeAll(fn, timeout));
expectType<void>(beforeEach(fn));
expectType<void>(beforeEach(asyncFn));
expectType<void>(beforeEach(fn, timeout));

expectType<void>(test.each(testTable)(testName, fn));
expectType<void>(test.each(testTable)(testName, fn, timeout));
expectType<void>(test.only.each(testTable)(testName, fn));
expectType<void>(test.only.each(testTable)(testName, fn, timeout));
expectType<void>(test.skip.each(testTable)(testName, fn));
expectType<void>(test.skip.each(testTable)(testName, fn, timeout));
expectType<void>(test.concurrent.each(testTable)(testName, asyncFn));
expectType<void>(test.concurrent.each(testTable)(testName, asyncFn, timeout));
expectType<void>(test.concurrent.only.each(testTable)(testName, asyncFn));
expectType<void>(
test.concurrent.only.each(testTable)(testName, asyncFn, timeout),
);
expectType<void>(test.concurrent.skip.each(testTable)(testName, asyncFn));
expectType<void>(
test.concurrent.skip.each(testTable)(testName, asyncFn, timeout),
);
expectType<void>(describe.each(testTable)(testName, fn));
expectType<void>(describe.each(testTable)(testName, fn, timeout));
expectType<void>(describe.only.each(testTable)(testName, fn));
expectType<void>(describe.only.each(testTable)(testName, fn, timeout));
expectType<void>(describe.skip.each(testTable)(testName, fn));
expectType<void>(describe.skip.each(testTable)(testName, fn, timeout));

0 comments on commit a79c34b

Please sign in to comment.