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 types of jest.now and withImplementation #62451

Merged
merged 1 commit into from Sep 29, 2022
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
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