Skip to content

Commit

Permalink
fix(jest-puppeteer): fix preset export
Browse files Browse the repository at this point in the history
Fix #528
  • Loading branch information
gregberge committed Mar 7, 2023
1 parent d256469 commit 7e97e9e
Show file tree
Hide file tree
Showing 16 changed files with 79 additions and 101 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -159,6 +159,7 @@ module.exports = {
page: true,
browser: true,
context: true,
puppeteerConfig: true,
jestPuppeteer: true,
},
};
Expand Down
4 changes: 0 additions & 4 deletions packages/jest-environment-puppeteer/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
18 changes: 0 additions & 18 deletions packages/jest-environment-puppeteer/rollup.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
58 changes: 44 additions & 14 deletions packages/jest-environment-puppeteer/src/env.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,49 +4,79 @@ 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<void>;
resetPage: () => Promise<void>;
resetBrowser: () => Promise<void>;
};

type StrictGlobal = {
browser?: Browser | undefined;
page?: Page | undefined;
context?: BrowserContext | undefined;
puppeteerConfig: JestPuppeteerConfig;
jestPuppeteer: JestPuppeteer;
};

export type JestPuppeteerGlobal = Required<StrictGlobal>;

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");

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.");
}
Expand All @@ -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);
Expand All @@ -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.");
}
Expand All @@ -89,21 +119,21 @@ 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();
}
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);
Expand All @@ -118,7 +148,7 @@ export class PuppeteerEnvironment extends NodeEnvironment {

async setup(): Promise<void> {
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 = {
Expand Down Expand Up @@ -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);
}
}
40 changes: 0 additions & 40 deletions packages/jest-environment-puppeteer/src/globals.ts

This file was deleted.

1 change: 1 addition & 0 deletions packages/jest-environment-puppeteer/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export { PuppeteerEnvironment as default } from "./env";
export type { JestPuppeteerGlobal } from "./env";
export type { JestPuppeteerConfig } from "./config";
2 changes: 2 additions & 0 deletions packages/jest-environment-puppeteer/tests/.eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,7 @@ module.exports = {
page: true,
browser: true,
context: true,
puppeteerConfig: true,
jestPuppeteer: true,
},
};
2 changes: 0 additions & 2 deletions packages/jest-environment-puppeteer/tests/basic.test.ts
Original file line number Diff line number Diff line change
@@ -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}`);
Expand Down
Original file line number Diff line number Diff line change
@@ -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 () => {
Expand Down
Original file line number Diff line number Diff line change
@@ -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 () => {
Expand Down
2 changes: 0 additions & 2 deletions packages/jest-environment-puppeteer/tests/config.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import { describe, it, expect, beforeEach } from "@jest/globals";

import * as path from "node:path";
import { readConfig } from "../src/config";

Expand Down
7 changes: 1 addition & 6 deletions packages/jest-puppeteer/jest-preset.js
Original file line number Diff line number Diff line change
@@ -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");
9 changes: 4 additions & 5 deletions packages/jest-puppeteer/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
},
Expand Down
8 changes: 4 additions & 4 deletions packages/jest-puppeteer/rollup.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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",
},
}),
Expand Down
1 change: 0 additions & 1 deletion packages/jest-puppeteer/src/globals.ts

This file was deleted.

21 changes: 21 additions & 0 deletions packages/jest-puppeteer/src/index.ts
Original file line number Diff line number Diff line change
@@ -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")],
};

0 comments on commit 7e97e9e

Please sign in to comment.