From 45f6644b80e67f63f1a5de60ab9736aaf04d75e7 Mon Sep 17 00:00:00 2001 From: Liam Doran Date: Mon, 15 Jun 2020 11:54:48 -0400 Subject: [PATCH 1/3] feat: add setSystemTime and getRealSystemTime to jest types --- types/jest/index.d.ts | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/types/jest/index.d.ts b/types/jest/index.d.ts index 3905f0ff7be207..c4eade3e4d14d4 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 | Date): 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. From 72fda94c51a915a52cd8d789d7837839a5dea3c7 Mon Sep 17 00:00:00 2001 From: Liam Doran Date: Mon, 15 Jun 2020 12:03:18 -0400 Subject: [PATCH 2/3] test: jest setSystemTime and getRealSystemTime --- types/jest/jest-tests.ts | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/types/jest/jest-tests.ts b/types/jest/jest-tests.ts index 09458eda6dfb1c..47e5b1129b20ea 100644 --- a/types/jest/jest-tests.ts +++ b/types/jest/jest-tests.ts @@ -291,6 +291,18 @@ jest.useFakeTimers('legacy'); // $ExpectError jest.useFakeTimers('foo'); +// https://jestjs.io/docs/en/jest-object#jestsetsystemtimenow-number--date +jest.setSystemTime(); +jest.setSystemTime(0); +jest.setSystemTime(new Date(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'); From 725c00d5da5651ae54f3b98679d54d28f8279751 Mon Sep 17 00:00:00 2001 From: Liam Doran Date: Tue, 16 Jun 2020 08:50:47 -0400 Subject: [PATCH 3/3] feat: remove Date from setSystemTime types while not yet officially supported --- types/jest/index.d.ts | 2 +- types/jest/jest-tests.ts | 1 - 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/types/jest/index.d.ts b/types/jest/index.d.ts index c4eade3e4d14d4..9d66a8a3c1def0 100644 --- a/types/jest/index.d.ts +++ b/types/jest/index.d.ts @@ -141,7 +141,7 @@ declare namespace jest { * > Note: This function is only available when using modern fake timers * > implementation */ - function setSystemTime(now?: number | Date): void; + 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 diff --git a/types/jest/jest-tests.ts b/types/jest/jest-tests.ts index 47e5b1129b20ea..7a537e997d237e 100644 --- a/types/jest/jest-tests.ts +++ b/types/jest/jest-tests.ts @@ -294,7 +294,6 @@ jest.useFakeTimers('foo'); // https://jestjs.io/docs/en/jest-object#jestsetsystemtimenow-number--date jest.setSystemTime(); jest.setSystemTime(0); -jest.setSystemTime(new Date(0)); // $ExpectError jest.setSystemTime('foo');