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
refactor(@jest/fake-timers): improve internal typings of legacy fake timers #12567
Merged
SimenB
merged 2 commits into
jestjs:main
from
mrazauskas:refactor-types-legacyFakeTimers
Mar 24, 2022
Merged
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,11 +5,16 @@ | |
* LICENSE file in the root directory of this source tree. | ||
*/ | ||
|
||
/* eslint-disable local/ban-types-eventually, local/prefer-spread-eventually */ | ||
/* eslint-disable local/prefer-spread-eventually */ | ||
|
||
import util = require('util'); | ||
import {StackTraceConfig, formatStackTrace} from 'jest-message-util'; | ||
import type {ModuleMocker} from 'jest-mock'; | ||
import type { | ||
FunctionLike, | ||
Mock, | ||
ModuleMocker, | ||
UnknownFunction, | ||
} from 'jest-mock'; | ||
import {setGlobal} from 'jest-util'; | ||
|
||
type Callback = (...args: Array<unknown>) => void; | ||
|
@@ -29,38 +34,42 @@ type Timer = { | |
}; | ||
|
||
type TimerAPI = { | ||
cancelAnimationFrame: FakeTimersGlobal['cancelAnimationFrame']; | ||
cancelAnimationFrame: typeof globalThis.cancelAnimationFrame; | ||
clearImmediate: typeof globalThis.clearImmediate; | ||
clearInterval: typeof globalThis.clearInterval; | ||
clearTimeout: typeof globalThis.clearTimeout; | ||
nextTick: typeof process.nextTick; | ||
|
||
requestAnimationFrame: FakeTimersGlobal['requestAnimationFrame']; | ||
requestAnimationFrame: typeof globalThis.requestAnimationFrame; | ||
setImmediate: typeof globalThis.setImmediate; | ||
setInterval: typeof globalThis.setInterval; | ||
setTimeout: typeof globalThis.setTimeout; | ||
}; | ||
|
||
type FakeTimerAPI = { | ||
cancelAnimationFrame: Mock<FakeTimers['_fakeClearTimer']>; | ||
clearImmediate: Mock<FakeTimers['_fakeClearImmediate']>; | ||
clearInterval: Mock<FakeTimers['_fakeClearTimer']>; | ||
clearTimeout: Mock<FakeTimers['_fakeClearTimer']>; | ||
nextTick: Mock<FakeTimers['_fakeNextTick']>; | ||
requestAnimationFrame: Mock<FakeTimers['_fakeRequestAnimationFrame']>; | ||
setImmediate: Mock<FakeTimers['_fakeSetImmediate']>; | ||
setInterval: Mock<FakeTimers['_fakeSetInterval']>; | ||
setTimeout: Mock<FakeTimers['_fakeSetTimeout']>; | ||
}; | ||
|
||
type TimerConfig<Ref> = { | ||
idToRef: (id: number) => Ref; | ||
refToId: (ref: Ref) => number | void; | ||
}; | ||
|
||
const MS_IN_A_YEAR = 31536000000; | ||
|
||
type GlobalThis = typeof globalThis; | ||
|
||
interface FakeTimersGlobal extends GlobalThis { | ||
cancelAnimationFrame: (handle: number) => void; | ||
requestAnimationFrame: (callback: (time: number) => void) => number; | ||
} | ||
|
||
export default class FakeTimers<TimerRef> { | ||
export default class FakeTimers<TimerRef = unknown> { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Adding a default to generic helps to keep |
||
private _cancelledTicks!: Record<string, boolean>; | ||
private _config: StackTraceConfig; | ||
private _disposed?: boolean; | ||
private _fakeTimerAPIs!: TimerAPI; | ||
private _global: FakeTimersGlobal; | ||
private _fakeTimerAPIs!: FakeTimerAPI; | ||
private _global: typeof globalThis; | ||
private _immediates!: Array<Tick>; | ||
private _maxLoops: number; | ||
private _moduleMocker: ModuleMocker; | ||
|
@@ -78,7 +87,7 @@ export default class FakeTimers<TimerRef> { | |
config, | ||
maxLoops, | ||
}: { | ||
global: FakeTimersGlobal; | ||
global: typeof globalThis; | ||
moduleMocker: ModuleMocker; | ||
timerConfig: TimerConfig<TimerRef>; | ||
config: StackTraceConfig; | ||
|
@@ -390,6 +399,7 @@ export default class FakeTimers<TimerRef> { | |
} | ||
|
||
private _checkFakeTimers() { | ||
// @ts-expect-error: condition always returns 'true' | ||
if (this._global.setTimeout !== this._fakeTimerAPIs?.setTimeout) { | ||
this._global.console.warn( | ||
'A function to advance timers was called but the timers API is not ' + | ||
|
@@ -407,32 +417,26 @@ export default class FakeTimers<TimerRef> { | |
} | ||
|
||
private _createMocks() { | ||
const fn = (impl: Function) => | ||
// @ts-expect-error TODO: figure out better typings here | ||
this._moduleMocker.fn().mockImplementation(impl); | ||
const fn = <T extends FunctionLike = UnknownFunction>(implementation?: T) => | ||
this._moduleMocker.fn(implementation); | ||
|
||
const promisifiableFakeSetTimeout = fn(this._fakeSetTimeout.bind(this)); | ||
// @ts-expect-error TODO: figure out better typings here | ||
// @ts-expect-error: no index | ||
promisifiableFakeSetTimeout[util.promisify.custom] = ( | ||
delay?: number, | ||
arg?: unknown, | ||
) => | ||
new Promise(resolve => promisifiableFakeSetTimeout(resolve, delay, arg)); | ||
|
||
// TODO: add better typings; these are mocks, but typed as regular timers | ||
this._fakeTimerAPIs = { | ||
cancelAnimationFrame: fn(this._fakeClearTimer.bind(this)), | ||
clearImmediate: fn(this._fakeClearImmediate.bind(this)), | ||
clearInterval: fn(this._fakeClearTimer.bind(this)), | ||
clearTimeout: fn(this._fakeClearTimer.bind(this)), | ||
nextTick: fn(this._fakeNextTick.bind(this)), | ||
// @ts-expect-error TODO: figure out better typings here | ||
requestAnimationFrame: fn(this._fakeRequestAnimationFrame.bind(this)), | ||
// @ts-expect-error TODO: figure out better typings here | ||
setImmediate: fn(this._fakeSetImmediate.bind(this)), | ||
// @ts-expect-error TODO: figure out better typings here | ||
setInterval: fn(this._fakeSetInterval.bind(this)), | ||
// @ts-expect-error TODO: figure out better typings here | ||
setTimeout: promisifiableFakeSetTimeout, | ||
}; | ||
} | ||
|
@@ -451,7 +455,7 @@ export default class FakeTimers<TimerRef> { | |
); | ||
} | ||
|
||
private _fakeNextTick(callback: Callback, ...args: Array<any>) { | ||
private _fakeNextTick(callback: Callback, ...args: Array<unknown>) { | ||
if (this._disposed) { | ||
return; | ||
} | ||
|
@@ -480,7 +484,7 @@ export default class FakeTimers<TimerRef> { | |
}, 1000 / 60); | ||
} | ||
|
||
private _fakeSetImmediate(callback: Callback, ...args: Array<any>) { | ||
private _fakeSetImmediate(callback: Callback, ...args: Array<unknown>) { | ||
if (this._disposed) { | ||
return null; | ||
} | ||
|
@@ -508,7 +512,7 @@ export default class FakeTimers<TimerRef> { | |
private _fakeSetInterval( | ||
callback: Callback, | ||
intervalDelay?: number, | ||
...args: Array<any> | ||
...args: Array<unknown> | ||
) { | ||
if (this._disposed) { | ||
return null; | ||
|
@@ -533,7 +537,7 @@ export default class FakeTimers<TimerRef> { | |
private _fakeSetTimeout( | ||
callback: Callback, | ||
delay?: number, | ||
...args: Array<any> | ||
...args: Array<unknown> | ||
) { | ||
if (this._disposed) { | ||
return null; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Might be these types were copied from TS, because they do not exist on
global
in Node.globalThis
defines these props, so now this is safe to remove, right?