diff --git a/CHANGELOG.md b/CHANGELOG.md index 6610643f005a..c316ca84cad7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -24,6 +24,7 @@ - `[jest-config]` Accept an array as as well as a string for `testRegex`([#7209]https://github.com/facebook/jest/pull/7209)) - `[babel-preset-jest]` [**BREAKING**] Export a function instead of an object for Babel 7 compatibility ([#7203](https://github.com/facebook/jest/pull/7203)) - `[expect]` Check constructor equality in .toStrictEqual() ([#7005](https://github.com/facebook/jest/pull/7005)) +- `[jest-util]` Add `jest.getTimerCount()` to get the count of scheduled fake timers ([#7285](https://github.com/facebook/jest/pull/7285)) ### Fixes @@ -85,6 +86,7 @@ - `[jest-haste-map]` Standardize filenames ([#7266](https://github.com/facebook/jest/pull/7266)) - `[*]` [**BREAKING**] Require Node.js 6+ for all packages ([#7258](https://github.com/facebook/jest/pull/7258)) - `[docs]` Add correct default value for `testUrl` config option ([#7277](https://github.com/facebook/jest/pull/7277)) +- `[jest-util]` [**BREAKING**] Remove long-deprecated globals for fake timers ([#7285](https://github.com/facebook/jest/pull/7285)) ### Performance diff --git a/docs/JestObjectAPI.md b/docs/JestObjectAPI.md index 89ded4fd7417..58c86823eb64 100644 --- a/docs/JestObjectAPI.md +++ b/docs/JestObjectAPI.md @@ -447,6 +447,10 @@ Removes any pending timers from the timer system. This means, if any timers have been scheduled (but have not yet executed), they will be cleared and will never have the opportunity to execute in the future. +### `jest.getTimerCount()` + +Returns the number of fake timers still left to run. + ## Misc ### `jest.setTimeout(timeout)` diff --git a/packages/jest-environment-jsdom/src/__mocks__/index.js b/packages/jest-environment-jsdom/src/__mocks__/index.js index 68f8411ac835..8ae042ba08ed 100644 --- a/packages/jest-environment-jsdom/src/__mocks__/index.js +++ b/packages/jest-environment-jsdom/src/__mocks__/index.js @@ -14,7 +14,6 @@ JSDOMEnvironment.mockImplementation(function(config) { this.global = { JSON, console: {}, - mockClearTimers: jest.fn(), }; const globalValues = Object.assign({}, config.globals); diff --git a/packages/jest-runtime/src/index.js b/packages/jest-runtime/src/index.js index 8adede5ec442..a24ed1712fd3 100644 --- a/packages/jest-runtime/src/index.js +++ b/packages/jest-runtime/src/index.js @@ -445,20 +445,22 @@ class Runtime { this._mockRegistry = Object.create(null); this._moduleRegistry = Object.create(null); - if (this._environment && this._environment.global) { - const envGlobal = this._environment.global; - Object.keys(envGlobal).forEach(key => { - const globalMock = envGlobal[key]; - if ( - (typeof globalMock === 'object' && globalMock !== null) || - typeof globalMock === 'function' - ) { - globalMock._isMockFunction === true && globalMock.mockClear(); - } - }); + if (this._environment) { + if (this._environment.global) { + const envGlobal = this._environment.global; + Object.keys(envGlobal).forEach(key => { + const globalMock = envGlobal[key]; + if ( + (typeof globalMock === 'object' && globalMock !== null) || + typeof globalMock === 'function' + ) { + globalMock._isMockFunction === true && globalMock.mockClear(); + } + }); + } - if (envGlobal.mockClearTimers) { - envGlobal.mockClearTimers(); + if (this._environment.fakeTimers) { + this._environment.fakeTimers.clearAllTimers(); } } } @@ -932,6 +934,7 @@ class Runtime { fn, genMockFromModule: (moduleName: string) => this._generateMock(from, moduleName), + getTimerCount: () => this._environment.fakeTimers.getTimerCount(), isMockFunction: this._moduleMocker.isMockFunction, mock, requireActual: localRequire.requireActual, diff --git a/packages/jest-util/src/__tests__/fake_timers.test.js b/packages/jest-util/src/__tests__/fake_timers.test.js index a9d8434532bf..c214f0312591 100644 --- a/packages/jest-util/src/__tests__/fake_timers.test.js +++ b/packages/jest-util/src/__tests__/fake_timers.test.js @@ -943,4 +943,26 @@ describe('FakeTimers', () => { expect(global.clearImmediate).not.toBe(nativeClearImmediate); }); }); + + describe('getTimerCount', () => { + it('returns the correct count', () => { + const timers = new FakeTimers({global, moduleMocker, timerConfig}); + + timers.useFakeTimers(); + + global.setTimeout(() => {}, 0); + global.setTimeout(() => {}, 0); + global.setTimeout(() => {}, 10); + + expect(timers.getTimerCount()).toEqual(3); + + timers.advanceTimersByTime(5); + + expect(timers.getTimerCount()).toEqual(1); + + timers.advanceTimersByTime(5); + + expect(timers.getTimerCount()).toEqual(0); + }); + }); }); diff --git a/packages/jest-util/src/fake_timers.js b/packages/jest-util/src/fake_timers.js index a31cb67b2c72..e1c64f6ce2c4 100644 --- a/packages/jest-util/src/fake_timers.js +++ b/packages/jest-util/src/fake_timers.js @@ -114,16 +114,6 @@ export default class FakeTimers { this.reset(); this._createMocks(); - - // These globally-accessible function are now deprecated! - // They will go away very soon, so do not use them! - // Instead, use the versions available on the `jest` object - global.mockRunTicksRepeatedly = this.runAllTicks.bind(this); - global.mockRunTimersOnce = this.runOnlyPendingTimers.bind(this); - global.mockAdvanceTimersByTime = this.advanceTimersByTime.bind(this); - global.mockRunTimersRepeatedly = this.runAllTimers.bind(this); - global.mockClearTimers = this.clearAllTimers.bind(this); - global.mockGetTimersCount = () => Object.keys(this._timers).length; } clearAllTimers() { @@ -352,6 +342,12 @@ export default class FakeTimers { global.process.nextTick = this._fakeTimerAPIs.nextTick; } + getTimerCount() { + this._checkFakeTimers(); + + return Object.keys(this._timers).length; + } + _checkFakeTimers() { if (this._global.setTimeout !== this._fakeTimerAPIs.setTimeout) { this._global.console.warn( diff --git a/types/Environment.js b/types/Environment.js index ed6ee314c490..07128ef7da27 100644 --- a/types/Environment.js +++ b/types/Environment.js @@ -28,6 +28,7 @@ declare class $JestEnvironment { advanceTimersByTime(msToRun: number): void, runOnlyPendingTimers(): void, runWithRealTimers(callback: any): void, + getTimerCount(): number, useFakeTimers(): void, useRealTimers(): void, }; diff --git a/types/Jest.js b/types/Jest.js index 88966e299f2c..889c45382a95 100644 --- a/types/Jest.js +++ b/types/Jest.js @@ -39,6 +39,7 @@ export type Jest = {| runOnlyPendingTimers(): void, advanceTimersByTime(msToRun: number): void, runTimersToTime(msToRun: number): void, + getTimerCount(): number, setMock(moduleName: string, moduleExports: any): Jest, setTimeout(timeout: number): Jest, spyOn(