Skip to content

Commit

Permalink
fix(jest-util): guard fake timer operations behind check for faked env
Browse files Browse the repository at this point in the history
  • Loading branch information
SimenB committed Dec 25, 2017
1 parent df13be2 commit b6eaa66
Showing 1 changed file with 33 additions and 33 deletions.
66 changes: 33 additions & 33 deletions packages/jest-util/src/fake_timers.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,21 +48,21 @@ export default class FakeTimers {
}

runAllTimers() {
this._checkFakeTimers();

this._clock.runAll();
if (this._checkFakeTimers()) {
this._clock.runAll();
}
}

runOnlyPendingTimers() {
this._checkFakeTimers();

this._clock.runToLast();
if (this._checkFakeTimers()) {
this._clock.runToLast();
}
}

advanceTimersByTime(msToRun: number) {
this._checkFakeTimers();

this._clock.tick(msToRun);
if (this._checkFakeTimers()) {
this._clock.tick(msToRun);
}
}

useRealTimers() {
Expand All @@ -73,31 +73,29 @@ export default class FakeTimers {
}

useFakeTimers() {
if (this._fakingTime) {
return;
if (!this._fakingTime) {
// This creates stubs based on the host environment, we want it to be based
// on `this._global`. See https://github.com/sinonjs/lolex/issues/146
this._clock = lolex.install({
loopLimit: this._maxLoops,
target: this._global,
// TODO: would be great to be able to say `true` or `'all'`
toFake: [
'setTimeout',
'clearTimeout',
'setImmediate',
'clearImmediate',
'setInterval',
'clearInterval',
'Date',
'nextTick',
'hrtime',
'requestAnimationFrame',
'cancelAnimationFrame',
],
});
this._fakingTime = true;
}

// This creates stubs based on the host environment, we want it to be based
// on `this._global`. See https://github.com/sinonjs/lolex/issues/146
this._clock = lolex.install({
loopLimit: this._maxLoops,
target: this._global,
// TODO: would be great to be able to say `true` or `'all'`
toFake: [
'setTimeout',
'clearTimeout',
'setImmediate',
'clearImmediate',
'setInterval',
'clearInterval',
'Date',
'nextTick',
'hrtime',
'requestAnimationFrame',
'cancelAnimationFrame',
],
});
this._fakingTime = true;
}

_checkFakeTimers() {
Expand All @@ -112,5 +110,7 @@ export default class FakeTimers {
}),
);
}

return this._fakingTime;
}
}

0 comments on commit b6eaa66

Please sign in to comment.