From fb691f7213ca148f35f7062ab47465ab3ffc2d5e Mon Sep 17 00:00:00 2001 From: Colin Rotherham Date: Mon, 27 Mar 2023 12:23:24 +0100 Subject: [PATCH] fix(types): fix environment global types PuppeteerEnvironment global is now typed with page/browser globals etc --- packages/jest-environment-puppeteer/src/env.ts | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/packages/jest-environment-puppeteer/src/env.ts b/packages/jest-environment-puppeteer/src/env.ts index 7992fe94..6084bb36 100644 --- a/packages/jest-environment-puppeteer/src/env.ts +++ b/packages/jest-environment-puppeteer/src/env.ts @@ -1,4 +1,8 @@ /* eslint-disable no-debugger */ +import type { + EnvironmentContext, + JestEnvironmentConfig, +} from "@jest/environment"; import NodeEnvironment from "jest-environment-node"; import { readConfig } from "./config"; import { blockStdin } from "./stdin"; @@ -126,6 +130,15 @@ const closeAll = async (global: StrictGlobal) => { }; export class PuppeteerEnvironment extends NodeEnvironment { + declare global: StrictGlobal & NodeEnvironment["global"]; + + constructor(config: JestEnvironmentConfig, _context: EnvironmentContext) { + super(config, _context); + + this.global.puppeteerConfig = {}; + this.global.jestPuppeteer = {}; + } + // Jest is not available here, so we have to reverse engineer // the setTimeout function, see https://github.com/facebook/jest/blob/ffe2352c781703b427fab10777043fb76d0d4267/packages/jest-runtime/src/index.ts#L2331 setTimeout(timeout: number) { @@ -134,7 +147,7 @@ export class PuppeteerEnvironment extends NodeEnvironment { async setup(): Promise { const config = await readConfig(); - const global = this.global as unknown as StrictGlobal; + const global = this.global; global.puppeteerConfig = config; global.jestPuppeteer = { @@ -161,7 +174,6 @@ export class PuppeteerEnvironment extends NodeEnvironment { } async teardown() { - const global = this.global as unknown as StrictGlobal; - await closeAll(global); + await closeAll(this.global); } }