diff --git a/types/jest/index.d.ts b/types/jest/index.d.ts index 3905f0ff7be207..9d66a8a3c1def0 100644 --- a/types/jest/index.d.ts +++ b/types/jest/index.d.ts @@ -131,6 +131,26 @@ declare namespace jest { * Returns the number of fake timers still left to run. */ function getTimerCount(): number; + /** + * Set the current system time used by fake timers. Simulates a user + * changing the system clock while your program is running. It affects the + * current time but it does not in itself cause e.g. timers to fire; they + * will fire exactly as they would have done without the call to + * jest.setSystemTime(). + * + * > Note: This function is only available when using modern fake timers + * > implementation + */ + function setSystemTime(now?: number): void; + /** + * When mocking time, Date.now() will also be mocked. If you for some + * reason need access to the real current time, you can invoke this + * function. + * + * > Note: This function is only available when using modern fake timers + * > implementation + */ + function getRealSystemTime(): number; /** * Indicates that the module system should never return a mocked version * of the specified module, including all of the specificied module's dependencies. diff --git a/types/jest/jest-tests.ts b/types/jest/jest-tests.ts index 09458eda6dfb1c..7a537e997d237e 100644 --- a/types/jest/jest-tests.ts +++ b/types/jest/jest-tests.ts @@ -291,6 +291,17 @@ jest.useFakeTimers('legacy'); // $ExpectError jest.useFakeTimers('foo'); +// https://jestjs.io/docs/en/jest-object#jestsetsystemtimenow-number--date +jest.setSystemTime(); +jest.setSystemTime(0); +// $ExpectError +jest.setSystemTime('foo'); + +// https://jestjs.io/docs/en/jest-object#jestgetrealsystemtime +const realSystemTime1: number = jest.getRealSystemTime(); +// $ExpectError +const realSystemTime2: number = jest.getRealSystemTime('foo'); + // https://jestjs.io/docs/en/jest-object#jestrequireactualmodulename // $ExpectType any jest.requireActual('./thisReturnsTheActualModule');