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

[jest]: add features released in v29.4 #64052

Merged
merged 18 commits into from Jan 25, 2023
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
27 changes: 22 additions & 5 deletions 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 <https://github.com/NoHomey>
Expand Down Expand Up @@ -258,6 +258,17 @@ declare namespace jest {
*/
// eslint-disable-next-line no-unnecessary-generics
function genMockFromModule<T>(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.
*/
Expand All @@ -267,7 +278,6 @@ declare namespace jest {
*/
// eslint-disable-next-line no-unnecessary-generics
function mock<T = unknown>(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
Expand Down Expand Up @@ -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<void>): Promise<void>;
/**
* Runs failed tests n-times until they pass or until the max number of retries is exhausted.
* This only works with jest-circus!
Expand Down Expand Up @@ -430,7 +445,8 @@ declare namespace jest {
type ConstructorArgumentsOf<T> = T extends new (...args: infer A) => any ? A : never;
type ConstructorReturnType<T> = T extends new (...args: any) => infer C ? C : any;

interface MockWithArgs<T extends MockableFunction> extends MockInstance<ReturnType<T>, ArgumentsOf<T>, ConstructorReturnType<T>> {
interface MockWithArgs<T extends MockableFunction>
extends MockInstance<ReturnType<T>, ArgumentsOf<T>, ConstructorReturnType<T>> {
Comment on lines +448 to +449
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Prettier cleanup up some formatting here and below.

new (...args: ConstructorArgumentsOf<T>): T;
(...args: ArgumentsOf<T>): ReturnType<T>;
}
Expand Down Expand Up @@ -1231,7 +1247,8 @@ declare namespace jest {
ReturnType<T>,
ArgsType<T>,
T extends (this: infer C, ...args: any[]) => any ? C : never
> & T;
> &
T;

/**
* Wrap a class with mock definitions
Expand Down
8 changes: 8 additions & 0 deletions types/jest/jest-tests.ts
Expand Up @@ -373,6 +373,14 @@ jest.advanceTimersToNextTimer(2);
// $ExpectType void
jest.clearAllTimers();

// $ExpectType boolean
jest.isEnvironmentTornDown();

// $ExpectType Promise<void>
jest.isolateModulesAsync(async () => {});
// @ts-expect-error: does not allow sync callbacks
jest.isolateModulesAsync(() => {});

// $ExpectType void
jest.runAllImmediates();

Expand Down