From fbb899904e79e7e80083690be744f6e17871ad03 Mon Sep 17 00:00:00 2001 From: Simen Bekkhus Date: Sat, 5 Dec 2020 12:43:22 +0100 Subject: [PATCH] pass `this` to test callbacks --- packages/jest-types/src/Circus.ts | 2 +- packages/jest-types/src/Global.ts | 16 +++++++++++++--- 2 files changed, 14 insertions(+), 4 deletions(-) diff --git a/packages/jest-types/src/Circus.ts b/packages/jest-types/src/Circus.ts index c5f941efc11c..3dab1d8d7abe 100644 --- a/packages/jest-types/src/Circus.ts +++ b/packages/jest-types/src/Circus.ts @@ -20,7 +20,7 @@ export type HookFn = Global.HookFn; export type AsyncFn = TestFn | HookFn; export type SharedHookType = 'afterAll' | 'beforeAll'; export type HookType = SharedHookType | 'afterEach' | 'beforeEach'; -export type TestContext = Record; +export type TestContext = Global.TestContext; export type Exception = any; // Since in JS anything can be thrown as an error. export type FormattedError = string; // String representation of error. export type Hook = { diff --git a/packages/jest-types/src/Global.ts b/packages/jest-types/src/Global.ts index 4d99c1214ead..0c2031cbb80a 100644 --- a/packages/jest-types/src/Global.ts +++ b/packages/jest-types/src/Global.ts @@ -12,10 +12,20 @@ type TestReturnValuePromise = Promise; type TestReturnValueGenerator = Generator; export type TestReturnValue = ValidTestReturnValues | TestReturnValuePromise; +export type TestContext = Record; + export type DoneFn = (reason?: string | Error) => void; -export type DoneTakingTestFn = (done: DoneFn) => ValidTestReturnValues; -export type PromiseReturningTestFn = () => TestReturnValue; -export type GeneratorReturningTestFn = () => TestReturnValueGenerator; +// these should not be undefined +export type DoneTakingTestFn = ( + this: TestContext | undefined, + done: DoneFn, +) => ValidTestReturnValues; +export type PromiseReturningTestFn = ( + this: TestContext | undefined, +) => TestReturnValue; +export type GeneratorReturningTestFn = ( + this: TestContext | undefined, +) => TestReturnValueGenerator; export type TestName = string; export type TestFn =