Skip to content

Commit

Permalink
🤖 Merge PR #62451 [jest] add types of jest.now and `withImplementat…
Browse files Browse the repository at this point in the history
…ion` by @mrazauskas
  • Loading branch information
mrazauskas committed Sep 29, 2022
1 parent b948ad5 commit 1c60456
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 3 deletions.
24 changes: 21 additions & 3 deletions 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 <https://github.com/NoHomey>
Expand Down Expand Up @@ -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;
/**
Expand Down Expand Up @@ -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<unknown>): Promise<void>;
/**
* 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:
Expand Down
17 changes: 17 additions & 0 deletions types/jest/jest-tests.ts
Expand Up @@ -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');
Expand Down Expand Up @@ -573,6 +578,18 @@ const spy3Mock = spy3
.mockReturnValue('value')
.mockReturnValueOnce('value');

// $ExpectType void
spy3.withImplementation(
() => 'mocked value',
() => {},
);

// $ExpectType Promise<void>
spy3.withImplementation(
() => 'mocked value',
async () => {},
);

const spiedPromiseTarget = {
resolvesString() {
return Promise.resolve('string');
Expand Down

0 comments on commit 1c60456

Please sign in to comment.