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 Oct 30, 2018
1 parent 0279206 commit 7d72284
Show file tree
Hide file tree
Showing 7 changed files with 10 additions and 32 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
8 changes: 0 additions & 8 deletions docs/JestObjectAPI.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)`
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
3 changes: 0 additions & 3 deletions packages/jest-runtime/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
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 @@ -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,
};
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 7d72284

Please sign in to comment.