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 setSystemTime and getRealSystemTime fn types #45496

Merged
merged 3 commits into from Jun 24, 2020
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
20 changes: 20 additions & 0 deletions types/jest/index.d.ts
Expand Up @@ -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
*/
Comment on lines +134 to +143
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tried to follow the comment patterns I observed elsewhere in this file, I just copy pasted this comment and the one below straight out of jest's docs.

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.
Expand Down
11 changes: 11 additions & 0 deletions types/jest/jest-tests.ts
Expand Up @@ -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');
Expand Down