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 Mar 21, 2019
1 parent ee23815 commit 7c13de5
Show file tree
Hide file tree
Showing 4 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 @@ -553,14 +553,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
3 changes: 0 additions & 3 deletions packages/jest-environment/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,4 @@ export interface Jest {
* every test so that local module state doesn't conflict between tests.
*/
isolateModules(fn: () => void): Jest;

getRealSystemTime(): number;
setSystemTime(now?: number): void;
}
26 changes: 9 additions & 17 deletions packages/jest-fake-timers/src/jestFakeTimers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,16 +80,20 @@ export default class FakeTimers {
}

useFakeTimers() {
const toFake = Object.keys(this._lolex.timers) as Array<
keyof LolexWithContext['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 @@ -98,22 +102,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 packages/jest-runtime/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1028,7 +1028,6 @@ class Runtime {
fn,
genMockFromModule: (moduleName: string) =>
this._generateMock(from, moduleName),
getRealSystemTime: () => _getFakeTimers().getRealSystemTime(),
getTimerCount: () => _getFakeTimers().getTimerCount(),
isMockFunction: this._moduleMocker.isMockFunction,
isolateModules,
Expand All @@ -1047,7 +1046,6 @@ class Runtime {
_getFakeTimers().advanceTimersByTime(msToRun),
setMock: (moduleName: string, mock: unknown) =>
setMockFactory(moduleName, () => mock),
setSystemTime: (now?: number) => _getFakeTimers().setSystemTime(now),
setTimeout,
spyOn,
unmock,
Expand Down

0 comments on commit 7c13de5

Please sign in to comment.