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

setSystemTime and getRealSystemTime type and doc update #10169

Merged
merged 5 commits into from Jun 16, 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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Expand Up @@ -19,6 +19,8 @@
- `[jest-core]` 🎉🎉🎉🎉🎉🎉🎉🎉🎉🎉🎉🎉🎉🎉🎉🎉🎉🎉🎉🎉🎉🎉🎉🎉🎉 ([#10000](https://github.com/facebook/jest/pull/10000))
- `[jest-core, jest-reporters, jest-test-result, jest-types]` Cleanup `displayName` type ([#10049](https://github.com/facebook/jest/pull/10049))
- `[jest-runtime]` Jest-internal sandbox escape hatch ([#9907](https://github.com/facebook/jest/pull/9907))
- `[jest-fake-timers]` Update `now` param type to support `Date` in addition to `number`. ([#10169](https://github.com/facebook/jest/pull/10169))
- `[docs]` Add param to `setSystemTime` docs and remove preceding period from it and `getRealSystemTime` ([#10169](https://github.com/facebook/jest/pull/10169))

### Performance

Expand Down
4 changes: 2 additions & 2 deletions docs/JestObjectAPI.md
Expand Up @@ -647,13 +647,13 @@ This means, if any timers have been scheduled (but have not yet executed), they

Returns the number of fake timers still left to run.

### `.jest.setSystemTime()`
### `jest.setSystemTime(now?: number | Date)`
Copy link
Member

Choose a reason for hiding this comment

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

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Copy link
Member

Choose a reason for hiding this comment

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

Oh, seems I'm blind 😅 Thanks!


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

### `.jest.getRealSystemTime()`
### `jest.getRealSystemTime()`

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.

Expand Down
12 changes: 11 additions & 1 deletion e2e/modern-fake-timers/from-config/__tests__/test.js
Expand Up @@ -7,7 +7,7 @@

'use strict';

test('fake timers', () => {
test('fake timers with number argument', () => {
jest.setSystemTime(0);

expect(Date.now()).toBe(0);
Expand All @@ -16,3 +16,13 @@ test('fake timers', () => {

expect(Date.now()).toBe(1000);
});

test('fake timers with Date argument', () => {
jest.setSystemTime(new Date(0));

expect(Date.now()).toBe(0);

jest.setSystemTime(new Date(1000));

expect(Date.now()).toBe(1000);
});
14 changes: 13 additions & 1 deletion e2e/modern-fake-timers/from-jest-object/__tests__/test.js
Expand Up @@ -7,7 +7,7 @@

'use strict';

test('fake timers', () => {
test('fake timers with number argument', () => {
jest.useFakeTimers('modern');

jest.setSystemTime(0);
Expand All @@ -18,3 +18,15 @@ test('fake timers', () => {

expect(Date.now()).toBe(1000);
});

test('fake timers with Date argument', () => {
jest.useFakeTimers('modern');

jest.setSystemTime(new Date(0));

expect(Date.now()).toBe(0);

jest.setSystemTime(new Date(1000));

expect(Date.now()).toBe(1000);
});
2 changes: 1 addition & 1 deletion packages/jest-environment/src/index.ts
Expand Up @@ -306,5 +306,5 @@ export interface Jest {
*
* > Note: This function is only available when using Lolex as fake timers implementation
*/
setSystemTime(now?: number): void;
setSystemTime(now?: number | Date): void;
}
2 changes: 1 addition & 1 deletion packages/jest-fake-timers/src/modernFakeTimers.ts
Expand Up @@ -118,7 +118,7 @@ export default class FakeTimers {
}
}

setSystemTime(now?: number): void {
setSystemTime(now?: number | Date): void {
if (this._checkFakeTimers()) {
this._clock.setSystemTime(now);
}
Expand Down
2 changes: 1 addition & 1 deletion packages/jest-runtime/src/index.ts
Expand Up @@ -1559,7 +1559,7 @@ class Runtime {
_getFakeTimers().advanceTimersByTime(msToRun),
setMock: (moduleName: string, mock: unknown) =>
setMockFactory(moduleName, () => mock),
setSystemTime: (now?: number) => {
setSystemTime: (now?: number | Date) => {
const fakeTimers = _getFakeTimers();

if (fakeTimers instanceof ModernFakeTimers) {
Expand Down
4 changes: 2 additions & 2 deletions website/versioned_docs/version-26.0/JestObjectAPI.md
Expand Up @@ -648,13 +648,13 @@ This means, if any timers have been scheduled (but have not yet executed), they

Returns the number of fake timers still left to run.

### `.jest.setSystemTime()`
### `jest.setSystemTime(now?: number | Date)`

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

### `.jest.getRealSystemTime()`
### `jest.getRealSystemTime()`

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.

Expand Down