Skip to content

Commit

Permalink
馃 Merge PR #64601 [jest]: add features released in v29.5 by @mrazauskas
Browse files Browse the repository at this point in the history
* [jest]: add features released in v29.5

* bump version

* ups..
  • Loading branch information
mrazauskas committed Mar 16, 2023
1 parent d38588f commit ecbabd3
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 18 deletions.
66 changes: 48 additions & 18 deletions types/jest/index.d.ts
@@ -1,4 +1,4 @@
// Type definitions for Jest 29.4
// Type definitions for Jest 29.5
// Project: https://jestjs.io/
// Definitions by: Asana (https://asana.com)
// Ivo Stratev <https://github.com/NoHomey>
Expand Down Expand Up @@ -330,40 +330,70 @@ declare namespace jest {
*/
function replaceProperty<T, K extends keyof T>(obj: T, key: K, value: T[K]): ReplaceProperty<T[K]>;
/**
* Exhausts tasks queued by setImmediate().
* > Note: This function is only available when using modern fake timers
* > implementation
* Exhausts tasks queued by `setImmediate()`.
*
* @remarks
* This function is only available when using legacy fake timers implementation.
*/
function runAllImmediates(): void;
/**
* Exhausts the micro-task queue (usually interfaced in node via process.nextTick).
* Exhausts the micro-task queue (i.e., tasks in Node.js scheduled with `process.nextTick()`).
*/
function runAllTicks(): void;
/**
* Exhausts both the macro-task queue (i.e., all tasks queued by setTimeout(),
* setInterval(), and setImmediate()) and the micro-task queue (usually interfaced
* in node via process.nextTick).
* Exhausts both the macro-task queue (i.e., tasks queued by `setTimeout()`, `setInterval()`
* and `setImmediate()`) and the micro-task queue (i.e., tasks in Node.js scheduled with
* `process.nextTick()`).
*/
function runAllTimers(): void;
/**
* Executes only the macro-tasks that are currently pending (i.e., only the
* tasks that have been queued by setTimeout() or setInterval() up to this point).
* If any of the currently pending macro-tasks schedule new macro-tasks,
* those new tasks will not be executed by this call.
* Asynchronous equivalent of `jest.runAllTimers()`. It also yields to the event loop,
* allowing any scheduled promise callbacks to execute _before_ running the timers.
*
* @remarks
* Not available when using legacy fake timers implementation.
*/
function runAllTimersAsync(): Promise<void>;
/**
* Executes only the macro-tasks that are currently pending (i.e., only the tasks that
* have been queued by `setTimeout()`, `setInterval()` and `setImmediate()` up to this point).
*/
function runOnlyPendingTimers(): void;
/**
* Advances all timers by msToRun milliseconds. All pending "macro-tasks" that have been
* queued via setTimeout() or setInterval(), and would be executed within this timeframe
* will be executed.
* Asynchronous equivalent of `jest.runOnlyPendingTimers()`. It also yields to the event loop,
* allowing any scheduled promise callbacks to execute _before_ running the timers.
*
* @remarks
* Not available when using legacy fake timers implementation.
*/
function runOnlyPendingTimersAsync(): Promise<void>;
/**
* Advances all timers by `msToRun` milliseconds. All pending macro-tasks that have been
* queued by `setTimeout()`, `setInterval()` and `setImmediate()`, and would be executed
* within this time frame will be executed.
*/
function advanceTimersByTime(msToRun: number): void;
/**
* Advances all timers by the needed milliseconds so that only the next
* timeouts/intervals will run. Optionally, you can provide steps, so it
* will run steps amount of next timeouts/intervals.
* Asynchronous equivalent of `jest.advanceTimersByTime()`. It also yields to the event loop,
* allowing any scheduled promise callbacks to execute _before_ running the timers.
*
* @remarks
* Not available when using legacy fake timers implementation.
*/
function advanceTimersByTimeAsync(msToRun: number): Promise<void>;
/**
* Advances all timers by the needed milliseconds so that only the next timeouts/intervals will run.
* Optionally, you can provide steps, so it will run steps amount of next timeouts/intervals.
*/
function advanceTimersToNextTimer(step?: number): void;
/**
* Asynchronous equivalent of `jest.advanceTimersToNextTimer()`. It also yields to the event loop,
* allowing any scheduled promise callbacks to execute _before_ running the timers.
*
* @remarks
* Not available when using legacy fake timers implementation.
*/
function advanceTimersToNextTimerAsync(steps?: number): Promise<void>;
/**
* Explicitly supplies the mock object that the module system should return
* for the specified module.
Expand Down
13 changes: 13 additions & 0 deletions types/jest/jest-tests.ts
Expand Up @@ -366,10 +366,17 @@ jest.autoMockOff()
// $ExpectType void
jest.advanceTimersByTime(9001);

// $ExpectType Promise<void>
jest.advanceTimersByTimeAsync(9001);

// $ExpectType void
jest.advanceTimersToNextTimer();
jest.advanceTimersToNextTimer(2);

// $ExpectType Promise<void>
jest.advanceTimersToNextTimerAsync();
jest.advanceTimersToNextTimerAsync(2);

// $ExpectType void
jest.clearAllTimers();

Expand All @@ -390,9 +397,15 @@ jest.runAllTicks();
// $ExpectType void
jest.runAllTimers();

// $ExpectType Promise<void>
jest.runAllTimersAsync();

// $ExpectType void
jest.runOnlyPendingTimers();

// $ExpectType Promise<void>
jest.runOnlyPendingTimersAsync();

// https://jestjs.io/docs/configuration#faketimers-object
jest.useFakeTimers();
jest.useFakeTimers({ legacyFakeTimers: false });
Expand Down

0 comments on commit ecbabd3

Please sign in to comment.