Skip to content

Commit

Permalink
only mock timer functions currently mocked by Jest
Browse files Browse the repository at this point in the history
  • Loading branch information
SimenB committed Feb 6, 2019
1 parent a9bcc97 commit 134d091
Show file tree
Hide file tree
Showing 6 changed files with 9 additions and 30 deletions.
8 changes: 0 additions & 8 deletions docs/JestObjectAPI.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)`
Expand Down
1 change: 0 additions & 1 deletion flow-typed/npm/lolex_v2.x.x.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 0 additions & 2 deletions packages/jest-runtime/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
Expand Down
24 changes: 9 additions & 15 deletions packages/jest-util/src/FakeTimers.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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();
Expand Down
2 changes: 0 additions & 2 deletions types/Environment.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
};
Expand Down
2 changes: 0 additions & 2 deletions types/Jest.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit 134d091

Please sign in to comment.