From 104a0f2fb3e51d3ff8828ef4da82bbbc1a10fabe Mon Sep 17 00:00:00 2001 From: Liam Doran Date: Mon, 15 Jun 2020 10:46:55 -0400 Subject: [PATCH 1/5] feat: update setSystemTime types to match FakeTimers._clock --- .../from-config/__tests__/test.js | 12 +++++++++++- .../from-jest-object/__tests__/test.js | 14 +++++++++++++- packages/jest-environment/src/index.ts | 2 +- packages/jest-fake-timers/src/modernFakeTimers.ts | 2 +- packages/jest-runtime/src/index.ts | 2 +- 5 files changed, 27 insertions(+), 5 deletions(-) diff --git a/e2e/modern-fake-timers/from-config/__tests__/test.js b/e2e/modern-fake-timers/from-config/__tests__/test.js index a32e5a5bc8e4..e18450eb6636 100644 --- a/e2e/modern-fake-timers/from-config/__tests__/test.js +++ b/e2e/modern-fake-timers/from-config/__tests__/test.js @@ -7,7 +7,7 @@ 'use strict'; -test('fake timers', () => { +test('fake timers with number argument', () => { jest.setSystemTime(0); expect(Date.now()).toBe(0); @@ -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(1000); + + expect(Date.now()).toBe(1000); +}); diff --git a/e2e/modern-fake-timers/from-jest-object/__tests__/test.js b/e2e/modern-fake-timers/from-jest-object/__tests__/test.js index acf8f56889cd..648e77b2c73d 100644 --- a/e2e/modern-fake-timers/from-jest-object/__tests__/test.js +++ b/e2e/modern-fake-timers/from-jest-object/__tests__/test.js @@ -7,7 +7,7 @@ 'use strict'; -test('fake timers', () => { +test('fake timers with number argument', () => { jest.useFakeTimers('modern'); jest.setSystemTime(0); @@ -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(1000); + + expect(Date.now()).toBe(1000); +}); diff --git a/packages/jest-environment/src/index.ts b/packages/jest-environment/src/index.ts index 21970ecd1543..547afb313373 100644 --- a/packages/jest-environment/src/index.ts +++ b/packages/jest-environment/src/index.ts @@ -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; } diff --git a/packages/jest-fake-timers/src/modernFakeTimers.ts b/packages/jest-fake-timers/src/modernFakeTimers.ts index 03ca30807a89..aba11f4387c6 100644 --- a/packages/jest-fake-timers/src/modernFakeTimers.ts +++ b/packages/jest-fake-timers/src/modernFakeTimers.ts @@ -118,7 +118,7 @@ export default class FakeTimers { } } - setSystemTime(now?: number): void { + setSystemTime(now?: number | Date): void { if (this._checkFakeTimers()) { this._clock.setSystemTime(now); } diff --git a/packages/jest-runtime/src/index.ts b/packages/jest-runtime/src/index.ts index 2cfe0a14d10a..3c63587d2bcd 100644 --- a/packages/jest-runtime/src/index.ts +++ b/packages/jest-runtime/src/index.ts @@ -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) { From 1e0c6241fef86b539896c5c0f41be13b3334d090 Mon Sep 17 00:00:00 2001 From: Liam Doran Date: Mon, 15 Jun 2020 11:01:42 -0400 Subject: [PATCH 2/5] docs: add setSystemTime argument --- docs/JestObjectAPI.md | 2 +- website/versioned_docs/version-26.0/JestObjectAPI.md | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/docs/JestObjectAPI.md b/docs/JestObjectAPI.md index be28b93e4d21..a4970e00f4c0 100644 --- a/docs/JestObjectAPI.md +++ b/docs/JestObjectAPI.md @@ -647,7 +647,7 @@ 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()`. diff --git a/website/versioned_docs/version-26.0/JestObjectAPI.md b/website/versioned_docs/version-26.0/JestObjectAPI.md index eb6c6ebac297..1ce12c0f60ed 100644 --- a/website/versioned_docs/version-26.0/JestObjectAPI.md +++ b/website/versioned_docs/version-26.0/JestObjectAPI.md @@ -648,7 +648,7 @@ 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()`. From 2a30041e12171cd64545e9ae40fc46f6fdfbe119 Mon Sep 17 00:00:00 2001 From: Liam Doran Date: Mon, 15 Jun 2020 11:02:26 -0400 Subject: [PATCH 3/5] docs: remove proceeding . before setSystemTime and getRealSystemTime docs --- docs/JestObjectAPI.md | 4 ++-- website/versioned_docs/version-26.0/JestObjectAPI.md | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/docs/JestObjectAPI.md b/docs/JestObjectAPI.md index a4970e00f4c0..d1205b6768d7 100644 --- a/docs/JestObjectAPI.md +++ b/docs/JestObjectAPI.md @@ -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(now?: number | Date)` +### `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. diff --git a/website/versioned_docs/version-26.0/JestObjectAPI.md b/website/versioned_docs/version-26.0/JestObjectAPI.md index 1ce12c0f60ed..ce3d6f314153 100644 --- a/website/versioned_docs/version-26.0/JestObjectAPI.md +++ b/website/versioned_docs/version-26.0/JestObjectAPI.md @@ -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(now?: number | Date)` +### `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. From 0811af6beb401af99fedf86a041430759e8f57a0 Mon Sep 17 00:00:00 2001 From: Liam Doran Date: Mon, 15 Jun 2020 11:41:16 -0400 Subject: [PATCH 4/5] docs: update changelog --- CHANGELOG.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 85c66a5d976f..9f7103b29bf0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 From 87ca8be99cfec7cd9d6ce51b980632da2d699256 Mon Sep 17 00:00:00 2001 From: Liam Doran Date: Tue, 16 Jun 2020 08:48:01 -0400 Subject: [PATCH 5/5] fix: setSystemTime tests that used number instead of Date --- e2e/modern-fake-timers/from-config/__tests__/test.js | 2 +- e2e/modern-fake-timers/from-jest-object/__tests__/test.js | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/e2e/modern-fake-timers/from-config/__tests__/test.js b/e2e/modern-fake-timers/from-config/__tests__/test.js index e18450eb6636..9974cab6c89b 100644 --- a/e2e/modern-fake-timers/from-config/__tests__/test.js +++ b/e2e/modern-fake-timers/from-config/__tests__/test.js @@ -22,7 +22,7 @@ test('fake timers with Date argument', () => { expect(Date.now()).toBe(0); - jest.setSystemTime(1000); + jest.setSystemTime(new Date(1000)); expect(Date.now()).toBe(1000); }); diff --git a/e2e/modern-fake-timers/from-jest-object/__tests__/test.js b/e2e/modern-fake-timers/from-jest-object/__tests__/test.js index 648e77b2c73d..6fd28535c2bd 100644 --- a/e2e/modern-fake-timers/from-jest-object/__tests__/test.js +++ b/e2e/modern-fake-timers/from-jest-object/__tests__/test.js @@ -26,7 +26,7 @@ test('fake timers with Date argument', () => { expect(Date.now()).toBe(0); - jest.setSystemTime(1000); + jest.setSystemTime(new Date(1000)); expect(Date.now()).toBe(1000); });