Skip to content

Commit

Permalink
Add generator function to accepted hook types (#10836)
Browse files Browse the repository at this point in the history
  • Loading branch information
villasv authored Dec 4, 2020
1 parent e12a302 commit fe144ea
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 6 deletions.
14 changes: 11 additions & 3 deletions packages/jest-circus/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -216,17 +216,25 @@ export const callAsyncCircusFn = (
});
};

returnedValue = fn.call(testContext, done);
returnedValue = fn.call<
Circus.TestContext | undefined,
Array<typeof done>,
void | Promise<unknown> | 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<unknown> | Generator | undefined
>(testContext);
} catch (error) {
reject(error);
return;
Expand Down
9 changes: 6 additions & 3 deletions packages/jest-types/src/Global.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,14 @@

import type {CoverageMapData} from 'istanbul-lib-coverage';

export type GeneratorFn = (...args: Array<any>) => Generator;
export type DoneFn = (reason?: string | Error) => void;
export type TestName = string;
export type TestFn = (
export type CallbackFn = (
done?: DoneFn,
) => Promise<void | undefined | unknown> | void | undefined;
) => void | undefined | Promise<void | undefined | unknown>;

export type TestName = string;
export type TestFn = GeneratorFn | CallbackFn;
export type ConcurrentTestFn = (
done?: DoneFn,
) => Promise<void | undefined | unknown>;
Expand Down
8 changes: 8 additions & 0 deletions test-types/top-level-globals.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,24 +19,32 @@ import {

const fn = () => {};
const asyncFn = async () => {};
const genFn = function* () {};
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(genFn));
expectType<void>(afterAll(fn, timeout));
expectType<void>(afterEach(fn));
expectType<void>(afterEach(asyncFn));
expectType<void>(afterEach(genFn));
expectType<void>(afterEach(fn, timeout));
expectType<void>(beforeAll(fn));
expectType<void>(beforeAll(asyncFn));
expectType<void>(beforeAll(genFn));
expectType<void>(beforeAll(fn, timeout));
expectType<void>(beforeEach(fn));
expectType<void>(beforeEach(asyncFn));
expectType<void>(beforeEach(genFn));
expectType<void>(beforeEach(fn, timeout));

expectType<void>(test(testName, fn));
expectType<void>(test(testName, asyncFn));
expectType<void>(test(testName, genFn));
expectType<void>(test.each(testTable)(testName, fn));
expectType<void>(test.each(testTable)(testName, fn, timeout));
expectType<void>(test.only.each(testTable)(testName, fn));
Expand Down

0 comments on commit fe144ea

Please sign in to comment.