From 4f563454483ae3af0b0c8b1b8ede4d857b4651be Mon Sep 17 00:00:00 2001 From: Simen Bekkhus Date: Thu, 25 Oct 2018 22:07:18 +0200 Subject: [PATCH] remove spying on fake timer functions --- .../__tests__/infinite_timer_game.test.js | 9 +- examples/timer/__tests__/timer_game.test.js | 11 ++- packages/jest-environment-jsdom/src/index.js | 1 - packages/jest-environment-node/src/index.js | 1 - .../src/__tests__/p_timeout.test.js | 5 ++ packages/jest-util/package.json | 3 - .../src/__tests__/fake_timers.test.js | 82 ++++++++----------- packages/jest-util/src/fake_timers.js | 46 ----------- 8 files changed, 49 insertions(+), 109 deletions(-) diff --git a/examples/timer/__tests__/infinite_timer_game.test.js b/examples/timer/__tests__/infinite_timer_game.test.js index e4e09d90fa54..d33d9b62eded 100644 --- a/examples/timer/__tests__/infinite_timer_game.test.js +++ b/examples/timer/__tests__/infinite_timer_game.test.js @@ -5,6 +5,7 @@ jest.useFakeTimers(); it('schedules a 10-second timer after 1 second', () => { + jest.spyOn(global, 'setTimeout'); const infiniteTimerGame = require('../infiniteTimerGame'); const callback = jest.fn(); @@ -12,8 +13,8 @@ it('schedules a 10-second timer after 1 second', () => { // 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) @@ -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); }); diff --git a/examples/timer/__tests__/timer_game.test.js b/examples/timer/__tests__/timer_game.test.js index 599d083c6fbc..c2f55ea24ecf 100644 --- a/examples/timer/__tests__/timer_game.test.js +++ b/examples/timer/__tests__/timer_game.test.js @@ -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', () => { @@ -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', () => { @@ -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); }); }); diff --git a/packages/jest-environment-jsdom/src/index.js b/packages/jest-environment-jsdom/src/index.js index 8b7b710dde77..4d4c966e5e16 100644 --- a/packages/jest-environment-jsdom/src/index.js +++ b/packages/jest-environment-jsdom/src/index.js @@ -75,7 +75,6 @@ class JSDOMEnvironment { this.fakeTimers = new FakeTimers({ config, global, - moduleMocker: this.moduleMocker, }); } diff --git a/packages/jest-environment-node/src/index.js b/packages/jest-environment-node/src/index.js index f5681357de16..aba9a5ce412e 100644 --- a/packages/jest-environment-node/src/index.js +++ b/packages/jest-environment-node/src/index.js @@ -51,7 +51,6 @@ class NodeEnvironment { this.fakeTimers = new FakeTimers({ config, global, - moduleMocker: this.moduleMocker, }); } diff --git a/packages/jest-jasmine2/src/__tests__/p_timeout.test.js b/packages/jest-jasmine2/src/__tests__/p_timeout.test.js index d0bccd1d237a..718c52cbc7b7 100644 --- a/packages/jest-jasmine2/src/__tests__/p_timeout.test.js +++ b/packages/jest-jasmine2/src/__tests__/p_timeout.test.js @@ -13,6 +13,11 @@ jest.useFakeTimers(); import pTimeout from '../p_timeout'; 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(); diff --git a/packages/jest-util/package.json b/packages/jest-util/package.json index fc4017aabcc7..822b43d54196 100644 --- a/packages/jest-util/package.json +++ b/packages/jest-util/package.json @@ -18,9 +18,6 @@ "slash": "^2.0.0", "source-map": "^0.6.0" }, - "devDependencies": { - "jest-mock": "^23.2.0" - }, "engines": { "node": ">= 6" } diff --git a/packages/jest-util/src/__tests__/fake_timers.test.js b/packages/jest-util/src/__tests__/fake_timers.test.js index 5c04af57faff..f36eb2ee7aa0 100644 --- a/packages/jest-util/src/__tests__/fake_timers.test.js +++ b/packages/jest-util/src/__tests__/fake_timers.test.js @@ -6,43 +6,34 @@ * */ -import vm from 'vm'; import FakeTimers from '../fake_timers'; -import mock from 'jest-mock'; describe('FakeTimers', () => { - let moduleMocker; - - beforeEach(() => { - const global = vm.runInNewContext('this'); - moduleMocker = new mock.ModuleMocker(global); - }); - describe('construction', () => { it('installs setTimeout mock', () => { const global = {Date, clearTimeout, process, setTimeout}; - const timers = new FakeTimers({global, moduleMocker}); + const timers = new FakeTimers({global}); timers.useFakeTimers(); expect(global.setTimeout).not.toBe(undefined); }); it('installs clearTimeout mock', () => { const global = {Date, clearTimeout, process, setTimeout}; - const timers = new FakeTimers({global, moduleMocker}); + const timers = new FakeTimers({global}); timers.useFakeTimers(); expect(global.clearTimeout).not.toBe(undefined); }); it('installs setInterval mock', () => { const global = {Date, clearTimeout, process, setTimeout}; - const timers = new FakeTimers({global, moduleMocker}); + const timers = new FakeTimers({global}); timers.useFakeTimers(); expect(global.setInterval).not.toBe(undefined); }); it('installs clearInterval mock', () => { const global = {Date, clearTimeout, process, setTimeout}; - const timers = new FakeTimers({global, moduleMocker}); + const timers = new FakeTimers({global}); timers.useFakeTimers(); expect(global.clearInterval).not.toBe(undefined); }); @@ -57,7 +48,7 @@ describe('FakeTimers', () => { }, setTimeout, }; - const timers = new FakeTimers({global, moduleMocker}); + const timers = new FakeTimers({global}); timers.useFakeTimers(); expect(global.process.nextTick).not.toBe(origNextTick); }); @@ -71,7 +62,7 @@ describe('FakeTimers', () => { setImmediate: origSetImmediate, setTimeout, }; - const timers = new FakeTimers({global, moduleMocker}); + const timers = new FakeTimers({global}); timers.useFakeTimers(); expect(global.setImmediate).not.toBe(origSetImmediate); }); @@ -87,7 +78,7 @@ describe('FakeTimers', () => { setImmediate: origSetImmediate, setTimeout, }; - const timers = new FakeTimers({global, moduleMocker}); + const timers = new FakeTimers({global}); timers.useFakeTimers(); expect(global.clearImmediate).not.toBe(origClearImmediate); }); @@ -104,7 +95,7 @@ describe('FakeTimers', () => { setTimeout, }; - const timers = new FakeTimers({global, moduleMocker}); + const timers = new FakeTimers({global}); timers.useFakeTimers(); const runOrder = []; @@ -135,7 +126,7 @@ describe('FakeTimers', () => { setTimeout, }; - const timers = new FakeTimers({global, moduleMocker}); + const timers = new FakeTimers({global}); timers.useFakeTimers(); timers.runAllTicks(); @@ -152,7 +143,7 @@ describe('FakeTimers', () => { setTimeout, }; - const timers = new FakeTimers({global, moduleMocker}); + const timers = new FakeTimers({global}); timers.useFakeTimers(); const mock1 = jest.fn(); @@ -176,11 +167,7 @@ describe('FakeTimers', () => { setTimeout, }; - const timers = new FakeTimers({ - global, - maxLoops: 100, - moduleMocker, - }); + const timers = new FakeTimers({global, maxLoops: 100}); timers.useFakeTimers(); @@ -199,7 +186,7 @@ describe('FakeTimers', () => { describe('runAllTimers', () => { it('runs all timers in order', () => { const global = {Date, clearTimeout, process, setTimeout}; - const timers = new FakeTimers({global, moduleMocker}); + const timers = new FakeTimers({global}); timers.useFakeTimers(); const runOrder = []; @@ -239,7 +226,6 @@ describe('FakeTimers', () => { rootDir: __dirname, }, global, - moduleMocker, }); timers.runAllTimers(); expect( @@ -257,14 +243,14 @@ describe('FakeTimers', () => { setTimeout: nativeSetTimeout, }; - const timers = new FakeTimers({global, moduleMocker}); + const timers = new FakeTimers({global}); timers.useFakeTimers(); timers.runAllTimers(); }); it('only runs a setTimeout callback once (ever)', () => { const global = {Date, clearTimeout, process, setTimeout}; - const timers = new FakeTimers({global, moduleMocker}); + const timers = new FakeTimers({global}); timers.useFakeTimers(); const fn = jest.fn(); @@ -280,7 +266,7 @@ describe('FakeTimers', () => { it('runs callbacks with arguments after the interval', () => { const global = {Date, clearTimeout, process, setTimeout}; - const timers = new FakeTimers({global, moduleMocker}); + const timers = new FakeTimers({global}); timers.useFakeTimers(); const fn = jest.fn(); @@ -301,7 +287,7 @@ describe('FakeTimers', () => { setTimeout: nativeSetTimeout, }; - const timers = new FakeTimers({global, moduleMocker}); + const timers = new FakeTimers({global}); // Lolex uses `setTimeout` during init to figure out if it's in Node or // browser env. So clear its calls before we install them into the env nativeSetTimeout.mockClear(); @@ -317,11 +303,7 @@ describe('FakeTimers', () => { it('throws before allowing infinite recursion', () => { const global = {Date, clearTimeout, process, setTimeout}; - const timers = new FakeTimers({ - global, - maxLoops: 100, - moduleMocker, - }); + const timers = new FakeTimers({global, maxLoops: 100}); timers.useFakeTimers(); global.setTimeout(function infinitelyRecursingCallback() { @@ -339,7 +321,7 @@ describe('FakeTimers', () => { it('also clears ticks', () => { const global = {Date, clearTimeout, process, setTimeout}; - const timers = new FakeTimers({global, moduleMocker}); + const timers = new FakeTimers({global}); timers.useFakeTimers(); const fn = jest.fn(); @@ -356,7 +338,7 @@ describe('FakeTimers', () => { describe('advanceTimersByTime', () => { it('runs timers in order', () => { const global = {Date, clearTimeout, process, setTimeout}; - const timers = new FakeTimers({global, moduleMocker}); + const timers = new FakeTimers({global}); timers.useFakeTimers(); const runOrder = []; @@ -395,7 +377,7 @@ describe('FakeTimers', () => { it('does nothing when no timers have been scheduled', () => { const global = {Date, clearTimeout, process, setTimeout}; - const timers = new FakeTimers({global, moduleMocker}); + const timers = new FakeTimers({global}); timers.useFakeTimers(); timers.advanceTimersByTime(100); @@ -405,7 +387,7 @@ describe('FakeTimers', () => { describe('reset', () => { it('resets all pending setTimeouts', () => { const global = {Date, clearTimeout, process, setTimeout}; - const timers = new FakeTimers({global, moduleMocker}); + const timers = new FakeTimers({global}); timers.useFakeTimers(); const mock1 = jest.fn(); @@ -418,7 +400,7 @@ describe('FakeTimers', () => { it('resets all pending setIntervals', () => { const global = {Date, clearTimeout, process, setTimeout}; - const timers = new FakeTimers({global, moduleMocker}); + const timers = new FakeTimers({global}); timers.useFakeTimers(); const mock1 = jest.fn(); @@ -439,7 +421,7 @@ describe('FakeTimers', () => { setImmediate: () => {}, setTimeout, }; - const timers = new FakeTimers({global, moduleMocker}); + const timers = new FakeTimers({global}); timers.useFakeTimers(); const mock1 = jest.fn(); @@ -453,7 +435,7 @@ describe('FakeTimers', () => { it('resets current advanceTimersByTime time cursor', () => { const global = {Date, clearTimeout, process, setTimeout}; - const timers = new FakeTimers({global, moduleMocker}); + const timers = new FakeTimers({global}); timers.useFakeTimers(); const mock1 = jest.fn(); @@ -480,7 +462,7 @@ describe('FakeTimers', () => { setTimeout, }; - const timers = new FakeTimers({global, moduleMocker}); + const timers = new FakeTimers({global}); timers.useFakeTimers(); const runOrder = []; @@ -547,7 +529,7 @@ describe('FakeTimers', () => { it('does not run timers that were cleared in another timer', () => { const global = {Date, clearTimeout, process, setTimeout}; - const timers = new FakeTimers({global, moduleMocker}); + const timers = new FakeTimers({global}); timers.useFakeTimers(); const fn = jest.fn(); @@ -576,7 +558,7 @@ describe('FakeTimers', () => { setInterval: nativeSetInterval, setTimeout: nativeSetTimeout, }; - const timers = new FakeTimers({global, moduleMocker}); + const timers = new FakeTimers({global}); timers.useFakeTimers(); // Ensure that timers has overridden the native timer APIs @@ -603,7 +585,7 @@ describe('FakeTimers', () => { process: {nextTick: nativeProcessNextTick}, setTimeout, }; - const timers = new FakeTimers({global, moduleMocker}); + const timers = new FakeTimers({global}); timers.useFakeTimers(); // Ensure that timers has overridden the native timer APIs @@ -627,7 +609,7 @@ describe('FakeTimers', () => { setImmediate: nativeSetImmediate, setTimeout, }; - const timers = new FakeTimers({global, moduleMocker}); + const timers = new FakeTimers({global}); timers.useFakeTimers(); // Ensure that timers has overridden the native timer APIs @@ -657,7 +639,7 @@ describe('FakeTimers', () => { setInterval: nativeSetInterval, setTimeout: nativeSetTimeout, }; - const timers = new FakeTimers({global, moduleMocker}); + const timers = new FakeTimers({global}); timers.useRealTimers(); // Ensure that the real timers are installed at this point @@ -684,7 +666,7 @@ describe('FakeTimers', () => { process: {nextTick: nativeProcessNextTick}, setTimeout, }; - const timers = new FakeTimers({global, moduleMocker}); + const timers = new FakeTimers({global}); timers.useRealTimers(); // Ensure that the real timers are installed at this point @@ -708,7 +690,7 @@ describe('FakeTimers', () => { setImmediate: nativeSetImmediate, setTimeout, }; - const fakeTimers = new FakeTimers({global, moduleMocker}); + const fakeTimers = new FakeTimers({global}); fakeTimers.useRealTimers(); // Ensure that the real timers are installed at this point diff --git a/packages/jest-util/src/fake_timers.js b/packages/jest-util/src/fake_timers.js index f0ddc1b155b7..d3209e5623e3 100644 --- a/packages/jest-util/src/fake_timers.js +++ b/packages/jest-util/src/fake_timers.js @@ -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'; @@ -23,28 +21,22 @@ export default class FakeTimers { _global: Global; _lolex: lolex; _maxLoops: number; - _moduleMocker: ModuleMocker; - _timerMocks: Array; 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() { @@ -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 = []; } } @@ -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() {