Skip to content

Commit

Permalink
chore: remove long-deprecated timer globals (#7285)
Browse files Browse the repository at this point in the history
  • Loading branch information
SimenB committed Oct 26, 2018
1 parent 251c295 commit e41f0bb
Show file tree
Hide file tree
Showing 8 changed files with 52 additions and 24 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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

Expand Down
4 changes: 4 additions & 0 deletions docs/JestObjectAPI.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)`
Expand Down
1 change: 0 additions & 1 deletion packages/jest-environment-jsdom/src/__mocks__/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ JSDOMEnvironment.mockImplementation(function(config) {
this.global = {
JSON,
console: {},
mockClearTimers: jest.fn(),
};

const globalValues = Object.assign({}, config.globals);
Expand Down
29 changes: 16 additions & 13 deletions packages/jest-runtime/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}
}
Expand Down Expand Up @@ -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,
Expand Down
22 changes: 22 additions & 0 deletions packages/jest-util/src/__tests__/fake_timers.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
});
});
16 changes: 6 additions & 10 deletions packages/jest-util/src/fake_timers.js
Original file line number Diff line number Diff line change
Expand Up @@ -114,16 +114,6 @@ export default class FakeTimers<TimerRef> {

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);

This comment has been minimized.

Copy link
@vincgraz

vincgraz Oct 29, 2018

Y

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() {
Expand Down Expand Up @@ -352,6 +342,12 @@ export default class FakeTimers<TimerRef> {
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(
Expand Down
1 change: 1 addition & 0 deletions types/Environment.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ declare class $JestEnvironment {
advanceTimersByTime(msToRun: number): void,
runOnlyPendingTimers(): void,
runWithRealTimers(callback: any): void,
getTimerCount(): number,
useFakeTimers(): void,
useRealTimers(): void,
};
Expand Down
1 change: 1 addition & 0 deletions types/Jest.js
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down

0 comments on commit e41f0bb

Please sign in to comment.