diff --git a/.eslintrc.js b/.eslintrc.js index 487a8d0e420b..4d69f9235bad 100644 --- a/.eslintrc.js +++ b/.eslintrc.js @@ -95,7 +95,6 @@ module.exports = { 'packages/jest-core/src/TestScheduler.ts', 'packages/jest-core/src/collectHandles.ts', 'packages/jest-core/src/plugins/UpdateSnapshotsInteractive.ts', - 'packages/jest-fake-timers/src/legacyFakeTimers.ts', 'packages/jest-haste-map/src/index.ts', 'packages/jest-haste-map/src/watchers/FSEventsWatcher.ts', 'packages/jest-jasmine2/src/jasmine/SpyStrategy.ts', diff --git a/packages/jest-fake-timers/src/legacyFakeTimers.ts b/packages/jest-fake-timers/src/legacyFakeTimers.ts index 2c897251443a..ee957b0444fb 100644 --- a/packages/jest-fake-timers/src/legacyFakeTimers.ts +++ b/packages/jest-fake-timers/src/legacyFakeTimers.ts @@ -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 {promisify} from '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) => void; @@ -29,18 +34,29 @@ 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; + clearImmediate: Mock; + clearInterval: Mock; + clearTimeout: Mock; + nextTick: Mock; + requestAnimationFrame: Mock; + setImmediate: Mock; + setInterval: Mock; + setTimeout: Mock; +}; + type TimerConfig = { idToRef: (id: number) => Ref; refToId: (ref: Ref) => number | void; @@ -48,19 +64,12 @@ type TimerConfig = { 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 { +export default class FakeTimers { private _cancelledTicks!: Record; private _config: StackTraceConfig; private _disposed?: boolean; - private _fakeTimerAPIs!: TimerAPI; - private _global: FakeTimersGlobal; + private _fakeTimerAPIs!: FakeTimerAPI; + private _global: typeof globalThis; private _immediates!: Array; private _maxLoops: number; private _moduleMocker: ModuleMocker; @@ -78,7 +87,7 @@ export default class FakeTimers { config, maxLoops, }: { - global: FakeTimersGlobal; + global: typeof globalThis; moduleMocker: ModuleMocker; timerConfig: TimerConfig; config: StackTraceConfig; @@ -390,6 +399,7 @@ export default class FakeTimers { } 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 { } private _createMocks() { - const fn = (impl: Function) => - // @ts-expect-error TODO: figure out better typings here - this._moduleMocker.fn().mockImplementation(impl); + const fn = (implementation?: T) => + this._moduleMocker.fn(implementation); const promisifiableFakeSetTimeout = fn(this._fakeSetTimeout.bind(this)); - // @ts-expect-error TODO: figure out better typings here - promisifiableFakeSetTimeout[util.promisify.custom] = ( + // @ts-expect-error: no index + promisifiableFakeSetTimeout[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 { ); } - private _fakeNextTick(callback: Callback, ...args: Array) { + private _fakeNextTick(callback: Callback, ...args: Array) { if (this._disposed) { return; } @@ -480,7 +484,7 @@ export default class FakeTimers { }, 1000 / 60); } - private _fakeSetImmediate(callback: Callback, ...args: Array) { + private _fakeSetImmediate(callback: Callback, ...args: Array) { if (this._disposed) { return null; } @@ -508,7 +512,7 @@ export default class FakeTimers { private _fakeSetInterval( callback: Callback, intervalDelay?: number, - ...args: Array + ...args: Array ) { if (this._disposed) { return null; @@ -533,7 +537,7 @@ export default class FakeTimers { private _fakeSetTimeout( callback: Callback, delay?: number, - ...args: Array + ...args: Array ) { if (this._disposed) { return null; diff --git a/packages/jest-mock/src/index.ts b/packages/jest-mock/src/index.ts index a478f0f1689f..8358cbe36631 100644 --- a/packages/jest-mock/src/index.ts +++ b/packages/jest-mock/src/index.ts @@ -112,7 +112,7 @@ export type MockedClass = MockInstance< prototype: T extends {prototype: any} ? Mocked : never; } & T; -type UnknownFunction = (...args: Array) => unknown; +export type UnknownFunction = (...args: Array) => unknown; /** * All what the internal typings need is to be sure that we have any-function.