From 7d7228418b2193abcb7ecf1393334f1d1cf66f42 Mon Sep 17 00:00:00 2001 From: Simen Bekkhus Date: Tue, 30 Oct 2018 16:45:42 +0100 Subject: [PATCH] only mock timer functions currently mocked by Jest --- CHANGELOG.md | 2 +- docs/JestObjectAPI.md | 8 -------- flow-typed/npm/lolex_v2.x.x.js | 1 - packages/jest-runtime/src/index.js | 3 --- packages/jest-util/src/FakeTimers.js | 24 +++++++++--------------- types/Environment.js | 2 -- types/Jest.js | 2 -- 7 files changed, 10 insertions(+), 32 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 1da5dce0081d..b5bd1912ab82 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -27,7 +27,7 @@ - `[babel-preset-jest]` [**BREAKING**] Export a function instead of an object for Babel 7 compatibility ([#7203](https://github.com/facebook/jest/pull/7203)) - `[expect]` Check constructor equality in .toStrictEqual() ([#7005](https://github.com/facebook/jest/pull/7005)) - `[jest-util]` Add `jest.getTimerCount()` to get the count of scheduled fake timers ([#7285](https://github.com/facebook/jest/pull/7285)) -- `[jest-util]`[**BREAKING**] Replace Jest's fake timers implementation with Lolex ([#5171](https://github.com/facebook/jest/pull/5171)) +- `[jest-util]`[**BREAKING**] Replace Jest's fake timers implementation with Lolex ([#7300](https://github.com/facebook/jest/pull/7300)) ### Fixes diff --git a/docs/JestObjectAPI.md b/docs/JestObjectAPI.md index 7bfdc634ae5b..edb1ff542587 100644 --- a/docs/JestObjectAPI.md +++ b/docs/JestObjectAPI.md @@ -447,14 +447,6 @@ 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()` - -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()`. - -### `.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. - ## Misc ### `jest.setTimeout(timeout)` diff --git a/flow-typed/npm/lolex_v2.x.x.js b/flow-typed/npm/lolex_v2.x.x.js index 62f677d8e4d8..922e9ace8d45 100644 --- a/flow-typed/npm/lolex_v2.x.x.js +++ b/flow-typed/npm/lolex_v2.x.x.js @@ -40,7 +40,6 @@ declare module 'lolex' { runMicrotasks(): void; runToFrame(): void; runToLast(): void; - setSystemTime(now?: number | Date): void; uninstall(): Object[]; Date: typeof Date; Performance: typeof Performance; diff --git a/packages/jest-runtime/src/index.js b/packages/jest-runtime/src/index.js index 87855867eb31..a54b9a9cae7a 100644 --- a/packages/jest-runtime/src/index.js +++ b/packages/jest-runtime/src/index.js @@ -934,7 +934,6 @@ class Runtime { fn, genMockFromModule: (moduleName: string) => this._generateMock(from, moduleName), - getRealSystemTime: () => this._environment.fakeTimers.getRealSystemTime(), getTimerCount: () => this._environment.fakeTimers.getTimerCount(), isMockFunction: this._moduleMocker.isMockFunction, mock, @@ -953,8 +952,6 @@ class Runtime { this._environment.fakeTimers.advanceTimersByTime(msToRun), setMock: (moduleName: string, mock: Object) => setMockFactory(moduleName, () => mock), - setSystemTime: (now?: number) => - this._environment.fakeTimers.setSystemTime(now), setTimeout, spyOn, unmock, diff --git a/packages/jest-util/src/FakeTimers.js b/packages/jest-util/src/FakeTimers.js index d3209e5623e3..9fc4f8953ae1 100644 --- a/packages/jest-util/src/FakeTimers.js +++ b/packages/jest-util/src/FakeTimers.js @@ -81,14 +81,20 @@ export default class FakeTimers { } useFakeTimers() { - const toFake = Object.keys(this._lolex.timers); - if (!this._fakingTime) { this._clock = this._lolex.install({ loopLimit: this._maxLoops, now: Date.now(), target: this._global, - toFake, + toFake: [ + 'setTimeout', + 'clearTimeout', + 'setImmediate', + 'clearImmediate', + 'setInterval', + 'clearInterval', + 'nextTick', + ], }); this._fakingTime = true; @@ -97,22 +103,10 @@ export default class FakeTimers { reset() { if (this._checkFakeTimers()) { - const {now} = this._clock; this._clock.reset(); - this._clock.setSystemTime(now); } } - setSystemTime(now?: number) { - if (this._checkFakeTimers()) { - this._clock.setSystemTime(now); - } - } - - getRealSystemTime() { - return Date.now(); - } - getTimerCount() { if (this._checkFakeTimers()) { return this._clock.countTimers(); diff --git a/types/Environment.js b/types/Environment.js index 5ca7baed91ba..b9a6081f55a1 100644 --- a/types/Environment.js +++ b/types/Environment.js @@ -28,8 +28,6 @@ declare class $JestEnvironment { runOnlyPendingTimers(): void, runWithRealTimers(callback: any): void, getTimerCount(): number, - setSystemTime(now?: number): void, - getRealSystemTime(): number, useFakeTimers(): void, useRealTimers(): void, }; diff --git a/types/Jest.js b/types/Jest.js index 4da932a929d1..b32366bd7b7a 100644 --- a/types/Jest.js +++ b/types/Jest.js @@ -36,8 +36,6 @@ export type Jest = {| runAllTicks(): void, runAllTimers(): void, runOnlyPendingTimers(): void, - getRealSystemTime(): number, - setSystemTime(now?: number): void, advanceTimersByTime(msToRun: number): void, runTimersToTime(msToRun: number): void, getTimerCount(): number,