diff --git a/types/jest/index.d.ts b/types/jest/index.d.ts index ad9ba4bfab444c..5201510824b0d1 100644 --- a/types/jest/index.d.ts +++ b/types/jest/index.d.ts @@ -1,4 +1,4 @@ -// Type definitions for Jest 29.2 +// Type definitions for Jest 29.4 // Project: https://jestjs.io/ // Definitions by: Asana (https://asana.com) // Ivo Stratev @@ -258,6 +258,17 @@ declare namespace jest { */ // eslint-disable-next-line no-unnecessary-generics function genMockFromModule(moduleName: string): T; + /** + * Returns `true` if test environment has been torn down. + * + * @example + * + * if (jest.isEnvironmentTornDown()) { + * // The Jest environment has been torn down, so stop doing work + * return; + * } + */ + function isEnvironmentTornDown(): boolean; /** * Returns whether the given function is a mock function. */ @@ -267,7 +278,6 @@ declare namespace jest { */ // eslint-disable-next-line no-unnecessary-generics function mock(moduleName: string, factory?: () => T, options?: MockOptions): typeof jest; - /** * Wraps types of the `source` object and its deep members with type definitions * of Jest mock function. Pass `{shallow: true}` option to disable the deeply @@ -296,10 +306,15 @@ declare namespace jest { */ function resetModules(): typeof jest; /** - * Creates a sandbox registry for the modules that are loaded inside the callback function.. + * Creates a sandbox registry for the modules that are loaded inside the callback function. * This is useful to isolate specific modules for every test so that local module state doesn't conflict between tests. */ function isolateModules(fn: () => void): typeof jest; + /** + * Equivalent of `jest.isolateModules()` for async functions to be wrapped. + * The caller is expected to `await` the completion of `jest.isolateModulesAsync()`. + */ + function isolateModulesAsync(fn: () => Promise): Promise; /** * Runs failed tests n-times until they pass or until the max number of retries is exhausted. * This only works with jest-circus! @@ -430,7 +445,8 @@ declare namespace jest { type ConstructorArgumentsOf = T extends new (...args: infer A) => any ? A : never; type ConstructorReturnType = T extends new (...args: any) => infer C ? C : any; - interface MockWithArgs extends MockInstance, ArgumentsOf, ConstructorReturnType> { + interface MockWithArgs + extends MockInstance, ArgumentsOf, ConstructorReturnType> { new (...args: ConstructorArgumentsOf): T; (...args: ArgumentsOf): ReturnType; } @@ -1231,7 +1247,8 @@ declare namespace jest { ReturnType, ArgsType, T extends (this: infer C, ...args: any[]) => any ? C : never - > & T; + > & + T; /** * Wrap a class with mock definitions diff --git a/types/jest/jest-tests.ts b/types/jest/jest-tests.ts index 41eaa80f605408..0d75eda32b5e5a 100644 --- a/types/jest/jest-tests.ts +++ b/types/jest/jest-tests.ts @@ -373,6 +373,14 @@ jest.advanceTimersToNextTimer(2); // $ExpectType void jest.clearAllTimers(); +// $ExpectType boolean +jest.isEnvironmentTornDown(); + +// $ExpectType Promise +jest.isolateModulesAsync(async () => {}); +// @ts-expect-error: does not allow sync callbacks +jest.isolateModulesAsync(() => {}); + // $ExpectType void jest.runAllImmediates();