From 4bfff52260dc3e13344f09f2965b56a3a2b2f54f Mon Sep 17 00:00:00 2001 From: Jelle Versele Date: Sat, 7 Oct 2017 11:30:03 +0200 Subject: [PATCH 1/4] Implement node Timer api when running in node environment. --- .../src/__tests__/jsdom_environment.test.js | 24 ++++ packages/jest-environment-jsdom/src/index.js | 15 ++- .../src/__tests__/node_environment.test.js | 15 +++ packages/jest-environment-node/src/index.js | 33 +++++- .../src/__tests__/fake_timers.test.js | 112 +++++++++++------- packages/jest-util/src/fake_timers.js | 20 +++- 6 files changed, 167 insertions(+), 52 deletions(-) create mode 100644 packages/jest-environment-jsdom/src/__tests__/jsdom_environment.test.js diff --git a/packages/jest-environment-jsdom/src/__tests__/jsdom_environment.test.js b/packages/jest-environment-jsdom/src/__tests__/jsdom_environment.test.js new file mode 100644 index 000000000000..92770a38007d --- /dev/null +++ b/packages/jest-environment-jsdom/src/__tests__/jsdom_environment.test.js @@ -0,0 +1,24 @@ +/** + * Copyright (c) 2014-present, Facebook, Inc. All rights reserved. + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + */ +'use strict'; + +const JSDomEnvironment = require.requireActual('../'); + +describe('JSDomEnvironment', () => { + it('should configure setTimeout/setInterval to use the browser api', () => { + const env1 = new JSDomEnvironment({}); + + env1.fakeTimers.useFakeTimers(); + + const timer1 = env1.global.setTimeout(() => {}, 0); + const timer2 = env1.global.setInterval(() => {}, 0); + + [timer1, timer2].forEach(timer => { + expect(typeof timer === 'number').toBe(true); + }); + }); +}); diff --git a/packages/jest-environment-jsdom/src/index.js b/packages/jest-environment-jsdom/src/index.js index f03b6543c92b..a14b1ea7cbcf 100644 --- a/packages/jest-environment-jsdom/src/index.js +++ b/packages/jest-environment-jsdom/src/index.js @@ -17,7 +17,7 @@ import JSDom from 'jsdom'; class JSDOMEnvironment { document: ?Object; - fakeTimers: ?FakeTimers; + fakeTimers: ?FakeTimers; global: ?Global; moduleMocker: ?ModuleMocker; @@ -44,7 +44,18 @@ class JSDOMEnvironment { } this.moduleMocker = new mock.ModuleMocker(global); - this.fakeTimers = new FakeTimers(global, this.moduleMocker, config); + + const timerConfig = { + idToRef: (id: number) => id, + refToId: (ref: number) => ref, + }; + + this.fakeTimers = new FakeTimers( + global, + this.moduleMocker, + timerConfig, + config, + ); } dispose(): void { diff --git a/packages/jest-environment-node/src/__tests__/node_environment.test.js b/packages/jest-environment-node/src/__tests__/node_environment.test.js index afbd75187c85..20ee17f9681a 100644 --- a/packages/jest-environment-node/src/__tests__/node_environment.test.js +++ b/packages/jest-environment-node/src/__tests__/node_environment.test.js @@ -27,4 +27,19 @@ describe('NodeEnvironment', () => { expect(env1.global.global).toBe(env1.global); }); + + it('should configure setTimeout/setInterval to use the node api', () => { + const env1 = new NodeEnvironment({}); + + env1.fakeTimers.useFakeTimers(); + + const timer1 = env1.global.setTimeout(() => {}, 0); + const timer2 = env1.global.setInterval(() => {}, 0); + + [timer1, timer2].forEach(timer => { + expect(timer.id).not.toBe(undefined); + expect(timer.ref).not.toBe(undefined); + expect(timer.unref).not.toBe(undefined); + }); + }); }); diff --git a/packages/jest-environment-node/src/index.js b/packages/jest-environment-node/src/index.js index c8655937335b..73c2c939e632 100644 --- a/packages/jest-environment-node/src/index.js +++ b/packages/jest-environment-node/src/index.js @@ -16,9 +16,15 @@ import vm from 'vm'; import {FakeTimers, installCommonGlobals} from 'jest-util'; import mock from 'jest-mock'; +type Timer = {| + id: number, + ref: () => Timer, + unref: () => Timer, +|}; + class NodeEnvironment { context: ?vm$Context; - fakeTimers: ?FakeTimers; + fakeTimers: ?FakeTimers; global: ?Global; moduleMocker: ?ModuleMocker; @@ -33,7 +39,30 @@ class NodeEnvironment { global.setTimeout = setTimeout; installCommonGlobals(global, config.globals); this.moduleMocker = new mock.ModuleMocker(global); - this.fakeTimers = new FakeTimers(global, this.moduleMocker, config); + + const timerIdToRef = (id: number) => ({ + id, + ref() { + return this; + }, + unref() { + return this; + }, + }); + + const timerRefToId = (timer: Timer) => timer.id; + + const timerConfig = { + idToRef: timerIdToRef, + refToId: timerRefToId, + }; + + this.fakeTimers = new FakeTimers( + global, + this.moduleMocker, + timerConfig, + config, + ); } dispose() { diff --git a/packages/jest-util/src/__tests__/fake_timers.test.js b/packages/jest-util/src/__tests__/fake_timers.test.js index ff2a2afc9596..63f3fec44c73 100644 --- a/packages/jest-util/src/__tests__/fake_timers.test.js +++ b/packages/jest-util/src/__tests__/fake_timers.test.js @@ -11,41 +11,46 @@ const vm = require('vm'); describe('FakeTimers', () => { - let FakeTimers, moduleMocker; + let FakeTimers, moduleMocker, timerConfig; beforeEach(() => { FakeTimers = require('../fake_timers').default; const mock = require('jest-mock'); const global = vm.runInNewContext('this'); moduleMocker = new mock.ModuleMocker(global); + + timerConfig = { + idToRef: (id: number) => id, + refToId: (ref: number) => ref, + }; }); describe('construction', () => { /* eslint-disable no-new */ it('installs setTimeout mock', () => { const global = {process}; - const timers = new FakeTimers(global, moduleMocker); + const timers = new FakeTimers(global, moduleMocker, timerConfig); timers.useFakeTimers(); expect(global.setTimeout).not.toBe(undefined); }); it('installs clearTimeout mock', () => { const global = {process}; - const timers = new FakeTimers(global, moduleMocker); + const timers = new FakeTimers(global, moduleMocker, timerConfig); timers.useFakeTimers(); expect(global.clearTimeout).not.toBe(undefined); }); it('installs setInterval mock', () => { const global = {process}; - const timers = new FakeTimers(global, moduleMocker); + const timers = new FakeTimers(global, moduleMocker, timerConfig); timers.useFakeTimers(); expect(global.setInterval).not.toBe(undefined); }); it('installs clearInterval mock', () => { const global = {process}; - const timers = new FakeTimers(global, moduleMocker); + const timers = new FakeTimers(global, moduleMocker, timerConfig); timers.useFakeTimers(); expect(global.clearInterval).not.toBe(undefined); }); @@ -57,7 +62,7 @@ describe('FakeTimers', () => { nextTick: origNextTick, }, }; - const timers = new FakeTimers(global, moduleMocker); + const timers = new FakeTimers(global, moduleMocker, timerConfig); timers.useFakeTimers(); expect(global.process.nextTick).not.toBe(origNextTick); }); @@ -68,7 +73,7 @@ describe('FakeTimers', () => { process, setImmediate: origSetImmediate, }; - const timers = new FakeTimers(global, moduleMocker); + const timers = new FakeTimers(global, moduleMocker, timerConfig); timers.useFakeTimers(); expect(global.setImmediate).not.toBe(origSetImmediate); }); @@ -81,7 +86,7 @@ describe('FakeTimers', () => { process, setImmediate: origSetImmediate, }; - const timers = new FakeTimers(global, moduleMocker); + const timers = new FakeTimers(global, moduleMocker, timerConfig); timers.useFakeTimers(); expect(global.clearImmediate).not.toBe(origClearImmediate); }); @@ -95,7 +100,7 @@ describe('FakeTimers', () => { }, }; - const timers = new FakeTimers(global, moduleMocker); + const timers = new FakeTimers(global, moduleMocker, timerConfig); timers.useFakeTimers(); const runOrder = []; @@ -123,7 +128,7 @@ describe('FakeTimers', () => { }, }; - const timers = new FakeTimers(global, moduleMocker); + const timers = new FakeTimers(global, moduleMocker, timerConfig); timers.useFakeTimers(); timers.runAllTicks(); @@ -137,7 +142,7 @@ describe('FakeTimers', () => { }, }; - const timers = new FakeTimers(global, moduleMocker); + const timers = new FakeTimers(global, moduleMocker, timerConfig); timers.useFakeTimers(); const mock1 = jest.genMockFn(); @@ -160,7 +165,7 @@ describe('FakeTimers', () => { }, }; - const timers = new FakeTimers(global, moduleMocker); + const timers = new FakeTimers(global, moduleMocker, timerConfig); timers.useFakeTimers(); const mock1 = jest.genMockFn(); @@ -183,7 +188,7 @@ describe('FakeTimers', () => { setImmediate: nativeSetImmediate, }; - const timers = new FakeTimers(global, moduleMocker); + const timers = new FakeTimers(global, moduleMocker, timerConfig); timers.useFakeTimers(); const mock1 = jest.genMockFn(); @@ -206,7 +211,7 @@ describe('FakeTimers', () => { }, }; - const timers = new FakeTimers(global, moduleMocker); + const timers = new FakeTimers(global, moduleMocker, timerConfig); timers.useFakeTimers(); const mock1 = jest.genMockFn(); @@ -229,7 +234,7 @@ describe('FakeTimers', () => { setImmediate: nativeSetImmediate, }; - const timers = new FakeTimers(global, moduleMocker); + const timers = new FakeTimers(global, moduleMocker, timerConfig); timers.useFakeTimers(); const mock1 = jest.genMockFn(); @@ -252,7 +257,7 @@ describe('FakeTimers', () => { setImmediate: nativeSetImmediate, }; - const timers = new FakeTimers(global, moduleMocker); + const timers = new FakeTimers(global, moduleMocker, timerConfig); timers.useFakeTimers(); const mock1 = jest.genMockFn(); @@ -275,7 +280,14 @@ describe('FakeTimers', () => { }, }; - const timers = new FakeTimers(global, moduleMocker, null, 100); + const timers = new FakeTimers( + global, + moduleMocker, + timerConfig, + null, + 100, + ); + timers.useFakeTimers(); global.process.nextTick(function infinitelyRecursingCallback() { @@ -296,7 +308,7 @@ describe('FakeTimers', () => { describe('runAllTimers', () => { it('runs all timers in order', () => { const global = {process}; - const timers = new FakeTimers(global, moduleMocker); + const timers = new FakeTimers(global, moduleMocker, timerConfig); timers.useFakeTimers(); const runOrder = []; @@ -320,7 +332,9 @@ describe('FakeTimers', () => { it('warns when trying to advance timers while real timers are used', () => { const consoleWarn = console.warn; console.warn = jest.fn(); - const timers = new FakeTimers(global, moduleMocker, {rootDir: __dirname}); + const timers = new FakeTimers(global, moduleMocker, timerConfig, { + rootDir: __dirname, + }); timers.runAllTimers(); expect( console.warn.mock.calls[0][0].split('\nStack Trace')[0], @@ -335,14 +349,14 @@ describe('FakeTimers', () => { setTimeout: nativeSetTimeout, }; - const timers = new FakeTimers(global, moduleMocker); + const timers = new FakeTimers(global, moduleMocker, timerConfig); timers.useFakeTimers(); timers.runAllTimers(); }); it('only runs a setTimeout callback once (ever)', () => { const global = {process}; - const timers = new FakeTimers(global, moduleMocker); + const timers = new FakeTimers(global, moduleMocker, timerConfig); timers.useFakeTimers(); const fn = jest.genMockFn(); @@ -358,7 +372,7 @@ describe('FakeTimers', () => { it('runs callbacks with arguments after the interval', () => { const global = {process}; - const timers = new FakeTimers(global, moduleMocker); + const timers = new FakeTimers(global, moduleMocker, timerConfig); timers.useFakeTimers(); const fn = jest.genMockFn(); @@ -376,7 +390,7 @@ describe('FakeTimers', () => { setTimeout: nativeSetTimeout, }; - const timers = new FakeTimers(global, moduleMocker); + const timers = new FakeTimers(global, moduleMocker, timerConfig); timers.useFakeTimers(); const mock1 = jest.genMockFn(); @@ -389,7 +403,13 @@ describe('FakeTimers', () => { it('throws before allowing infinite recursion', () => { const global = {process}; - const timers = new FakeTimers(global, moduleMocker, null, 100); + const timers = new FakeTimers( + global, + moduleMocker, + timerConfig, + null, + 100, + ); timers.useFakeTimers(); global.setTimeout(function infinitelyRecursingCallback() { @@ -408,7 +428,7 @@ describe('FakeTimers', () => { it('also clears ticks', () => { const global = {process}; - const timers = new FakeTimers(global, moduleMocker); + const timers = new FakeTimers(global, moduleMocker, timerConfig); timers.useFakeTimers(); const fn = jest.genMockFn(); @@ -425,7 +445,7 @@ describe('FakeTimers', () => { describe('runTimersToTime', () => { it('runs timers in order', () => { const global = {process}; - const timers = new FakeTimers(global, moduleMocker); + const timers = new FakeTimers(global, moduleMocker, timerConfig); timers.useFakeTimers(); const runOrder = []; @@ -464,7 +484,7 @@ describe('FakeTimers', () => { it('does nothing when no timers have been scheduled', () => { const global = {process}; - const timers = new FakeTimers(global, moduleMocker); + const timers = new FakeTimers(global, moduleMocker, timerConfig); timers.useFakeTimers(); timers.runTimersToTime(100); @@ -472,7 +492,13 @@ describe('FakeTimers', () => { it('throws before allowing infinite recursion', () => { const global = {process}; - const timers = new FakeTimers(global, moduleMocker, null, 100); + const timers = new FakeTimers( + global, + moduleMocker, + timerConfig, + null, + 100, + ); timers.useFakeTimers(); global.setTimeout(function infinitelyRecursingCallback() { @@ -493,7 +519,7 @@ describe('FakeTimers', () => { describe('reset', () => { it('resets all pending setTimeouts', () => { const global = {process}; - const timers = new FakeTimers(global, moduleMocker); + const timers = new FakeTimers(global, moduleMocker, timerConfig); timers.useFakeTimers(); const mock1 = jest.genMockFn(); @@ -506,7 +532,7 @@ describe('FakeTimers', () => { it('resets all pending setIntervals', () => { const global = {process}; - const timers = new FakeTimers(global, moduleMocker); + const timers = new FakeTimers(global, moduleMocker, timerConfig); timers.useFakeTimers(); const mock1 = jest.genMockFn(); @@ -524,7 +550,7 @@ describe('FakeTimers', () => { }, setImmediate: () => {}, }; - const timers = new FakeTimers(global, moduleMocker); + const timers = new FakeTimers(global, moduleMocker, timerConfig); timers.useFakeTimers(); const mock1 = jest.genMockFn(); @@ -539,7 +565,7 @@ describe('FakeTimers', () => { it('resets current runTimersToTime time cursor', () => { const global = {process}; - const timers = new FakeTimers(global, moduleMocker); + const timers = new FakeTimers(global, moduleMocker, timerConfig); timers.useFakeTimers(); const mock1 = jest.genMockFn(); @@ -563,7 +589,7 @@ describe('FakeTimers', () => { setImmediate: nativeSetImmediate, }; - const timers = new FakeTimers(global, moduleMocker); + const timers = new FakeTimers(global, moduleMocker, timerConfig); timers.useFakeTimers(); const runOrder = []; @@ -611,7 +637,7 @@ describe('FakeTimers', () => { it('does not run timers that were cleared in another timer', () => { const global = {process}; - const timers = new FakeTimers(global, moduleMocker); + const timers = new FakeTimers(global, moduleMocker, timerConfig); timers.useFakeTimers(); const fn = jest.genMockFn(); @@ -639,7 +665,7 @@ describe('FakeTimers', () => { setInterval: nativeSetInterval, setTimeout: nativeSetTimeout, }; - const timers = new FakeTimers(global, moduleMocker); + const timers = new FakeTimers(global, moduleMocker, timerConfig); timers.useFakeTimers(); // clearInterval() @@ -684,7 +710,7 @@ describe('FakeTimers', () => { setInterval: nativeSetInterval, setTimeout: nativeSetTimeout, }; - const timers = new FakeTimers(global, moduleMocker); + const timers = new FakeTimers(global, moduleMocker, timerConfig); timers.useFakeTimers(); // clearInterval() @@ -738,7 +764,7 @@ describe('FakeTimers', () => { process, setTimeout: nativeSetTimeout, }; - const timers = new FakeTimers(global, moduleMocker); + const timers = new FakeTimers(global, moduleMocker, timerConfig); timers.useFakeTimers(); expect(() => { @@ -770,7 +796,7 @@ describe('FakeTimers', () => { setInterval: nativeSetInterval, setTimeout: nativeSetTimeout, }; - const timers = new FakeTimers(global, moduleMocker); + const timers = new FakeTimers(global, moduleMocker, timerConfig); timers.useFakeTimers(); // Ensure that timers has overridden the native timer APIs @@ -794,7 +820,7 @@ describe('FakeTimers', () => { const global = { process: {nextTick: nativeProcessNextTick}, }; - const timers = new FakeTimers(global, moduleMocker); + const timers = new FakeTimers(global, moduleMocker, timerConfig); timers.useFakeTimers(); // Ensure that timers has overridden the native timer APIs @@ -815,7 +841,7 @@ describe('FakeTimers', () => { process, setImmediate: nativeSetImmediate, }; - const timers = new FakeTimers(global, moduleMocker); + const timers = new FakeTimers(global, moduleMocker, timerConfig); timers.useFakeTimers(); // Ensure that timers has overridden the native timer APIs @@ -844,7 +870,7 @@ describe('FakeTimers', () => { setInterval: nativeSetInterval, setTimeout: nativeSetTimeout, }; - const timers = new FakeTimers(global, moduleMocker); + const timers = new FakeTimers(global, moduleMocker, timerConfig); timers.useRealTimers(); // Ensure that the real timers are installed at this point @@ -868,7 +894,7 @@ describe('FakeTimers', () => { const global = { process: {nextTick: nativeProcessNextTick}, }; - const timers = new FakeTimers(global, moduleMocker); + const timers = new FakeTimers(global, moduleMocker, timerConfig); timers.useRealTimers(); // Ensure that the real timers are installed at this point @@ -889,7 +915,7 @@ describe('FakeTimers', () => { process, setImmediate: nativeSetImmediate, }; - const fakeTimers = new FakeTimers(global, moduleMocker); + const fakeTimers = new FakeTimers(global, moduleMocker, timerConfig); 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 6323ad19adf1..150e724be339 100644 --- a/packages/jest-util/src/fake_timers.js +++ b/packages/jest-util/src/fake_timers.js @@ -57,9 +57,14 @@ type TimerAPI = { /* eslint-enable flowtype/no-weak-types */ }; +type TimerConfig = {| + idToRef: (id: number) => Ref, + refToId: (ref: Ref) => number, +|}; + const MS_IN_A_YEAR = 31536000000; -export default class FakeTimers { +export default class FakeTimers { _cancelledImmediates: {[key: TimerID]: boolean}; _cancelledTicks: {[key: TimerID]: boolean}; _config: ProjectConfig; @@ -74,14 +79,17 @@ export default class FakeTimers { _timerAPIs: TimerAPI; _timers: {[key: TimerID]: Timer}; _uuidCounter: number; + _timerConfig: TimerConfig; constructor( global: Global, moduleMocker: ModuleMocker, + timerConfig: TimerConfig, config: ProjectConfig, maxLoops?: number, ) { this._global = global; + this._timerConfig = timerConfig; this._config = config; this._maxLoops = maxLoops || 100000; this._uuidCounter = 1; @@ -370,9 +378,11 @@ export default class FakeTimers { }; } - _fakeClearTimer(uuid: TimerID) { + _fakeClearTimer(timerRef: TimerRef) { + const uuid = this._timerConfig.refToId(timerRef); + if (this._timers.hasOwnProperty(uuid)) { - delete this._timers[uuid]; + delete this._timers[String(uuid)]; } } @@ -462,7 +472,7 @@ export default class FakeTimers { type: 'interval', }; - return uuid; + return this._timerConfig.idToRef(uuid); } _fakeSetTimeout(callback: Callback, delay?: number) { @@ -488,7 +498,7 @@ export default class FakeTimers { type: 'timeout', }; - return uuid; + return this._timerConfig.idToRef(uuid); } _getNextTimerHandle() { From 22c673a83f4ec674f4e1c251b99d7c4a02ce0027 Mon Sep 17 00:00:00 2001 From: Jelle Versele Date: Sat, 7 Oct 2017 23:12:30 +0200 Subject: [PATCH 2/4] CR feedback, use toBeUndefined. --- .../src/__tests__/node_environment.test.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/packages/jest-environment-node/src/__tests__/node_environment.test.js b/packages/jest-environment-node/src/__tests__/node_environment.test.js index 20ee17f9681a..e1235f0e27c8 100644 --- a/packages/jest-environment-node/src/__tests__/node_environment.test.js +++ b/packages/jest-environment-node/src/__tests__/node_environment.test.js @@ -37,9 +37,9 @@ describe('NodeEnvironment', () => { const timer2 = env1.global.setInterval(() => {}, 0); [timer1, timer2].forEach(timer => { - expect(timer.id).not.toBe(undefined); - expect(timer.ref).not.toBe(undefined); - expect(timer.unref).not.toBe(undefined); + expect(timer.id).not.toBeUndefined(); + expect(timer.ref).not.toBeUndefined(); + expect(timer.unref).not.toBeUndefined(); }); }); }); From 48a129fef5649d4d66294d0d523f3057c0008b5e Mon Sep 17 00:00:00 2001 From: Jelle Versele Date: Sat, 7 Oct 2017 23:41:24 +0200 Subject: [PATCH 3/4] CR feedback, turn Faketimer constructor args into an object.. --- packages/jest-environment-jsdom/src/index.js | 8 +- packages/jest-environment-node/src/index.js | 8 +- .../src/__tests__/fake_timers.test.js | 106 +++++++++--------- packages/jest-util/src/fake_timers.js | 10 +- 4 files changed, 70 insertions(+), 62 deletions(-) diff --git a/packages/jest-environment-jsdom/src/index.js b/packages/jest-environment-jsdom/src/index.js index a14b1ea7cbcf..05ae31ec7a2b 100644 --- a/packages/jest-environment-jsdom/src/index.js +++ b/packages/jest-environment-jsdom/src/index.js @@ -50,12 +50,12 @@ class JSDOMEnvironment { refToId: (ref: number) => ref, }; - this.fakeTimers = new FakeTimers( + this.fakeTimers = new FakeTimers({ + config, global, - this.moduleMocker, + moduleMocker: this.moduleMocker, timerConfig, - config, - ); + }); } dispose(): void { diff --git a/packages/jest-environment-node/src/index.js b/packages/jest-environment-node/src/index.js index 73c2c939e632..fe29d8044b0a 100644 --- a/packages/jest-environment-node/src/index.js +++ b/packages/jest-environment-node/src/index.js @@ -57,12 +57,12 @@ class NodeEnvironment { refToId: timerRefToId, }; - this.fakeTimers = new FakeTimers( + this.fakeTimers = new FakeTimers({ + config, global, - this.moduleMocker, + moduleMocker: this.moduleMocker, timerConfig, - config, - ); + }); } dispose() { diff --git a/packages/jest-util/src/__tests__/fake_timers.test.js b/packages/jest-util/src/__tests__/fake_timers.test.js index 63f3fec44c73..dbfca1ee3085 100644 --- a/packages/jest-util/src/__tests__/fake_timers.test.js +++ b/packages/jest-util/src/__tests__/fake_timers.test.js @@ -29,28 +29,28 @@ describe('FakeTimers', () => { /* eslint-disable no-new */ it('installs setTimeout mock', () => { const global = {process}; - const timers = new FakeTimers(global, moduleMocker, timerConfig); + const timers = new FakeTimers({global, moduleMocker, timerConfig}); timers.useFakeTimers(); expect(global.setTimeout).not.toBe(undefined); }); it('installs clearTimeout mock', () => { const global = {process}; - const timers = new FakeTimers(global, moduleMocker, timerConfig); + const timers = new FakeTimers({global, moduleMocker, timerConfig}); timers.useFakeTimers(); expect(global.clearTimeout).not.toBe(undefined); }); it('installs setInterval mock', () => { const global = {process}; - const timers = new FakeTimers(global, moduleMocker, timerConfig); + const timers = new FakeTimers({global, moduleMocker, timerConfig}); timers.useFakeTimers(); expect(global.setInterval).not.toBe(undefined); }); it('installs clearInterval mock', () => { const global = {process}; - const timers = new FakeTimers(global, moduleMocker, timerConfig); + const timers = new FakeTimers({global, moduleMocker, timerConfig}); timers.useFakeTimers(); expect(global.clearInterval).not.toBe(undefined); }); @@ -62,7 +62,7 @@ describe('FakeTimers', () => { nextTick: origNextTick, }, }; - const timers = new FakeTimers(global, moduleMocker, timerConfig); + const timers = new FakeTimers({global, moduleMocker, timerConfig}); timers.useFakeTimers(); expect(global.process.nextTick).not.toBe(origNextTick); }); @@ -73,7 +73,7 @@ describe('FakeTimers', () => { process, setImmediate: origSetImmediate, }; - const timers = new FakeTimers(global, moduleMocker, timerConfig); + const timers = new FakeTimers({global, moduleMocker, timerConfig}); timers.useFakeTimers(); expect(global.setImmediate).not.toBe(origSetImmediate); }); @@ -86,7 +86,7 @@ describe('FakeTimers', () => { process, setImmediate: origSetImmediate, }; - const timers = new FakeTimers(global, moduleMocker, timerConfig); + const timers = new FakeTimers({global, moduleMocker, timerConfig}); timers.useFakeTimers(); expect(global.clearImmediate).not.toBe(origClearImmediate); }); @@ -100,7 +100,7 @@ describe('FakeTimers', () => { }, }; - const timers = new FakeTimers(global, moduleMocker, timerConfig); + const timers = new FakeTimers({global, moduleMocker, timerConfig}); timers.useFakeTimers(); const runOrder = []; @@ -128,7 +128,7 @@ describe('FakeTimers', () => { }, }; - const timers = new FakeTimers(global, moduleMocker, timerConfig); + const timers = new FakeTimers({global, moduleMocker, timerConfig}); timers.useFakeTimers(); timers.runAllTicks(); @@ -142,7 +142,7 @@ describe('FakeTimers', () => { }, }; - const timers = new FakeTimers(global, moduleMocker, timerConfig); + const timers = new FakeTimers({global, moduleMocker, timerConfig}); timers.useFakeTimers(); const mock1 = jest.genMockFn(); @@ -165,7 +165,7 @@ describe('FakeTimers', () => { }, }; - const timers = new FakeTimers(global, moduleMocker, timerConfig); + const timers = new FakeTimers({global, moduleMocker, timerConfig}); timers.useFakeTimers(); const mock1 = jest.genMockFn(); @@ -188,7 +188,7 @@ describe('FakeTimers', () => { setImmediate: nativeSetImmediate, }; - const timers = new FakeTimers(global, moduleMocker, timerConfig); + const timers = new FakeTimers({global, moduleMocker, timerConfig}); timers.useFakeTimers(); const mock1 = jest.genMockFn(); @@ -211,7 +211,7 @@ describe('FakeTimers', () => { }, }; - const timers = new FakeTimers(global, moduleMocker, timerConfig); + const timers = new FakeTimers({global, moduleMocker, timerConfig}); timers.useFakeTimers(); const mock1 = jest.genMockFn(); @@ -234,7 +234,7 @@ describe('FakeTimers', () => { setImmediate: nativeSetImmediate, }; - const timers = new FakeTimers(global, moduleMocker, timerConfig); + const timers = new FakeTimers({global, moduleMocker, timerConfig}); timers.useFakeTimers(); const mock1 = jest.genMockFn(); @@ -257,7 +257,7 @@ describe('FakeTimers', () => { setImmediate: nativeSetImmediate, }; - const timers = new FakeTimers(global, moduleMocker, timerConfig); + const timers = new FakeTimers({global, moduleMocker, timerConfig}); timers.useFakeTimers(); const mock1 = jest.genMockFn(); @@ -280,13 +280,12 @@ describe('FakeTimers', () => { }, }; - const timers = new FakeTimers( + const timers = new FakeTimers({ global, + maxLoops: 100, moduleMocker, timerConfig, - null, - 100, - ); + }); timers.useFakeTimers(); @@ -308,7 +307,7 @@ describe('FakeTimers', () => { describe('runAllTimers', () => { it('runs all timers in order', () => { const global = {process}; - const timers = new FakeTimers(global, moduleMocker, timerConfig); + const timers = new FakeTimers({global, moduleMocker, timerConfig}); timers.useFakeTimers(); const runOrder = []; @@ -332,8 +331,13 @@ describe('FakeTimers', () => { it('warns when trying to advance timers while real timers are used', () => { const consoleWarn = console.warn; console.warn = jest.fn(); - const timers = new FakeTimers(global, moduleMocker, timerConfig, { - rootDir: __dirname, + const timers = new FakeTimers({ + config: { + rootDir: __dirname, + }, + global, + moduleMocker, + timerConfig, }); timers.runAllTimers(); expect( @@ -349,14 +353,14 @@ describe('FakeTimers', () => { setTimeout: nativeSetTimeout, }; - const timers = new FakeTimers(global, moduleMocker, timerConfig); + const timers = new FakeTimers({global, moduleMocker, timerConfig}); timers.useFakeTimers(); timers.runAllTimers(); }); it('only runs a setTimeout callback once (ever)', () => { const global = {process}; - const timers = new FakeTimers(global, moduleMocker, timerConfig); + const timers = new FakeTimers({global, moduleMocker, timerConfig}); timers.useFakeTimers(); const fn = jest.genMockFn(); @@ -372,7 +376,7 @@ describe('FakeTimers', () => { it('runs callbacks with arguments after the interval', () => { const global = {process}; - const timers = new FakeTimers(global, moduleMocker, timerConfig); + const timers = new FakeTimers({global, moduleMocker, timerConfig}); timers.useFakeTimers(); const fn = jest.genMockFn(); @@ -390,7 +394,7 @@ describe('FakeTimers', () => { setTimeout: nativeSetTimeout, }; - const timers = new FakeTimers(global, moduleMocker, timerConfig); + const timers = new FakeTimers({global, moduleMocker, timerConfig}); timers.useFakeTimers(); const mock1 = jest.genMockFn(); @@ -403,13 +407,12 @@ describe('FakeTimers', () => { it('throws before allowing infinite recursion', () => { const global = {process}; - const timers = new FakeTimers( + const timers = new FakeTimers({ global, + maxLoops: 100, moduleMocker, timerConfig, - null, - 100, - ); + }); timers.useFakeTimers(); global.setTimeout(function infinitelyRecursingCallback() { @@ -428,7 +431,7 @@ describe('FakeTimers', () => { it('also clears ticks', () => { const global = {process}; - const timers = new FakeTimers(global, moduleMocker, timerConfig); + const timers = new FakeTimers({global, moduleMocker, timerConfig}); timers.useFakeTimers(); const fn = jest.genMockFn(); @@ -445,7 +448,7 @@ describe('FakeTimers', () => { describe('runTimersToTime', () => { it('runs timers in order', () => { const global = {process}; - const timers = new FakeTimers(global, moduleMocker, timerConfig); + const timers = new FakeTimers({global, moduleMocker, timerConfig}); timers.useFakeTimers(); const runOrder = []; @@ -484,7 +487,7 @@ describe('FakeTimers', () => { it('does nothing when no timers have been scheduled', () => { const global = {process}; - const timers = new FakeTimers(global, moduleMocker, timerConfig); + const timers = new FakeTimers({global, moduleMocker, timerConfig}); timers.useFakeTimers(); timers.runTimersToTime(100); @@ -492,13 +495,12 @@ describe('FakeTimers', () => { it('throws before allowing infinite recursion', () => { const global = {process}; - const timers = new FakeTimers( + const timers = new FakeTimers({ global, + maxLoops: 100, moduleMocker, timerConfig, - null, - 100, - ); + }); timers.useFakeTimers(); global.setTimeout(function infinitelyRecursingCallback() { @@ -519,7 +521,7 @@ describe('FakeTimers', () => { describe('reset', () => { it('resets all pending setTimeouts', () => { const global = {process}; - const timers = new FakeTimers(global, moduleMocker, timerConfig); + const timers = new FakeTimers({global, moduleMocker, timerConfig}); timers.useFakeTimers(); const mock1 = jest.genMockFn(); @@ -532,7 +534,7 @@ describe('FakeTimers', () => { it('resets all pending setIntervals', () => { const global = {process}; - const timers = new FakeTimers(global, moduleMocker, timerConfig); + const timers = new FakeTimers({global, moduleMocker, timerConfig}); timers.useFakeTimers(); const mock1 = jest.genMockFn(); @@ -550,7 +552,7 @@ describe('FakeTimers', () => { }, setImmediate: () => {}, }; - const timers = new FakeTimers(global, moduleMocker, timerConfig); + const timers = new FakeTimers({global, moduleMocker, timerConfig}); timers.useFakeTimers(); const mock1 = jest.genMockFn(); @@ -565,7 +567,7 @@ describe('FakeTimers', () => { it('resets current runTimersToTime time cursor', () => { const global = {process}; - const timers = new FakeTimers(global, moduleMocker, timerConfig); + const timers = new FakeTimers({global, moduleMocker, timerConfig}); timers.useFakeTimers(); const mock1 = jest.genMockFn(); @@ -589,7 +591,7 @@ describe('FakeTimers', () => { setImmediate: nativeSetImmediate, }; - const timers = new FakeTimers(global, moduleMocker, timerConfig); + const timers = new FakeTimers({global, moduleMocker, timerConfig}); timers.useFakeTimers(); const runOrder = []; @@ -637,7 +639,7 @@ describe('FakeTimers', () => { it('does not run timers that were cleared in another timer', () => { const global = {process}; - const timers = new FakeTimers(global, moduleMocker, timerConfig); + const timers = new FakeTimers({global, moduleMocker, timerConfig}); timers.useFakeTimers(); const fn = jest.genMockFn(); @@ -665,7 +667,7 @@ describe('FakeTimers', () => { setInterval: nativeSetInterval, setTimeout: nativeSetTimeout, }; - const timers = new FakeTimers(global, moduleMocker, timerConfig); + const timers = new FakeTimers({global, moduleMocker, timerConfig}); timers.useFakeTimers(); // clearInterval() @@ -710,7 +712,7 @@ describe('FakeTimers', () => { setInterval: nativeSetInterval, setTimeout: nativeSetTimeout, }; - const timers = new FakeTimers(global, moduleMocker, timerConfig); + const timers = new FakeTimers({global, moduleMocker, timerConfig}); timers.useFakeTimers(); // clearInterval() @@ -764,7 +766,7 @@ describe('FakeTimers', () => { process, setTimeout: nativeSetTimeout, }; - const timers = new FakeTimers(global, moduleMocker, timerConfig); + const timers = new FakeTimers({global, moduleMocker, timerConfig}); timers.useFakeTimers(); expect(() => { @@ -796,7 +798,7 @@ describe('FakeTimers', () => { setInterval: nativeSetInterval, setTimeout: nativeSetTimeout, }; - const timers = new FakeTimers(global, moduleMocker, timerConfig); + const timers = new FakeTimers({global, moduleMocker, timerConfig}); timers.useFakeTimers(); // Ensure that timers has overridden the native timer APIs @@ -820,7 +822,7 @@ describe('FakeTimers', () => { const global = { process: {nextTick: nativeProcessNextTick}, }; - const timers = new FakeTimers(global, moduleMocker, timerConfig); + const timers = new FakeTimers({global, moduleMocker, timerConfig}); timers.useFakeTimers(); // Ensure that timers has overridden the native timer APIs @@ -841,7 +843,7 @@ describe('FakeTimers', () => { process, setImmediate: nativeSetImmediate, }; - const timers = new FakeTimers(global, moduleMocker, timerConfig); + const timers = new FakeTimers({global, moduleMocker, timerConfig}); timers.useFakeTimers(); // Ensure that timers has overridden the native timer APIs @@ -870,7 +872,7 @@ describe('FakeTimers', () => { setInterval: nativeSetInterval, setTimeout: nativeSetTimeout, }; - const timers = new FakeTimers(global, moduleMocker, timerConfig); + const timers = new FakeTimers({global, moduleMocker, timerConfig}); timers.useRealTimers(); // Ensure that the real timers are installed at this point @@ -894,7 +896,7 @@ describe('FakeTimers', () => { const global = { process: {nextTick: nativeProcessNextTick}, }; - const timers = new FakeTimers(global, moduleMocker, timerConfig); + const timers = new FakeTimers({global, moduleMocker, timerConfig}); timers.useRealTimers(); // Ensure that the real timers are installed at this point @@ -915,7 +917,7 @@ describe('FakeTimers', () => { process, setImmediate: nativeSetImmediate, }; - const fakeTimers = new FakeTimers(global, moduleMocker, timerConfig); + const fakeTimers = new FakeTimers({global, moduleMocker, timerConfig}); 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 150e724be339..9f8f4873669b 100644 --- a/packages/jest-util/src/fake_timers.js +++ b/packages/jest-util/src/fake_timers.js @@ -81,13 +81,19 @@ export default class FakeTimers { _uuidCounter: number; _timerConfig: TimerConfig; - constructor( + constructor({ + global, + moduleMocker, + timerConfig, + config, + maxLoops, + }: { global: Global, moduleMocker: ModuleMocker, timerConfig: TimerConfig, config: ProjectConfig, maxLoops?: number, - ) { + }) { this._global = global; this._timerConfig = timerConfig; this._config = config; From 8fe3228d9a45cf4f8c308bf1833360f9cd9c598b Mon Sep 17 00:00:00 2001 From: Jelle Versele Date: Sat, 7 Oct 2017 23:44:31 +0200 Subject: [PATCH 4/4] tweak Faketimer tests, assert if ref/unref are actually functions. --- .../src/__tests__/jsdom_environment.test.js | 2 +- .../src/__tests__/node_environment.test.js | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/packages/jest-environment-jsdom/src/__tests__/jsdom_environment.test.js b/packages/jest-environment-jsdom/src/__tests__/jsdom_environment.test.js index 92770a38007d..47c73604a3ca 100644 --- a/packages/jest-environment-jsdom/src/__tests__/jsdom_environment.test.js +++ b/packages/jest-environment-jsdom/src/__tests__/jsdom_environment.test.js @@ -18,7 +18,7 @@ describe('JSDomEnvironment', () => { const timer2 = env1.global.setInterval(() => {}, 0); [timer1, timer2].forEach(timer => { - expect(typeof timer === 'number').toBe(true); + expect(typeof timer).toBe('number'); }); }); }); diff --git a/packages/jest-environment-node/src/__tests__/node_environment.test.js b/packages/jest-environment-node/src/__tests__/node_environment.test.js index e1235f0e27c8..2ad2955723ee 100644 --- a/packages/jest-environment-node/src/__tests__/node_environment.test.js +++ b/packages/jest-environment-node/src/__tests__/node_environment.test.js @@ -38,8 +38,8 @@ describe('NodeEnvironment', () => { [timer1, timer2].forEach(timer => { expect(timer.id).not.toBeUndefined(); - expect(timer.ref).not.toBeUndefined(); - expect(timer.unref).not.toBeUndefined(); + expect(typeof timer.ref).toBe('function'); + expect(typeof timer.unref).toBe('function'); }); }); });