Skip to content

Commit

Permalink
fix(jest-types): support build with Node.js 16
Browse files Browse the repository at this point in the history
  • Loading branch information
splincode committed Aug 24, 2021
1 parent 3652958 commit db5ecbc
Show file tree
Hide file tree
Showing 12 changed files with 25 additions and 83 deletions.
52 changes: 0 additions & 52 deletions e2e/__tests__/consoleLogOutputWhenRunInBand.test.ts

This file was deleted.

7 changes: 3 additions & 4 deletions packages/jest-each/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,8 @@
import type {Global} from '@jest/types';
import bind from './bind';

type Global = Global.Global;
const install = (
g: Global,
g: typeof globalThis,
table: Global.EachTable,
...data: Global.TemplateData
) => {
Expand Down Expand Up @@ -69,10 +68,10 @@ const install = (
const each = (
table: Global.EachTable,
...data: Global.TemplateData
): ReturnType<typeof install> => install(global as Global, table, ...data);
): ReturnType<typeof install> => install(global as unknown as typeof globalThis, table, ...data);

each.withGlobal =
(g: Global) =>
(g: typeof globalThis) =>
(table: Global.EachTable, ...data: Global.TemplateData) =>
install(g, table, ...data);

Expand Down
11 changes: 5 additions & 6 deletions packages/jest-environment-jsdom/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ type Win = Window &
};
};

class JSDOMEnvironment implements JestEnvironment {
class JSDOMEnvironment implements JestEnvironment<number> {
dom: JSDOM | null;
fakeTimers: LegacyFakeTimers<number> | null;
fakeTimersModern: ModernFakeTimers | null;
Expand All @@ -45,15 +45,14 @@ class JSDOMEnvironment implements JestEnvironment {
throw new Error('JSDOM did not return a Window object');
}

// 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.
Expand Down Expand Up @@ -95,12 +94,12 @@ class JSDOMEnvironment implements JestEnvironment {

this.fakeTimers = new LegacyFakeTimers({
config,
global,
global: global as any as typeof globalThis,
moduleMocker: this.moduleMocker,
timerConfig,
});

this.fakeTimersModern = new ModernFakeTimers({config, global});
this.fakeTimersModern = new ModernFakeTimers({config, global: global as any as typeof globalThis});
}

async setup(): Promise<void> {}
Expand Down
4 changes: 2 additions & 2 deletions packages/jest-environment/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,10 @@ export type ModuleWrapper = (
...extraGlobals: Array<Global.Global[keyof Global.Global]>
) => unknown;

export declare class JestEnvironment {
export declare class JestEnvironment<T = unknown> {
constructor(config: Config.ProjectConfig, context?: EnvironmentContext);
global: Global.Global;
fakeTimers: LegacyFakeTimers<unknown> | null;
fakeTimers: LegacyFakeTimers<T> | null;
fakeTimersModern: ModernFakeTimers | null;
moduleMocker: ModuleMocker | null;
getVmContext(): Context | null;
Expand Down
4 changes: 2 additions & 2 deletions packages/jest-jasmine2/src/jasmine/Env.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,8 @@ 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<string, {spies: Array<Spy>}> = {};
const currentlyExecutingSuites: Array<Suite> = [];
Expand Down
2 changes: 1 addition & 1 deletion packages/jest-jasmine2/src/jestExpect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ export default (config: {expand: boolean}): void => {
};
});

const expect = global.expect;
const expect = global.expect as any;
expect.extend(jestMatchersObject);
};
};
6 changes: 2 additions & 4 deletions packages/jest-jasmine2/src/queueRunner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,12 @@ import {formatTime} from 'jest-util';
import PCancelable from './PCancelable';
import pTimeout from './pTimeout';

type Global = typeof globalThis;

export type Options = {
clearTimeout: Global['clearTimeout'];
clearTimeout: typeof globalThis['clearTimeout'];
fail: (error: Error) => void;
onException: (error: Error) => void;
queueableFns: Array<QueueableFn>;
setTimeout: Global['setTimeout'];
setTimeout: typeof globalThis['setTimeout'];
userContext: unknown;
};

Expand Down
10 changes: 4 additions & 6 deletions packages/jest-mock/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@
* LICENSE file in the root directory of this source tree.
*/

/* eslint-disable local/ban-types-eventually, local/prefer-rest-params-eventually */

type Global = typeof globalThis; // | Window – add once TS improves typings;
/* eslint-disable local/prefer-rest-params-eventually */

export type MockFunctionMetadataType =
| 'object'
Expand Down Expand Up @@ -364,7 +362,7 @@ function isReadonlyProp(object: any, prop: string): boolean {
}

export class ModuleMocker {
private _environmentGlobal: Global;
private _environmentGlobal: typeof globalThis;
private _mockState: WeakMap<Mock<any, any>, MockFunctionState<any, any>>;
private _mockConfigRegistry: WeakMap<Function, MockFunctionConfig>;
private _spyState: Set<() => void>;
Expand All @@ -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();
Expand Down Expand Up @@ -1110,7 +1108,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);
6 changes: 3 additions & 3 deletions packages/jest-repl/src/cli/runtime-cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,12 +80,12 @@ 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,
Expand Down
2 changes: 1 addition & 1 deletion packages/jest-runner/src/runTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ 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,
Expand Down
1 change: 1 addition & 0 deletions packages/jest-worker/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 1 addition & 2 deletions packages/jest-worker/src/workers/NodeThreadsWorker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,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,
Expand All @@ -76,7 +75,7 @@ export default class ExperimentalWorker implements WorkerInterface {
silent: true,
...this._options.forkOptions,
},
});
} as import('worker_threads').WorkerOptions);

if (this._worker.stdout) {
if (!this._stdout) {
Expand Down

0 comments on commit db5ecbc

Please sign in to comment.