From 134d091a2342723d352ea8f1f1634271554bfc78 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 --- docs/JestObjectAPI.md | 8 -------- flow-typed/npm/lolex_v2.x.x.js | 1 - packages/jest-runtime/src/index.js | 2 -- packages/jest-util/src/FakeTimers.js | 24 +++++++++--------------- types/Environment.js | 2 -- types/Jest.js | 2 -- 6 files changed, 9 insertions(+), 30 deletions(-) diff --git a/docs/JestObjectAPI.md b/docs/JestObjectAPI.md index 39433102f8e3..6c84635e2ef9 100644 --- a/docs/JestObjectAPI.md +++ b/docs/JestObjectAPI.md @@ -460,14 +460,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 1ed791b7c0ce..00217450282e 100644 --- a/packages/jest-runtime/src/index.js +++ b/packages/jest-runtime/src/index.js @@ -1002,7 +1002,6 @@ class Runtime { fn, genMockFromModule: (moduleName: string) => this._generateMock(from, moduleName), - getRealSystemTime: () => _getFakeTimers().getRealSystemTime(), getTimerCount: () => _getFakeTimers().getTimerCount(), isMockFunction: this._moduleMocker.isMockFunction, isolateModules, @@ -1021,7 +1020,6 @@ class Runtime { _getFakeTimers().advanceTimersByTime(msToRun), setMock: (moduleName: string, mock: Object) => setMockFactory(moduleName, () => mock), - setSystemTime: (now?: number) => _getFakeTimers().setSystemTime(now), setTimeout, spyOn, unmock, diff --git a/packages/jest-util/src/FakeTimers.js b/packages/jest-util/src/FakeTimers.js index e18ebc30599f..4344cb94eb99 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 b5769380c71a..cb638f294da1 100644 --- a/types/Environment.js +++ b/types/Environment.js @@ -29,8 +29,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 24846e54ad2e..0482017b8318 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,