Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add generator function to accepted hook types #10836

Merged
merged 1 commit into from
Dec 4, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 11 additions & 3 deletions packages/jest-circus/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -215,17 +215,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