Skip to content

Commit

Permalink
fix(jest-types): compat with @types/node v16
Browse files Browse the repository at this point in the history
  • Loading branch information
splincode authored and splincode committed Aug 24, 2021
1 parent d38156c commit c5c7105
Show file tree
Hide file tree
Showing 26 changed files with 134 additions and 118 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

### Fixes

- `[jest-types]` Compat with `@types/node` v16 ([#11645](https://github.com/facebook/jest/pull/11645))
- `[jest-environment-jsdom]` Add support for `userAgent` option ([#11773](https://github.com/facebook/jest/pull/11773))
- `[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)
Expand Down
6 changes: 3 additions & 3 deletions packages/jest-console/src/BufferedConsole.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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({
Expand Down Expand Up @@ -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));
}
Expand Down
6 changes: 3 additions & 3 deletions packages/jest-console/src/CustomConsole.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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,
Expand Down Expand Up @@ -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));
}
Expand Down
8 changes: 4 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,11 @@ 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
14 changes: 8 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 Down Expand Up @@ -51,15 +51,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 @@ -101,12 +100,15 @@ 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<Timer = unknown> {
constructor(config: Config.ProjectConfig, context?: EnvironmentContext);
global: Global.Global;
fakeTimers: LegacyFakeTimers<unknown> | null;
fakeTimers: LegacyFakeTimers<Timer> | null;
fakeTimersModern: ModernFakeTimers | null;
moduleMocker: ModuleMocker | null;
getVmContext(): Context | null;
Expand Down
Loading

0 comments on commit c5c7105

Please sign in to comment.