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 #10526

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
14 changes: 11 additions & 3 deletions packages/jest-circus/src/utils.ts
Expand Up @@ -214,17 +214,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
7 changes: 6 additions & 1 deletion packages/jest-types/src/Global.ts
Expand Up @@ -7,6 +7,11 @@

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

// TypeScript's GeneratorFunction is too narrow
interface GeneratorFn {
(...args: Array<any>): Generator;
}

export type DoneFn = (reason?: string | Error) => void;
export type TestName = string;
export type TestFn = (
Expand All @@ -17,7 +22,7 @@ export type ConcurrentTestFn = (
) => Promise<void | undefined | unknown>;
export type BlockFn = () => void;
export type BlockName = string;
export type HookFn = TestFn;
export type HookFn = TestFn | GeneratorFn;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

test functions can also be generators, so change that definition instead?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, I couldn't be sure just by reading the docs. On it.


export type Col = unknown;
export type Row = Array<Col>;
Expand Down
5 changes: 5 additions & 0 deletions test-types/top-level-globals.test.ts
Expand Up @@ -20,22 +20,27 @@ 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.each(testTable)(testName, fn));
Expand Down