From 1c6045632de3b0453dfbdc9acef0ddea49ae7407 Mon Sep 17 00:00:00 2001 From: Tom Mrazauskas Date: Thu, 29 Sep 2022 07:22:32 +0300 Subject: [PATCH] =?UTF-8?q?=F0=9F=A4=96=20Merge=20PR=20#62451=20[jest]=20a?= =?UTF-8?q?dd=20types=20of=20`jest.now`=20and=20`withImplementation`=20by?= =?UTF-8?q?=20@mrazauskas?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- types/jest/index.d.ts | 24 +++++++++++++++++++++--- types/jest/jest-tests.ts | 17 +++++++++++++++++ 2 files changed, 38 insertions(+), 3 deletions(-) diff --git a/types/jest/index.d.ts b/types/jest/index.d.ts index 4f9639fac0cf1a..1c8214bbe8884b 100644 --- a/types/jest/index.d.ts +++ b/types/jest/index.d.ts @@ -1,4 +1,4 @@ -// Type definitions for Jest 29.0 +// Type definitions for Jest 29.1 // Project: https://jestjs.io/ // Definitions by: Asana (https://asana.com) // Ivo Stratev @@ -181,9 +181,13 @@ declare namespace jest { * > implementation */ function getRealSystemTime(): number; + /** + * Returns the current time in ms of the fake timer clock. + */ + function now(): number; /** * Indicates that the module system should never return a mocked version - * of the specified module, including all of the specificied module's dependencies. + * of the specified module, including all of the specified module's dependencies. */ function deepUnmock(moduleName: string): typeof jest; /** @@ -1249,7 +1253,21 @@ declare namespace jest { * myMockFn((err, val) => console.log(val)); // false */ mockImplementationOnce(fn: (...args: Y) => T): this; - /** Sets the name of the mock`. */ + /** + * Temporarily overrides the default mock implementation within the callback, + * then restores its previous implementation. + * + * @remarks + * If the callback is async or returns a `thenable`, `withImplementation` will return a promise. + * Awaiting the promise will await the callback and reset the implementation. + */ + withImplementation(fn: (...args: Y) => T, callback: () => Promise): Promise; + /** + * Temporarily overrides the default mock implementation within the callback, + * then restores its previous implementation. + */ + withImplementation(fn: (...args: Y) => T, callback: () => void): void; + /** Sets the name of the mock. */ mockName(name: string): this; /** * Just a simple sugar function for: diff --git a/types/jest/jest-tests.ts b/types/jest/jest-tests.ts index 6cc7a63f7a34f1..9f2c3c8dc73bce 100644 --- a/types/jest/jest-tests.ts +++ b/types/jest/jest-tests.ts @@ -408,6 +408,11 @@ const realSystemTime1: number = jest.getRealSystemTime(); // @ts-expect-error const realSystemTime2: number = jest.getRealSystemTime('foo'); +// $ExpectType number +jest.now(); +// @ts-expect-error +jest.now('1995-12-17T03:24:00'); + // https://jestjs.io/docs/en/jest-object#jestrequireactualmodulename // $ExpectType any jest.requireActual('./thisReturnsTheActualModule'); @@ -573,6 +578,18 @@ const spy3Mock = spy3 .mockReturnValue('value') .mockReturnValueOnce('value'); +// $ExpectType void +spy3.withImplementation( + () => 'mocked value', + () => {}, +); + +// $ExpectType Promise +spy3.withImplementation( + () => 'mocked value', + async () => {}, +); + const spiedPromiseTarget = { resolvesString() { return Promise.resolve('string');