Skip to content

Commit

Permalink
remove spying on fake timer functions
Browse files Browse the repository at this point in the history
  • Loading branch information
SimenB committed Oct 30, 2018
1 parent 11d8f5a commit ef2379c
Show file tree
Hide file tree
Showing 8 changed files with 49 additions and 109 deletions.
9 changes: 5 additions & 4 deletions examples/timer/__tests__/infinite_timer_game.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,16 @@
jest.useFakeTimers();

it('schedules a 10-second timer after 1 second', () => {
jest.spyOn(global, 'setTimeout');
const infiniteTimerGame = require('../infiniteTimerGame');
const callback = jest.fn();

infiniteTimerGame(callback);

// At this point in time, there should have been a single call to
// setTimeout to schedule the end of the game in 1 second.
expect(setTimeout.mock.calls.length).toBe(1);
expect(setTimeout.mock.calls[0][1]).toBe(1000);
expect(setTimeout).toBeCalledTimes(1);
expect(setTimeout).toHaveBeenNthCalledWith(1, expect.any(Function), 1000);

// Fast forward and exhaust only currently pending timers
// (but not any new timers that get created during that process)
Expand All @@ -24,6 +25,6 @@ it('schedules a 10-second timer after 1 second', () => {

// And it should have created a new timer to start the game over in
// 10 seconds
expect(setTimeout.mock.calls.length).toBe(2);
expect(setTimeout.mock.calls[1][1]).toBe(10000);
expect(setTimeout).toBeCalledTimes(2);
expect(setTimeout).toHaveBeenNthCalledWith(2, expect.any(Function), 10000);
});
11 changes: 7 additions & 4 deletions examples/timer/__tests__/timer_game.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,15 @@
jest.useFakeTimers();

describe('timerGame', () => {
beforeEach(() => {
jest.spyOn(global, 'setTimeout');
});
it('waits 1 second before ending the game', () => {
const timerGame = require('../timerGame');
timerGame();

expect(setTimeout.mock.calls.length).toBe(1);
expect(setTimeout.mock.calls[0][1]).toBe(1000);
expect(setTimeout).toBeCalledTimes(1);
expect(setTimeout).toBeCalledWith(expect.any(Function), 1000);
});

it('calls the callback after 1 second via runAllTimers', () => {
Expand All @@ -27,7 +30,7 @@ describe('timerGame', () => {

// Now our callback should have been called!
expect(callback).toBeCalled();
expect(callback.mock.calls.length).toBe(1);
expect(callback).toBeCalledTimes(1);
});

it('calls the callback after 1 second via advanceTimersByTime', () => {
Expand All @@ -44,6 +47,6 @@ describe('timerGame', () => {

// Now our callback should have been called!
expect(callback).toBeCalled();
expect(callback.mock.calls.length).toBe(1);
expect(callback).toBeCalledTimes(1);
});
});
1 change: 0 additions & 1 deletion packages/jest-environment-jsdom/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,6 @@ class JSDOMEnvironment {
this.fakeTimers = new FakeTimers({
config,
global,
moduleMocker: this.moduleMocker,
});
}

Expand Down
1 change: 0 additions & 1 deletion packages/jest-environment-node/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ class NodeEnvironment {
this.fakeTimers = new FakeTimers({
config,
global,
moduleMocker: this.moduleMocker,
});
}

Expand Down
5 changes: 5 additions & 0 deletions packages/jest-jasmine2/src/__tests__/pTimeout.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@ jest.useFakeTimers();
import pTimeout from '../pTimeout';

describe('pTimeout', () => {
beforeEach(() => {
jest.spyOn(global, 'setTimeout');
jest.spyOn(global, 'clearTimeout');
});

it('calls `clearTimeout` and resolves when `promise` resolves.', async () => {
const onTimeout = jest.fn();
const promise = Promise.resolve();
Expand Down
3 changes: 0 additions & 3 deletions packages/jest-util/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,6 @@
"slash": "^2.0.0",
"source-map": "^0.6.0"
},
"devDependencies": {
"jest-mock": "^23.2.0"
},
"engines": {
"node": ">= 6"
}
Expand Down
46 changes: 0 additions & 46 deletions packages/jest-util/src/FakeTimers.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@

import type {ProjectConfig} from 'types/Config';
import type {Global} from 'types/Global';
import type {ModuleMocker} from 'types/Mock';
import type {JestMockFn} from 'types/Jest';
import type {lolex, Clock} from 'lolex';

import {withGlobal as lolexWithGlobal} from 'lolex';
Expand All @@ -23,28 +21,22 @@ export default class FakeTimers {
_global: Global;
_lolex: lolex;
_maxLoops: number;
_moduleMocker: ModuleMocker;
_timerMocks: Array<JestMockFn>;

constructor({
global,
moduleMocker,
config,
maxLoops,
}: {
global: Global,
moduleMocker: ModuleMocker,
config: ProjectConfig,
maxLoops?: number,
}) {
this._global = global;
this._config = config;
this._maxLoops = maxLoops || 100000;
this._moduleMocker = moduleMocker;

this._fakingTime = false;
this._lolex = lolexWithGlobal(global);
this._timerMocks = [];
}

clearAllTimers() {
Expand Down Expand Up @@ -83,12 +75,8 @@ export default class FakeTimers {

useRealTimers() {
if (this._fakingTime) {
this._timerMocks.forEach(func => {
func.mockRestore();
});
this._clock.uninstall();
this._fakingTime = false;
this._timerMocks = [];
}
}

Expand All @@ -105,40 +93,6 @@ export default class FakeTimers {

this._fakingTime = true;
}

let nowSpy;

this._timerMocks = Object.keys(this._clock)
.filter(func => func === 'performance' || toFake.includes(func))
.map(func => {
if (func === 'hrtime' || func === 'nextTick') {
return this._moduleMocker
.spyOn(this._global.process, func)
.mockName(`process.${func}`);
}

if (func === 'performance') {
return this._moduleMocker
.spyOn(this._global.performance, 'now')
.mockName('performance.now');
}

if (func === 'Date') {
nowSpy = this._moduleMocker
.spyOn(this._global.Date, 'now')
.mockName('Date.now');
}

const spy = this._moduleMocker.spyOn(this._global, func).mockName(func);

spy.now = nowSpy;

return spy;
});

if (nowSpy) {
this._timerMocks.unshift(nowSpy);
}
}

reset() {
Expand Down
Loading

0 comments on commit ef2379c

Please sign in to comment.