From 39190eae941b7160ba75b8588fa57b1fcec9090e Mon Sep 17 00:00:00 2001 From: Max Ivanov Date: Fri, 27 Aug 2021 11:41:33 +0300 Subject: [PATCH] fix(jest-types): compat with @types/node v16 (#11645) --- CHANGELOG.md | 1 + packages/jest-console/src/BufferedConsole.ts | 6 +- packages/jest-console/src/CustomConsole.ts | 6 +- packages/jest-each/src/index.ts | 3 +- packages/jest-environment-jsdom/src/index.ts | 13 ++- packages/jest-environment/src/index.ts | 4 +- .../src/__tests__/legacyFakeTimers.test.ts | 108 +++++++++--------- .../jest-fake-timers/src/legacyFakeTimers.ts | 8 +- .../jest-fake-timers/src/modernFakeTimers.ts | 4 +- packages/jest-jasmine2/src/jasmine/Env.ts | 6 +- packages/jest-jasmine2/src/jestExpect.ts | 1 - packages/jest-jasmine2/src/pTimeout.ts | 4 +- packages/jest-jasmine2/src/queueRunner.ts | 6 +- packages/jest-jasmine2/src/types.ts | 2 +- .../jest-mock/src/__tests__/index.test.ts | 2 +- packages/jest-mock/src/index.ts | 8 +- packages/jest-repl/src/cli/runtime-cli.ts | 14 ++- packages/jest-runner/src/runTest.ts | 6 +- packages/jest-runtime/src/index.ts | 22 ++-- packages/jest-types/src/Config.ts | 2 +- packages/jest-types/src/Global.ts | 3 +- .../__tests__/installCommonGlobals.test.ts | 2 +- .../jest-util/src/installCommonGlobals.ts | 4 +- packages/jest-util/src/setGlobal.ts | 2 +- packages/jest-worker/src/types.ts | 1 + .../src/workers/NodeThreadsWorker.ts | 8 +- .../jest-worker/src/workers/processChild.ts | 2 +- 27 files changed, 134 insertions(+), 114 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 732ed0c47d2a..1220a580c073 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,6 +10,7 @@ - `[jest-environment-node]` Add `Event` and `EventTarget` to node global environment. ([#11705](https://github.com/facebook/jest/issues/11705)) - `[jest-mock]` Fix `spyOn` to use `Object.prototype.hasOwnProperty` [#11721](https://github.com/facebook/jest/pull/11721) - `[jest-resolver]` Add dependency on `jest-haste-map` [#11759](https://github.com/facebook/jest/pull/11759) +- `[jest-types]` Compat with `@types/node` v16 ([#11645](https://github.com/facebook/jest/pull/11645)) ### Chore & Maintenance diff --git a/packages/jest-console/src/BufferedConsole.ts b/packages/jest-console/src/BufferedConsole.ts index 8d67c968dea7..c670f6d4a698 100644 --- a/packages/jest-console/src/BufferedConsole.ts +++ b/packages/jest-console/src/BufferedConsole.ts @@ -7,7 +7,7 @@ import assert = require('assert'); import {Console} from 'console'; -import {format, formatWithOptions, inspect} from 'util'; +import {InspectOptions, format, formatWithOptions, inspect} from 'util'; import chalk = require('chalk'); import {ErrorWithStack, formatTime} from 'jest-util'; import type { @@ -24,7 +24,7 @@ export default class BufferedConsole extends Console { private _timers: LogTimers = {}; private _groupDepth = 0; - Console: NodeJS.ConsoleConstructor = Console; + Console: typeof Console = Console; constructor() { super({ @@ -95,7 +95,7 @@ export default class BufferedConsole extends Console { this._log('debug', format(firstArg, ...rest)); } - dir(firstArg: unknown, options: NodeJS.InspectOptions = {}): void { + dir(firstArg: unknown, options: InspectOptions = {}): void { const representation = inspect(firstArg, options); this._log('dir', formatWithOptions(options, representation)); } diff --git a/packages/jest-console/src/CustomConsole.ts b/packages/jest-console/src/CustomConsole.ts index cbf8cf768ee3..7c8009ad1159 100644 --- a/packages/jest-console/src/CustomConsole.ts +++ b/packages/jest-console/src/CustomConsole.ts @@ -7,7 +7,7 @@ import assert = require('assert'); import {Console} from 'console'; -import {format, formatWithOptions, inspect} from 'util'; +import {InspectOptions, format, formatWithOptions, inspect} from 'util'; import chalk = require('chalk'); import {clearLine, formatTime} from 'jest-util'; import type {LogCounters, LogMessage, LogTimers, LogType} from './types'; @@ -22,7 +22,7 @@ export default class CustomConsole extends Console { private _timers: LogTimers = {}; private _groupDepth = 0; - Console: NodeJS.ConsoleConstructor = Console; + Console: typeof Console = Console; constructor( stdout: NodeJS.WriteStream, @@ -73,7 +73,7 @@ export default class CustomConsole extends Console { this._log('debug', format(firstArg, ...args)); } - dir(firstArg: unknown, options: NodeJS.InspectOptions = {}): void { + dir(firstArg: unknown, options: InspectOptions = {}): void { const representation = inspect(firstArg, options); this._log('dir', formatWithOptions(options, representation)); } diff --git a/packages/jest-each/src/index.ts b/packages/jest-each/src/index.ts index 4436c465f803..104ef047dc6b 100644 --- a/packages/jest-each/src/index.ts +++ b/packages/jest-each/src/index.ts @@ -69,7 +69,8 @@ const install = ( const each = ( table: Global.EachTable, ...data: Global.TemplateData -): ReturnType => install(global as Global, table, ...data); +): ReturnType => + install(global as unknown as Global, table, ...data); each.withGlobal = (g: Global) => diff --git a/packages/jest-environment-jsdom/src/index.ts b/packages/jest-environment-jsdom/src/index.ts index 319d60d58d78..39b63efc4607 100644 --- a/packages/jest-environment-jsdom/src/index.ts +++ b/packages/jest-environment-jsdom/src/index.ts @@ -22,7 +22,7 @@ type Win = Window & }; }; -class JSDOMEnvironment implements JestEnvironment { +class JSDOMEnvironment implements JestEnvironment { dom: JSDOM | null; fakeTimers: LegacyFakeTimers | null; fakeTimersModern: ModernFakeTimers | null; @@ -52,14 +52,14 @@ class JSDOMEnvironment implements JestEnvironment { } // for "universal" code (code should use `globalThis`) - global.global = global; + global.global = global as any; // Node's error-message stack size is limited at 10, but it's pretty useful // to see more than that when a test fails. this.global.Error.stackTraceLimit = 100; installCommonGlobals(global as any, config.globals); - // TODO: remove this ASAP, but it currntly causes tests to run really slow + // TODO: remove this ASAP, but it currently causes tests to run really slow global.Buffer = Buffer; // Report uncaught errors. @@ -101,12 +101,15 @@ class JSDOMEnvironment implements JestEnvironment { this.fakeTimers = new LegacyFakeTimers({ config, - global, + global: global as unknown as typeof globalThis, moduleMocker: this.moduleMocker, timerConfig, }); - this.fakeTimersModern = new ModernFakeTimers({config, global}); + this.fakeTimersModern = new ModernFakeTimers({ + config, + global: global as unknown as typeof globalThis, + }); } async setup(): Promise {} diff --git a/packages/jest-environment/src/index.ts b/packages/jest-environment/src/index.ts index 3ea78562c32e..4ce75ea2303b 100644 --- a/packages/jest-environment/src/index.ts +++ b/packages/jest-environment/src/index.ts @@ -32,10 +32,10 @@ export type ModuleWrapper = ( ...extraGlobals: Array ) => unknown; -export declare class JestEnvironment { +export declare class JestEnvironment { constructor(config: Config.ProjectConfig, context?: EnvironmentContext); global: Global.Global; - fakeTimers: LegacyFakeTimers | null; + fakeTimers: LegacyFakeTimers | null; fakeTimersModern: ModernFakeTimers | null; moduleMocker: ModuleMocker | null; getVmContext(): Context | null; diff --git a/packages/jest-fake-timers/src/__tests__/legacyFakeTimers.test.ts b/packages/jest-fake-timers/src/__tests__/legacyFakeTimers.test.ts index d6da7dd1336b..8dca81492f51 100644 --- a/packages/jest-fake-timers/src/__tests__/legacyFakeTimers.test.ts +++ b/packages/jest-fake-timers/src/__tests__/legacyFakeTimers.test.ts @@ -31,7 +31,7 @@ describe('FakeTimers', () => { describe('construction', () => { it('installs setTimeout mock', () => { - const global = {process} as unknown as NodeJS.Global; + const global = {process} as unknown as typeof globalThis; const timers = new FakeTimers({ config, global, @@ -43,7 +43,7 @@ describe('FakeTimers', () => { }); it('accepts to promisify setTimeout mock', async () => { - const global = {process} as unknown as NodeJS.Global; + const global = {process} as unknown as typeof globalThis; const timers = new FakeTimers({ config, global, @@ -57,7 +57,7 @@ describe('FakeTimers', () => { }); it('installs clearTimeout mock', () => { - const global = {process} as unknown as NodeJS.Global; + const global = {process} as unknown as typeof globalThis; const timers = new FakeTimers({ config, global, @@ -69,7 +69,7 @@ describe('FakeTimers', () => { }); it('installs setInterval mock', () => { - const global = {process} as unknown as NodeJS.Global; + const global = {process} as unknown as typeof globalThis; const timers = new FakeTimers({ config, global, @@ -81,7 +81,7 @@ describe('FakeTimers', () => { }); it('installs clearInterval mock', () => { - const global = {process} as unknown as NodeJS.Global; + const global = {process} as unknown as typeof globalThis; const timers = new FakeTimers({ config, global, @@ -98,7 +98,7 @@ describe('FakeTimers', () => { process: { nextTick: origNextTick, }, - } as unknown as NodeJS.Global; + } as unknown as typeof globalThis; const timers = new FakeTimers({ config, global, @@ -114,7 +114,7 @@ describe('FakeTimers', () => { const global = { process, setImmediate: origSetImmediate, - } as unknown as NodeJS.Global; + } as unknown as typeof globalThis; const timers = new FakeTimers({ config, global, @@ -132,7 +132,7 @@ describe('FakeTimers', () => { clearImmediate: origClearImmediate, process, setImmediate: origSetImmediate, - } as unknown as NodeJS.Global; + } as unknown as typeof globalThis; const timers = new FakeTimers({ config, global, @@ -146,7 +146,7 @@ describe('FakeTimers', () => { it('does not mock requestAnimationFrame if not available', () => { const global = { process, - } as unknown as NodeJS.Global & Window; + } as unknown as typeof globalThis & Window; const timers = new FakeTimers({ config, global, @@ -162,7 +162,7 @@ describe('FakeTimers', () => { const global = { process, requestAnimationFrame: origRequestAnimationFrame, - } as unknown as NodeJS.Global & Window; + } as unknown as typeof globalThis & Window; const timers = new FakeTimers({ config, global, @@ -177,7 +177,7 @@ describe('FakeTimers', () => { it('does not mock cancelAnimationFrame if not available on global', () => { const global = { process, - } as unknown as NodeJS.Global & Window; + } as unknown as typeof globalThis & Window; const timers = new FakeTimers({ config, global, @@ -193,7 +193,7 @@ describe('FakeTimers', () => { const global = { cancelAnimationFrame: origCancelAnimationFrame, process, - } as unknown as NodeJS.Global & Window; + } as unknown as typeof globalThis & Window; const timers = new FakeTimers({ config, global, @@ -212,7 +212,7 @@ describe('FakeTimers', () => { process: { nextTick: () => {}, }, - } as unknown as NodeJS.Global; + } as unknown as typeof globalThis; const timers = new FakeTimers({ config, @@ -245,7 +245,7 @@ describe('FakeTimers', () => { process: { nextTick, }, - } as unknown as NodeJS.Global; + } as unknown as typeof globalThis; const timers = new FakeTimers({ config, @@ -264,7 +264,7 @@ describe('FakeTimers', () => { process: { nextTick: () => {}, }, - } as unknown as NodeJS.Global; + } as unknown as typeof globalThis; const timers = new FakeTimers({ config, @@ -292,7 +292,7 @@ describe('FakeTimers', () => { process: { nextTick: nativeNextTick, }, - } as unknown as NodeJS.Global; + } as unknown as typeof globalThis; const timers = new FakeTimers({ config, @@ -320,7 +320,7 @@ describe('FakeTimers', () => { const global = { process, setImmediate: nativeSetImmediate, - } as unknown as NodeJS.Global; + } as unknown as typeof globalThis; const timers = new FakeTimers({ config, @@ -348,7 +348,7 @@ describe('FakeTimers', () => { process: { nextTick: nativeNextTick, }, - } as unknown as NodeJS.Global; + } as unknown as typeof globalThis; const timers = new FakeTimers({ config, @@ -376,7 +376,7 @@ describe('FakeTimers', () => { const global = { process, setImmediate: nativeSetImmediate, - } as unknown as NodeJS.Global; + } as unknown as typeof globalThis; const timers = new FakeTimers({ config, @@ -404,7 +404,7 @@ describe('FakeTimers', () => { const global = { process, setImmediate: nativeSetImmediate, - } as unknown as NodeJS.Global; + } as unknown as typeof globalThis; const timers = new FakeTimers({ config, @@ -432,7 +432,7 @@ describe('FakeTimers', () => { process: { nextTick: () => {}, }, - } as unknown as NodeJS.Global; + } as unknown as typeof globalThis; const timers = new FakeTimers({ config, @@ -465,7 +465,7 @@ describe('FakeTimers', () => { cancelAnimationFrame: () => {}, process, requestAnimationFrame: () => {}, - } as unknown as NodeJS.Global & Window; + } as unknown as typeof globalThis & Window; const timers = new FakeTimers({ config, global, @@ -531,7 +531,7 @@ describe('FakeTimers', () => { const global = { process, setTimeout: nativeSetTimeout, - } as unknown as NodeJS.Global; + } as unknown as typeof globalThis; const timers = new FakeTimers({ config, @@ -544,7 +544,7 @@ describe('FakeTimers', () => { }); it('only runs a setTimeout callback once (ever)', () => { - const global = {process} as unknown as NodeJS.Global; + const global = {process} as unknown as typeof globalThis; const timers = new FakeTimers({ config, global, @@ -565,7 +565,7 @@ describe('FakeTimers', () => { }); it('runs callbacks with arguments after the interval', () => { - const global = {process} as unknown as NodeJS.Global; + const global = {process} as unknown as typeof globalThis; const timers = new FakeTimers({ config, global, @@ -588,7 +588,7 @@ describe('FakeTimers', () => { const global = { process, setTimeout: nativeSetTimeout, - } as unknown as NodeJS.Global; + } as unknown as typeof globalThis; const timers = new FakeTimers({ config, @@ -607,7 +607,7 @@ describe('FakeTimers', () => { }); it('throws before allowing infinite recursion', () => { - const global = {process} as unknown as NodeJS.Global; + const global = {process} as unknown as typeof globalThis; const timers = new FakeTimers({ config, global, @@ -632,7 +632,7 @@ describe('FakeTimers', () => { }); it('also clears ticks', () => { - const global = {process} as unknown as NodeJS.Global; + const global = {process} as unknown as typeof globalThis; const timers = new FakeTimers({ config, global, @@ -658,7 +658,7 @@ describe('FakeTimers', () => { cancelAnimationFrame: () => {}, process, requestAnimationFrame: () => {}, - } as unknown as NodeJS.Global & Window; + } as unknown as typeof globalThis & Window; const timers = new FakeTimers({ config, global, @@ -728,7 +728,7 @@ describe('FakeTimers', () => { }); it('does nothing when no timers have been scheduled', () => { - const global = {process} as unknown as NodeJS.Global; + const global = {process} as unknown as typeof globalThis; const timers = new FakeTimers({ config, global, @@ -740,7 +740,7 @@ describe('FakeTimers', () => { timers.advanceTimersByTime(100); }); it('throws before allowing infinite recursion', () => { - const global = {process} as unknown as NodeJS.Global; + const global = {process} as unknown as typeof globalThis; const timers = new FakeTimers({ config, global, @@ -771,7 +771,7 @@ describe('FakeTimers', () => { cancelAnimationFrame: () => {}, process, requestAnimationFrame: () => {}, - } as unknown as NodeJS.Global & Window; + } as unknown as typeof globalThis & Window; const timers = new FakeTimers({ config, global, @@ -834,7 +834,7 @@ describe('FakeTimers', () => { cancelAnimationFrame: () => {}, process, requestAnimationFrame: () => {}, - } as unknown as NodeJS.Global & Window; + } as unknown as typeof globalThis & Window; const timers = new FakeTimers({ config, global, @@ -880,7 +880,7 @@ describe('FakeTimers', () => { }); it('setTimeout inside setTimeout', () => { - const global = {process} as unknown as NodeJS.Global; + const global = {process} as unknown as typeof globalThis; const timers = new FakeTimers({ config, global, @@ -908,7 +908,7 @@ describe('FakeTimers', () => { }); it('does nothing when no timers have been scheduled', () => { - const global = {process} as unknown as NodeJS.Global; + const global = {process} as unknown as typeof globalThis; const timers = new FakeTimers({ config, global, @@ -923,7 +923,7 @@ describe('FakeTimers', () => { describe('reset', () => { it('resets all pending setTimeouts', () => { - const global = {process} as unknown as NodeJS.Global; + const global = {process} as unknown as typeof globalThis; const timers = new FakeTimers({ config, global, @@ -941,7 +941,7 @@ describe('FakeTimers', () => { }); it('resets all pending setIntervals', () => { - const global = {process} as unknown as NodeJS.Global; + const global = {process} as unknown as typeof globalThis; const timers = new FakeTimers({ config, global, @@ -963,7 +963,7 @@ describe('FakeTimers', () => { cancelAnimationFrame: () => {}, process, requestAnimationFrame: () => {}, - } as unknown as NodeJS.Global & Window; + } as unknown as typeof globalThis & Window; const timers = new FakeTimers({ config, global, @@ -986,7 +986,7 @@ describe('FakeTimers', () => { nextTick: () => {}, }, setImmediate: () => {}, - } as unknown as NodeJS.Global; + } as unknown as typeof globalThis; const timers = new FakeTimers({ config, global, @@ -1006,7 +1006,7 @@ describe('FakeTimers', () => { }); it('resets current advanceTimersByTime time cursor', () => { - const global = {process} as unknown as NodeJS.Global; + const global = {process} as unknown as typeof globalThis; const timers = new FakeTimers({ config, global, @@ -1036,7 +1036,7 @@ describe('FakeTimers', () => { process, requestAnimationFrame: () => {}, setImmediate: nativeSetImmediate, - } as unknown as NodeJS.Global & Window; + } as unknown as typeof globalThis & Window; const timers = new FakeTimers({ config, @@ -1104,7 +1104,7 @@ describe('FakeTimers', () => { }); it('does not run timers that were cleared in another timer', () => { - const global = {process} as unknown as NodeJS.Global; + const global = {process} as unknown as typeof globalThis; const timers = new FakeTimers({ config, global, @@ -1137,7 +1137,7 @@ describe('FakeTimers', () => { process, setInterval: nativeSetInterval, setTimeout: nativeSetTimeout, - } as unknown as NodeJS.Global; + } as unknown as typeof globalThis; const timers = new FakeTimers({ config, global, @@ -1187,7 +1187,7 @@ describe('FakeTimers', () => { process, setInterval: nativeSetInterval, setTimeout: nativeSetTimeout, - } as unknown as NodeJS.Global; + } as unknown as typeof globalThis; const timers = new FakeTimers({ config, global, @@ -1246,7 +1246,7 @@ describe('FakeTimers', () => { const global = { process, setTimeout: nativeSetTimeout, - } as unknown as NodeJS.Global; + } as unknown as typeof globalThis; const timers = new FakeTimers({ config, global, @@ -1283,7 +1283,7 @@ describe('FakeTimers', () => { process, setInterval: nativeSetInterval, setTimeout: nativeSetTimeout, - } as unknown as NodeJS.Global; + } as unknown as typeof globalThis; const timers = new FakeTimers({ config, global, @@ -1312,7 +1312,7 @@ describe('FakeTimers', () => { const global = { process: {nextTick: nativeProcessNextTick}, - } as unknown as NodeJS.Global; + } as unknown as typeof globalThis; const timers = new FakeTimers({ config, global, @@ -1338,7 +1338,7 @@ describe('FakeTimers', () => { clearImmediate: nativeClearImmediate, process, setImmediate: nativeSetImmediate, - } as unknown as NodeJS.Global; + } as unknown as typeof globalThis; const timers = new FakeTimers({ config, global, @@ -1366,7 +1366,7 @@ describe('FakeTimers', () => { cancelAnimationFrame: nativeCancelAnimationFrame, process, requestAnimationFrame: nativeRequestAnimationFrame, - } as unknown as NodeJS.Global & Window; + } as unknown as typeof globalThis & Window; const timers = new FakeTimers({ config, global, @@ -1402,7 +1402,7 @@ describe('FakeTimers', () => { process, setInterval: nativeSetInterval, setTimeout: nativeSetTimeout, - } as unknown as NodeJS.Global; + } as unknown as typeof globalThis; const timers = new FakeTimers({ config, global, @@ -1431,7 +1431,7 @@ describe('FakeTimers', () => { const global = { process: {nextTick: nativeProcessNextTick}, - } as unknown as NodeJS.Global; + } as unknown as typeof globalThis; const timers = new FakeTimers({ config, global, @@ -1457,7 +1457,7 @@ describe('FakeTimers', () => { clearImmediate: nativeClearImmediate, process, setImmediate: nativeSetImmediate, - } as unknown as NodeJS.Global; + } as unknown as typeof globalThis; const fakeTimers = new FakeTimers({ config, global, @@ -1485,7 +1485,7 @@ describe('FakeTimers', () => { cancelAnimationFrame: nativeCancelAnimationFrame, process, requestAnimationFrame: nativeRequestAnimationFrame, - } as unknown as NodeJS.Global & Window; + } as unknown as typeof globalThis & Window; const fakeTimers = new FakeTimers({ config, global, @@ -1573,7 +1573,7 @@ describe('FakeTimers', () => { cancelAnimationFrame: () => {}, process, requestAnimationFrame: () => {}, - } as unknown as NodeJS.Global & Window; + } as unknown as typeof globalThis & Window; const timers = new FakeTimers({ config, global, diff --git a/packages/jest-fake-timers/src/legacyFakeTimers.ts b/packages/jest-fake-timers/src/legacyFakeTimers.ts index 1805e8c9b9af..07505acb578f 100644 --- a/packages/jest-fake-timers/src/legacyFakeTimers.ts +++ b/packages/jest-fake-timers/src/legacyFakeTimers.ts @@ -48,9 +48,11 @@ type TimerConfig = { const MS_IN_A_YEAR = 31536000000; -interface FakeTimersGlobal extends NodeJS.Global { - cancelAnimationFrame?: (handle: number) => void; - requestAnimationFrame?: (callback: (time: number) => void) => number; +type GlobalThis = typeof globalThis; + +interface FakeTimersGlobal extends GlobalThis { + cancelAnimationFrame: (handle: number) => void; + requestAnimationFrame: (callback: (time: number) => void) => number; } export default class FakeTimers { diff --git a/packages/jest-fake-timers/src/modernFakeTimers.ts b/packages/jest-fake-timers/src/modernFakeTimers.ts index 80cd292a69be..59c4c49321b4 100644 --- a/packages/jest-fake-timers/src/modernFakeTimers.ts +++ b/packages/jest-fake-timers/src/modernFakeTimers.ts @@ -16,7 +16,7 @@ export default class FakeTimers { private _clock!: InstalledClock; private _config: StackTraceConfig; private _fakingTime: boolean; - private _global: NodeJS.Global; + private _global: typeof globalThis; private _fakeTimers: FakeTimerWithContext; private _maxLoops: number; @@ -25,7 +25,7 @@ export default class FakeTimers { config, maxLoops, }: { - global: NodeJS.Global; + global: typeof globalThis; config: StackTraceConfig; maxLoops?: number; }) { diff --git a/packages/jest-jasmine2/src/jasmine/Env.ts b/packages/jest-jasmine2/src/jasmine/Env.ts index 4f141bb6b540..fccc7df35980 100644 --- a/packages/jest-jasmine2/src/jasmine/Env.ts +++ b/packages/jest-jasmine2/src/jasmine/Env.ts @@ -104,8 +104,10 @@ export default function (j$: Jasmine) { let catchExceptions = true; - const realSetTimeout = global.setTimeout; - const realClearTimeout = global.clearTimeout; + const realSetTimeout = + global.setTimeout as typeof globalThis['setTimeout']; + const realClearTimeout = + global.clearTimeout as typeof globalThis['clearTimeout']; const runnableResources: Record}> = {}; const currentlyExecutingSuites: Array = []; diff --git a/packages/jest-jasmine2/src/jestExpect.ts b/packages/jest-jasmine2/src/jestExpect.ts index 7b2b105d36f3..6b586a2fbeb2 100644 --- a/packages/jest-jasmine2/src/jestExpect.ts +++ b/packages/jest-jasmine2/src/jestExpect.ts @@ -64,7 +64,6 @@ export default (config: {expand: boolean}): void => { }; }); - const expect = global.expect; expect.extend(jestMatchersObject); }; }; diff --git a/packages/jest-jasmine2/src/pTimeout.ts b/packages/jest-jasmine2/src/pTimeout.ts index d982cf732d4b..0df9e0132581 100644 --- a/packages/jest-jasmine2/src/pTimeout.ts +++ b/packages/jest-jasmine2/src/pTimeout.ts @@ -10,8 +10,8 @@ export default function pTimeout( promise: Promise, ms: number, - clearTimeout: NodeJS.Global['clearTimeout'], - setTimeout: NodeJS.Global['setTimeout'], + clearTimeout: typeof globalThis['clearTimeout'], + setTimeout: typeof globalThis['setTimeout'], onTimeout: () => void, ): Promise { return new Promise((resolve, reject) => { diff --git a/packages/jest-jasmine2/src/queueRunner.ts b/packages/jest-jasmine2/src/queueRunner.ts index 4dcf02f15e17..8e70cd625434 100644 --- a/packages/jest-jasmine2/src/queueRunner.ts +++ b/packages/jest-jasmine2/src/queueRunner.ts @@ -9,14 +9,12 @@ import {formatTime} from 'jest-util'; import PCancelable from './PCancelable'; import pTimeout from './pTimeout'; -type Global = NodeJS.Global; - export type Options = { - clearTimeout: Global['clearTimeout']; + clearTimeout: typeof globalThis['clearTimeout']; fail: (error: Error) => void; onException: (error: Error) => void; queueableFns: Array; - setTimeout: Global['setTimeout']; + setTimeout: typeof globalThis['setTimeout']; userContext: unknown; }; diff --git a/packages/jest-jasmine2/src/types.ts b/packages/jest-jasmine2/src/types.ts index f6832a815293..bc131dedaf56 100644 --- a/packages/jest-jasmine2/src/types.ts +++ b/packages/jest-jasmine2/src/types.ts @@ -90,7 +90,7 @@ export type Jasmine = { testPath: Config.Path; addMatchers: (matchers: JasmineMatchersObject) => void; } & typeof expect & - NodeJS.Global; + typeof globalThis; declare global { module NodeJS { diff --git a/packages/jest-mock/src/__tests__/index.test.ts b/packages/jest-mock/src/__tests__/index.test.ts index ddd6af4f1fba..854d79a1c7b7 100644 --- a/packages/jest-mock/src/__tests__/index.test.ts +++ b/packages/jest-mock/src/__tests__/index.test.ts @@ -14,7 +14,7 @@ import {ModuleMocker, fn, spyOn} from '../'; describe('moduleMocker', () => { let moduleMocker: ModuleMocker; let mockContext: Context; - let mockGlobals: NodeJS.Global; + let mockGlobals: typeof globalThis; beforeEach(() => { mockContext = vm.createContext(); diff --git a/packages/jest-mock/src/index.ts b/packages/jest-mock/src/index.ts index 885bf2ecf2d4..e92b0b3eb80c 100644 --- a/packages/jest-mock/src/index.ts +++ b/packages/jest-mock/src/index.ts @@ -7,8 +7,6 @@ /* eslint-disable local/ban-types-eventually, local/prefer-rest-params-eventually */ -type Global = NodeJS.Global; // | Window – add once TS improves typings; - export type MockFunctionMetadataType = | 'object' | 'array' @@ -364,7 +362,7 @@ function isReadonlyProp(object: any, prop: string): boolean { } export class ModuleMocker { - private _environmentGlobal: Global; + private _environmentGlobal: typeof globalThis; private _mockState: WeakMap, MockFunctionState>; private _mockConfigRegistry: WeakMap; private _spyState: Set<() => void>; @@ -375,7 +373,7 @@ export class ModuleMocker { * @param global Global object of the test environment, used to create * mocks */ - constructor(global: Global) { + constructor(global: typeof globalThis) { this._environmentGlobal = global; this._mockState = new WeakMap(); this._mockConfigRegistry = new WeakMap(); @@ -1113,7 +1111,7 @@ export class ModuleMocker { } } -const JestMock = new ModuleMocker(global); +const JestMock = new ModuleMocker(global as unknown as typeof globalThis); export const fn = JestMock.fn.bind(JestMock); export const spyOn = JestMock.spyOn.bind(JestMock); diff --git a/packages/jest-repl/src/cli/runtime-cli.ts b/packages/jest-repl/src/cli/runtime-cli.ts index 3c29fe7ef145..619b77daf8b9 100644 --- a/packages/jest-repl/src/cli/runtime-cli.ts +++ b/packages/jest-repl/src/cli/runtime-cli.ts @@ -80,12 +80,20 @@ export async function run( const environment = new Environment(config); setGlobal( - environment.global, + environment.global as unknown as typeof globalThis, 'console', new CustomConsole(process.stdout, process.stderr), ); - setGlobal(environment.global, 'jestProjectConfig', config); - setGlobal(environment.global, 'jestGlobalConfig', globalConfig); + setGlobal( + environment.global as unknown as typeof globalThis, + 'jestProjectConfig', + config, + ); + setGlobal( + environment.global as unknown as typeof globalThis, + 'jestGlobalConfig', + globalConfig, + ); const runtime = new Runtime( config, diff --git a/packages/jest-runner/src/runTest.ts b/packages/jest-runner/src/runTest.ts index 2d8d5432f46e..cd855860a98a 100644 --- a/packages/jest-runner/src/runTest.ts +++ b/packages/jest-runner/src/runTest.ts @@ -156,7 +156,11 @@ async function runTestInternal( ? new LeakDetector(environment) : null; - setGlobal(environment.global, 'console', testConsole); + setGlobal( + environment.global as unknown as typeof globalThis, + 'console', + testConsole, + ); const runtime = new Runtime( config, diff --git a/packages/jest-runtime/src/index.ts b/packages/jest-runtime/src/index.ts index cbe95fd67bde..26387c639e3e 100644 --- a/packages/jest-runtime/src/index.ts +++ b/packages/jest-runtime/src/index.ts @@ -929,16 +929,18 @@ export default class Runtime { if (this._environment) { if (this._environment.global) { const envGlobal = this._environment.global; - (Object.keys(envGlobal) as Array).forEach(key => { - const globalMock = envGlobal[key]; - if ( - ((typeof globalMock === 'object' && globalMock !== null) || - typeof globalMock === 'function') && - globalMock._isMockFunction === true - ) { - globalMock.mockClear(); - } - }); + (Object.keys(envGlobal) as Array).forEach( + key => { + const globalMock = envGlobal[key]; + if ( + ((typeof globalMock === 'object' && globalMock !== null) || + typeof globalMock === 'function') && + globalMock._isMockFunction === true + ) { + globalMock.mockClear(); + } + }, + ); } if (this._environment.fakeTimers) { diff --git a/packages/jest-types/src/Config.ts b/packages/jest-types/src/Config.ts index 42b2487c620b..abda0fb3c200 100644 --- a/packages/jest-types/src/Config.ts +++ b/packages/jest-types/src/Config.ts @@ -350,7 +350,7 @@ export type ProjectConfig = { displayName?: DisplayName; errorOnDeprecated: boolean; extensionsToTreatAsEsm: Array; - extraGlobals: Array; + extraGlobals: Array; filter?: Path; forceCoverageMatch: Array; globalSetup?: string; diff --git a/packages/jest-types/src/Global.ts b/packages/jest-types/src/Global.ts index 46a4d0669b96..010624038707 100644 --- a/packages/jest-types/src/Global.ts +++ b/packages/jest-types/src/Global.ts @@ -133,7 +133,6 @@ export interface GlobalAdditions extends TestFrameworkGlobals { export interface Global extends GlobalAdditions, - // TODO: Maybe add `| Window` in the future? - Omit { + Omit { [extras: string]: unknown; } diff --git a/packages/jest-util/src/__tests__/installCommonGlobals.test.ts b/packages/jest-util/src/__tests__/installCommonGlobals.test.ts index 6bde7264523e..f6a332584c4d 100644 --- a/packages/jest-util/src/__tests__/installCommonGlobals.test.ts +++ b/packages/jest-util/src/__tests__/installCommonGlobals.test.ts @@ -10,7 +10,7 @@ import {createContext, runInContext} from 'vm'; let installCommonGlobals: typeof import('../installCommonGlobals').default; let fake: jest.Mock; -function getGlobal(): NodeJS.Global { +function getGlobal(): typeof globalThis { return runInContext('this', createContext()); } diff --git a/packages/jest-util/src/installCommonGlobals.ts b/packages/jest-util/src/installCommonGlobals.ts index 90520a13d91c..5931f672e3e8 100644 --- a/packages/jest-util/src/installCommonGlobals.ts +++ b/packages/jest-util/src/installCommonGlobals.ts @@ -13,9 +13,9 @@ import deepCyclicCopy from './deepCyclicCopy'; const DTRACE = Object.keys(global).filter(key => key.startsWith('DTRACE')); export default function ( - globalObject: NodeJS.Global, + globalObject: typeof globalThis, globals: Config.ConfigGlobals, -): NodeJS.Global & Config.ConfigGlobals { +): typeof globalThis & Config.ConfigGlobals { globalObject.process = createProcessObject(); const symbol = globalObject.Symbol as unknown as SymbolConstructor; diff --git a/packages/jest-util/src/setGlobal.ts b/packages/jest-util/src/setGlobal.ts index f840a7f4bae4..bbce818ddac0 100644 --- a/packages/jest-util/src/setGlobal.ts +++ b/packages/jest-util/src/setGlobal.ts @@ -6,7 +6,7 @@ */ export default ( - globalToMutate: NodeJS.Global | Window, + globalToMutate: typeof globalThis, key: string, value: unknown, ): void => { diff --git a/packages/jest-worker/src/types.ts b/packages/jest-worker/src/types.ts index 7211d3d93993..3022faf3c93f 100644 --- a/packages/jest-worker/src/types.ts +++ b/packages/jest-worker/src/types.ts @@ -14,6 +14,7 @@ export interface ResourceLimits { maxYoungGenerationSizeMb?: number; maxOldGenerationSizeMb?: number; codeRangeSizeMb?: number; + stackSizeMb?: number; } // Because of the dynamic nature of a worker communication process, all messages diff --git a/packages/jest-worker/src/workers/NodeThreadsWorker.ts b/packages/jest-worker/src/workers/NodeThreadsWorker.ts index 0d13ce958dd4..efd5d60af57c 100644 --- a/packages/jest-worker/src/workers/NodeThreadsWorker.ts +++ b/packages/jest-worker/src/workers/NodeThreadsWorker.ts @@ -7,7 +7,10 @@ import * as path from 'path'; import {PassThrough} from 'stream'; -import {Worker} from 'worker_threads'; +import { + Worker, + WorkerOptions as WorkerThreadsWorkerOptions, +} from 'worker_threads'; import mergeStream = require('merge-stream'); import { CHILD_MESSAGE_INITIALIZE, @@ -61,7 +64,6 @@ export default class ExperimentalWorker implements WorkerInterface { initialize(): void { this._worker = new Worker(path.resolve(__dirname, './threadChild.js'), { eval: false, - // @ts-expect-error: added in newer versions resourceLimits: this._options.resourceLimits, stderr: true, stdout: true, @@ -76,7 +78,7 @@ export default class ExperimentalWorker implements WorkerInterface { silent: true, ...this._options.forkOptions, }, - }); + } as WorkerThreadsWorkerOptions); if (this._worker.stdout) { if (!this._stdout) { diff --git a/packages/jest-worker/src/workers/processChild.ts b/packages/jest-worker/src/workers/processChild.ts index 64d29e19e132..71f83a1c0742 100644 --- a/packages/jest-worker/src/workers/processChild.ts +++ b/packages/jest-worker/src/workers/processChild.ts @@ -34,7 +34,7 @@ let initialized = false; * If an invalid message is detected, the child will exit (by throwing) with a * non-zero exit code. */ -const messageListener: NodeJS.MessageListener = request => { +const messageListener: NodeJS.MessageListener = (request: any) => { switch (request[0]) { case CHILD_MESSAGE_INITIALIZE: const init: ChildMessageInitialize = request;