diff --git a/README.md b/README.md index 69714932..2c70d5a9 100644 --- a/README.md +++ b/README.md @@ -147,7 +147,7 @@ module.exports = { ### Configure ESLint -Jest Puppeteer exposes three new globals: `browser`, `page`, `context`. If you want to avoid errors, you can add them to your `.eslintrc.js`: +Jest Puppeteer exposes five globals: `browser`, `page`, `context`, `puppeteerConfig` and `jestPuppeteer`. If you want to avoid errors, you can add them in your ESLint config: ```js // .eslintrc.js @@ -159,6 +159,7 @@ module.exports = { page: true, browser: true, context: true, + puppeteerConfig: true, jestPuppeteer: true, }, }; diff --git a/packages/jest-environment-puppeteer/package.json b/packages/jest-environment-puppeteer/package.json index 79c74b35..81c41d01 100644 --- a/packages/jest-environment-puppeteer/package.json +++ b/packages/jest-environment-puppeteer/package.json @@ -10,10 +10,6 @@ "types": "./dist/index.d.ts", "default": "./dist/index.js" }, - "./globals": { - "types": "./dist/globals.d.ts", - "default": "./dist/globals.js" - }, "./setup": "./setup.js", "./teardown": "./teardown.js", "./package.json": "./package.json" diff --git a/packages/jest-environment-puppeteer/rollup.config.mjs b/packages/jest-environment-puppeteer/rollup.config.mjs index 4b6fbfa4..23ae2be7 100644 --- a/packages/jest-environment-puppeteer/rollup.config.mjs +++ b/packages/jest-environment-puppeteer/rollup.config.mjs @@ -30,24 +30,6 @@ export default [ }, plugins: [swcPlugin], }), - bundle({ - input: "src/globals.ts", - output: { - file: "dist/globals.js", - format: "cjs", - interop: "compat", - }, - plugins: [swcPlugin], - }), - bundle({ - input: "src/globals.ts", - plugins: [dts()], - output: { - file: "dist/globals.d.ts", - format: "es", - exports: "default", - }, - }), bundle({ output: { file: "dist/index.js", diff --git a/packages/jest-environment-puppeteer/src/env.ts b/packages/jest-environment-puppeteer/src/env.ts index 6b99f211..df8ca7a8 100644 --- a/packages/jest-environment-puppeteer/src/env.ts +++ b/packages/jest-environment-puppeteer/src/env.ts @@ -4,7 +4,37 @@ import { mkdir } from "node:fs/promises"; import { readConfig } from "./config"; import { blockStdin } from "./stdin"; import { connectBrowserFromWorker } from "./browsers"; -import type { JestPuppeteerGlobal } from "./globals"; +import type { JestPuppeteerConfig } from "./config"; +import type { Page, BrowserContext, Browser } from "puppeteer"; + +type JestPuppeteer = { + debug: () => Promise; + resetPage: () => Promise; + resetBrowser: () => Promise; +}; + +type StrictGlobal = { + browser?: Browser | undefined; + page?: Page | undefined; + context?: BrowserContext | undefined; + puppeteerConfig: JestPuppeteerConfig; + jestPuppeteer: JestPuppeteer; +}; + +export type JestPuppeteerGlobal = Required; + +declare global { + // @ts-ignore + var browser: Global["browser"]; + // @ts-ignore + var page: Global["page"]; + // @ts-ignore + var context: Global["context"]; + // @ts-ignore + var puppeteerConfig: Global["puppeteerConfig"]; + // @ts-ignore + var jestPuppeteer: Global["jestPuppeteer"]; +} const testTimeoutSymbol = Symbol.for("TEST_TIMEOUT_SYMBOL"); @@ -12,41 +42,41 @@ const handlePageError = (error: Error) => { process.emit("uncaughtException", error); }; -const getBrowser = (global: JestPuppeteerGlobal) => { +const getBrowser = (global: StrictGlobal) => { if (!global.browser) { throw new Error("Cannot access browser before launching browser."); } return global.browser; }; -const getContext = (global: JestPuppeteerGlobal) => { +const getContext = (global: StrictGlobal) => { if (!global.context) { throw new Error("Cannot access context before launching context."); } return global.context; }; -const connectBrowser = async (global: JestPuppeteerGlobal) => { +const connectBrowser = async (global: StrictGlobal) => { if (global.browser) { throw new Error("Cannot connect browser before closing previous browser."); } global.browser = await connectBrowserFromWorker(global.puppeteerConfig); }; -const disconnectBrowser = async (global: JestPuppeteerGlobal) => { +const disconnectBrowser = async (global: StrictGlobal) => { if (!global.browser) return; await global.browser.disconnect(); global.browser = undefined; }; -const getPage = (global: JestPuppeteerGlobal) => { +const getPage = (global: StrictGlobal) => { if (!global.page) { throw new Error("Cannot access page before launching browser."); } return global.page; }; -const openPage = async (global: JestPuppeteerGlobal) => { +const openPage = async (global: StrictGlobal) => { if (global.page) { throw new Error("Cannot open page before closing previous page."); } @@ -57,7 +87,7 @@ const openPage = async (global: JestPuppeteerGlobal) => { global.page = page; }; -const closePage = async (global: JestPuppeteerGlobal) => { +const closePage = async (global: StrictGlobal) => { if (!global.page) return; if (global.puppeteerConfig.exitOnPageError) { global.page.off("pageerror", handlePageError); @@ -68,7 +98,7 @@ const closePage = async (global: JestPuppeteerGlobal) => { global.page = undefined; }; -const createContext = async (global: JestPuppeteerGlobal) => { +const createContext = async (global: StrictGlobal) => { if (global.context) { throw new Error("Cannot create context before closing previous context."); } @@ -89,7 +119,7 @@ const createContext = async (global: JestPuppeteerGlobal) => { } }; -const closeContext = async (global: JestPuppeteerGlobal) => { +const closeContext = async (global: StrictGlobal) => { if (!global.context) return; if (global.context.isIncognito()) { await global.context.close(); @@ -97,13 +127,13 @@ const closeContext = async (global: JestPuppeteerGlobal) => { global.context = undefined; }; -const initAll = async (global: JestPuppeteerGlobal) => { +const initAll = async (global: StrictGlobal) => { await connectBrowser(global); await createContext(global); await openPage(global); }; -const closeAll = async (global: JestPuppeteerGlobal) => { +const closeAll = async (global: StrictGlobal) => { await closePage(global); await closeContext(global); await disconnectBrowser(global); @@ -118,7 +148,7 @@ export class PuppeteerEnvironment extends NodeEnvironment { async setup(): Promise { const config = await readConfig(); - const global = this.global as unknown as JestPuppeteerGlobal; + const global = this.global as unknown as StrictGlobal; global.puppeteerConfig = config; global.jestPuppeteer = { @@ -148,7 +178,7 @@ export class PuppeteerEnvironment extends NodeEnvironment { } async teardown() { - const global = this.global as unknown as JestPuppeteerGlobal; + const global = this.global as unknown as StrictGlobal; await closeAll(global); } } diff --git a/packages/jest-environment-puppeteer/src/globals.ts b/packages/jest-environment-puppeteer/src/globals.ts deleted file mode 100644 index 39b731b3..00000000 --- a/packages/jest-environment-puppeteer/src/globals.ts +++ /dev/null @@ -1,40 +0,0 @@ -import type { JestPuppeteerConfig } from "./config"; -import type { Page, BrowserContext, Browser } from "puppeteer"; - -export type JestPuppeteer = { - debug: () => Promise; - resetPage: () => Promise; - resetBrowser: () => Promise; -}; - -export type JestPuppeteerGlobal = { - browser?: Browser | undefined; - page?: Page | undefined; - context?: BrowserContext | undefined; - puppeteerConfig: JestPuppeteerConfig; - jestPuppeteer: JestPuppeteer; -}; - -type Global = typeof globalThis & { - browser: Browser; - page: Page; - context: BrowserContext; - puppeteerConfig: JestPuppeteerConfig; - jestPuppeteer: JestPuppeteer; -}; - -declare global { - const browser: Browser; - const page: Page; - const context: BrowserContext; - const puppeteerConfig: JestPuppeteerConfig; - const jestPuppeteer: JestPuppeteer; -} - -const global = globalThis as Global; - -export const browser = global.browser; -export const page = global.page; -export const context = global.context; -export const puppeteerConfig: JestPuppeteerConfig = global.puppeteerConfig; -export const jestPuppeteer = global.jestPuppeteer; diff --git a/packages/jest-environment-puppeteer/src/index.ts b/packages/jest-environment-puppeteer/src/index.ts index 57f9679d..fd29313e 100644 --- a/packages/jest-environment-puppeteer/src/index.ts +++ b/packages/jest-environment-puppeteer/src/index.ts @@ -1,2 +1,3 @@ export { PuppeteerEnvironment as default } from "./env"; +export type { JestPuppeteerGlobal } from "./env"; export type { JestPuppeteerConfig } from "./config"; diff --git a/packages/jest-environment-puppeteer/tests/.eslintrc.js b/packages/jest-environment-puppeteer/tests/.eslintrc.js index 17eb17c9..1f6eeb00 100644 --- a/packages/jest-environment-puppeteer/tests/.eslintrc.js +++ b/packages/jest-environment-puppeteer/tests/.eslintrc.js @@ -7,5 +7,7 @@ module.exports = { page: true, browser: true, context: true, + puppeteerConfig: true, + jestPuppeteer: true, }, }; diff --git a/packages/jest-environment-puppeteer/tests/basic.test.ts b/packages/jest-environment-puppeteer/tests/basic.test.ts index 220679ae..6a5b039d 100644 --- a/packages/jest-environment-puppeteer/tests/basic.test.ts +++ b/packages/jest-environment-puppeteer/tests/basic.test.ts @@ -1,5 +1,3 @@ -import { describe, beforeAll, it, expect } from "@jest/globals"; - describe("Basic", () => { beforeAll(async () => { await page.goto(`http://localhost:${process.env.TEST_SERVER_PORT}`); diff --git a/packages/jest-environment-puppeteer/tests/browserContext-1.test.ts b/packages/jest-environment-puppeteer/tests/browserContext-1.test.ts index 851d9daf..1b587f54 100644 --- a/packages/jest-environment-puppeteer/tests/browserContext-1.test.ts +++ b/packages/jest-environment-puppeteer/tests/browserContext-1.test.ts @@ -1,5 +1,3 @@ -import { describe, it, expect } from "@jest/globals"; - describe("browserContext", () => { const test = process.env.INCOGNITO ? it : it.skip; test("incognito should isolate cookies (part 1)", async () => { diff --git a/packages/jest-environment-puppeteer/tests/browserContext-2.test.ts b/packages/jest-environment-puppeteer/tests/browserContext-2.test.ts index e19a80cb..ccf0e089 100644 --- a/packages/jest-environment-puppeteer/tests/browserContext-2.test.ts +++ b/packages/jest-environment-puppeteer/tests/browserContext-2.test.ts @@ -1,5 +1,3 @@ -import { describe, it, expect } from "@jest/globals"; - describe("browserContext", () => { const test = process.env.INCOGNITO ? it : it.skip; test("incognito should isolate cookies (part 2)", async () => { diff --git a/packages/jest-environment-puppeteer/tests/config.test.ts b/packages/jest-environment-puppeteer/tests/config.test.ts index 388e4257..82021ff7 100644 --- a/packages/jest-environment-puppeteer/tests/config.test.ts +++ b/packages/jest-environment-puppeteer/tests/config.test.ts @@ -1,5 +1,3 @@ -import { describe, it, expect, beforeEach } from "@jest/globals"; - import * as path from "node:path"; import { readConfig } from "../src/config"; diff --git a/packages/jest-puppeteer/jest-preset.js b/packages/jest-puppeteer/jest-preset.js index 90473cad..5d07e643 100644 --- a/packages/jest-puppeteer/jest-preset.js +++ b/packages/jest-puppeteer/jest-preset.js @@ -1,6 +1 @@ -module.exports = { - globalSetup: require.resolve("jest-environment-puppeteer/setup"), - globalTeardown: require.resolve("jest-environment-puppeteer/teardown"), - testEnvironment: require.resolve("jest-environment-puppeteer"), - setupFilesAfterEnv: [require.resolve("expect-puppeteer")], -}; +module.exports = require("./dist/index.js"); diff --git a/packages/jest-puppeteer/package.json b/packages/jest-puppeteer/package.json index 3f3fa075..4d6ff53f 100644 --- a/packages/jest-puppeteer/package.json +++ b/packages/jest-puppeteer/package.json @@ -3,13 +3,12 @@ "description": "Run your tests using Jest & Puppeteer.", "version": "8.0.2", "type": "commonjs", + "main": "./dist/index.js", + "types": "./dist/index.d.ts", "exports": { ".": { - "types": "./dist/globals.d.ts" - }, - "./globals": { - "default": "./dist/globals.js", - "types": "./dist/globals.d.ts" + "types": "./dist/index.d.ts", + "default": "./dist/index.js" }, "./package.json": "./package.json" }, diff --git a/packages/jest-puppeteer/rollup.config.mjs b/packages/jest-puppeteer/rollup.config.mjs index c5cb70a2..d2cd7a44 100644 --- a/packages/jest-puppeteer/rollup.config.mjs +++ b/packages/jest-puppeteer/rollup.config.mjs @@ -21,18 +21,18 @@ const swcPlugin = swc( export default [ bundle({ - input: "src/globals.ts", + input: "src/index.ts", output: { - file: "dist/globals.js", + file: "dist/index.js", format: "cjs", }, plugins: [swcPlugin], }), bundle({ - input: "src/globals.ts", + input: "src/index.ts", plugins: [dts()], output: { - file: "dist/globals.d.ts", + file: "dist/index.d.ts", format: "es", }, }), diff --git a/packages/jest-puppeteer/src/globals.ts b/packages/jest-puppeteer/src/globals.ts deleted file mode 100644 index 6fa1bbe0..00000000 --- a/packages/jest-puppeteer/src/globals.ts +++ /dev/null @@ -1 +0,0 @@ -export * from "jest-environment-puppeteer/globals"; diff --git a/packages/jest-puppeteer/src/index.ts b/packages/jest-puppeteer/src/index.ts new file mode 100644 index 00000000..b51b1aa9 --- /dev/null +++ b/packages/jest-puppeteer/src/index.ts @@ -0,0 +1,21 @@ +import type { JestPuppeteerGlobal } from "jest-environment-puppeteer"; + +declare global { + // @ts-ignore + var browser: JestPuppeteerGlobal["browser"]; + // @ts-ignore + var page: JestPuppeteerGlobal["page"]; + // @ts-ignore + var context: JestPuppeteerGlobal["context"]; + // @ts-ignore + var puppeteerConfig: JestPuppeteerGlobal["puppeteerConfig"]; + // @ts-ignore + var jestPuppeteer: JestPuppeteerGlobal["jestPuppeteer"]; +} + +module.exports = { + globalSetup: require.resolve("jest-environment-puppeteer/setup"), + globalTeardown: require.resolve("jest-environment-puppeteer/teardown"), + testEnvironment: require.resolve("jest-environment-puppeteer"), + setupFilesAfterEnv: [require.resolve("expect-puppeteer")], +};