Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: remove long-deprecated timer globals #7285

Merged
merged 6 commits into from
Oct 26, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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))
SimenB marked this conversation as resolved.
Show resolved Hide resolved

### 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();
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll copy my comment over, but does it make sense to do this? Would one expect resetModules to remove scheduled fake timers?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe if you consider fake timers and mocks to be in the same "group". In your other PR might make less sense as you're making that distinction clearer.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd expect people to call jest.clearAllTimers themselves if needed, tbh. Not sure how breaking that is, though

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can tell you that pretty easily, haha

}
}
}
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);
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() {
thymikee marked this conversation as resolved.
Show resolved Hide resolved
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