Skip to content

Commit 82e4163

Browse files
committed
fix(jest-puppeteer): fix preset export
Fix #528
1 parent d256469 commit 82e4163

File tree

16 files changed

+79
-101
lines changed

16 files changed

+79
-101
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ module.exports = {
147147

148148
### Configure ESLint
149149

150-
Jest Puppeteer exposes three new globals: `browser`, `page`, `context`. If you want to avoid errors, you can add them to your `.eslintrc.js`:
150+
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:
151151

152152
```js
153153
// .eslintrc.js
@@ -159,6 +159,7 @@ module.exports = {
159159
page: true,
160160
browser: true,
161161
context: true,
162+
puppeteerConfig: true,
162163
jestPuppeteer: true,
163164
},
164165
};

packages/jest-environment-puppeteer/package.json

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,6 @@
1010
"types": "./dist/index.d.ts",
1111
"default": "./dist/index.js"
1212
},
13-
"./globals": {
14-
"types": "./dist/globals.d.ts",
15-
"default": "./dist/globals.js"
16-
},
1713
"./setup": "./setup.js",
1814
"./teardown": "./teardown.js",
1915
"./package.json": "./package.json"

packages/jest-environment-puppeteer/rollup.config.mjs

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -30,24 +30,6 @@ export default [
3030
},
3131
plugins: [swcPlugin],
3232
}),
33-
bundle({
34-
input: "src/globals.ts",
35-
output: {
36-
file: "dist/globals.js",
37-
format: "cjs",
38-
interop: "compat",
39-
},
40-
plugins: [swcPlugin],
41-
}),
42-
bundle({
43-
input: "src/globals.ts",
44-
plugins: [dts()],
45-
output: {
46-
file: "dist/globals.d.ts",
47-
format: "es",
48-
exports: "default",
49-
},
50-
}),
5133
bundle({
5234
output: {
5335
file: "dist/index.js",

packages/jest-environment-puppeteer/src/env.ts

Lines changed: 44 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,49 +4,79 @@ import { mkdir } from "node:fs/promises";
44
import { readConfig } from "./config";
55
import { blockStdin } from "./stdin";
66
import { connectBrowserFromWorker } from "./browsers";
7-
import type { JestPuppeteerGlobal } from "./globals";
7+
import type { JestPuppeteerConfig } from "./config";
8+
import type { Page, BrowserContext, Browser } from "puppeteer";
9+
10+
type JestPuppeteer = {
11+
debug: () => Promise<void>;
12+
resetPage: () => Promise<void>;
13+
resetBrowser: () => Promise<void>;
14+
};
15+
16+
type StrictGlobal = {
17+
browser?: Browser | undefined;
18+
page?: Page | undefined;
19+
context?: BrowserContext | undefined;
20+
puppeteerConfig: JestPuppeteerConfig;
21+
jestPuppeteer: JestPuppeteer;
22+
};
23+
24+
export type JestPuppeteerGlobal = Required<StrictGlobal>;
25+
26+
declare global {
27+
// @ts-ignore
28+
var browser: Global["browser"];
29+
// @ts-ignore
30+
var page: Global["page"];
31+
// @ts-ignore
32+
var context: Global["context"];
33+
// @ts-ignore
34+
var puppeteerConfig: Global["puppeteerConfig"];
35+
// @ts-ignore
36+
var jestPuppeteer: Global["jestPuppeteer"];
37+
}
838

939
const testTimeoutSymbol = Symbol.for("TEST_TIMEOUT_SYMBOL");
1040

1141
const handlePageError = (error: Error) => {
1242
process.emit("uncaughtException", error);
1343
};
1444

15-
const getBrowser = (global: JestPuppeteerGlobal) => {
45+
const getBrowser = (global: StrictGlobal) => {
1646
if (!global.browser) {
1747
throw new Error("Cannot access browser before launching browser.");
1848
}
1949
return global.browser;
2050
};
2151

22-
const getContext = (global: JestPuppeteerGlobal) => {
52+
const getContext = (global: StrictGlobal) => {
2353
if (!global.context) {
2454
throw new Error("Cannot access context before launching context.");
2555
}
2656
return global.context;
2757
};
2858

29-
const connectBrowser = async (global: JestPuppeteerGlobal) => {
59+
const connectBrowser = async (global: StrictGlobal) => {
3060
if (global.browser) {
3161
throw new Error("Cannot connect browser before closing previous browser.");
3262
}
3363
global.browser = await connectBrowserFromWorker(global.puppeteerConfig);
3464
};
3565

36-
const disconnectBrowser = async (global: JestPuppeteerGlobal) => {
66+
const disconnectBrowser = async (global: StrictGlobal) => {
3767
if (!global.browser) return;
3868
await global.browser.disconnect();
3969
global.browser = undefined;
4070
};
4171

42-
const getPage = (global: JestPuppeteerGlobal) => {
72+
const getPage = (global: StrictGlobal) => {
4373
if (!global.page) {
4474
throw new Error("Cannot access page before launching browser.");
4575
}
4676
return global.page;
4777
};
4878

49-
const openPage = async (global: JestPuppeteerGlobal) => {
79+
const openPage = async (global: StrictGlobal) => {
5080
if (global.page) {
5181
throw new Error("Cannot open page before closing previous page.");
5282
}
@@ -57,7 +87,7 @@ const openPage = async (global: JestPuppeteerGlobal) => {
5787
global.page = page;
5888
};
5989

60-
const closePage = async (global: JestPuppeteerGlobal) => {
90+
const closePage = async (global: StrictGlobal) => {
6191
if (!global.page) return;
6292
if (global.puppeteerConfig.exitOnPageError) {
6393
global.page.off("pageerror", handlePageError);
@@ -68,7 +98,7 @@ const closePage = async (global: JestPuppeteerGlobal) => {
6898
global.page = undefined;
6999
};
70100

71-
const createContext = async (global: JestPuppeteerGlobal) => {
101+
const createContext = async (global: StrictGlobal) => {
72102
if (global.context) {
73103
throw new Error("Cannot create context before closing previous context.");
74104
}
@@ -89,21 +119,21 @@ const createContext = async (global: JestPuppeteerGlobal) => {
89119
}
90120
};
91121

92-
const closeContext = async (global: JestPuppeteerGlobal) => {
122+
const closeContext = async (global: StrictGlobal) => {
93123
if (!global.context) return;
94124
if (global.context.isIncognito()) {
95125
await global.context.close();
96126
}
97127
global.context = undefined;
98128
};
99129

100-
const initAll = async (global: JestPuppeteerGlobal) => {
130+
const initAll = async (global: StrictGlobal) => {
101131
await connectBrowser(global);
102132
await createContext(global);
103133
await openPage(global);
104134
};
105135

106-
const closeAll = async (global: JestPuppeteerGlobal) => {
136+
const closeAll = async (global: StrictGlobal) => {
107137
await closePage(global);
108138
await closeContext(global);
109139
await disconnectBrowser(global);
@@ -118,7 +148,7 @@ export class PuppeteerEnvironment extends NodeEnvironment {
118148

119149
async setup(): Promise<void> {
120150
const config = await readConfig();
121-
const global = this.global as unknown as JestPuppeteerGlobal;
151+
const global = this.global as unknown as StrictGlobal;
122152
global.puppeteerConfig = config;
123153

124154
global.jestPuppeteer = {
@@ -148,7 +178,7 @@ export class PuppeteerEnvironment extends NodeEnvironment {
148178
}
149179

150180
async teardown() {
151-
const global = this.global as unknown as JestPuppeteerGlobal;
181+
const global = this.global as unknown as StrictGlobal;
152182
await closeAll(global);
153183
}
154184
}

packages/jest-environment-puppeteer/src/globals.ts

Lines changed: 0 additions & 40 deletions
This file was deleted.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
export { PuppeteerEnvironment as default } from "./env";
2+
export type { JestPuppeteerGlobal } from "./env";
23
export type { JestPuppeteerConfig } from "./config";

packages/jest-environment-puppeteer/tests/.eslintrc.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,7 @@ module.exports = {
77
page: true,
88
browser: true,
99
context: true,
10+
puppeteerConfig: true,
11+
jestPuppeteer: true,
1012
},
1113
};

packages/jest-environment-puppeteer/tests/basic.test.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
import { describe, beforeAll, it, expect } from "@jest/globals";
2-
31
describe("Basic", () => {
42
beforeAll(async () => {
53
await page.goto(`http://localhost:${process.env.TEST_SERVER_PORT}`);

packages/jest-environment-puppeteer/tests/browserContext-1.test.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
import { describe, it, expect } from "@jest/globals";
2-
31
describe("browserContext", () => {
42
const test = process.env.INCOGNITO ? it : it.skip;
53
test("incognito should isolate cookies (part 1)", async () => {

packages/jest-environment-puppeteer/tests/browserContext-2.test.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
import { describe, it, expect } from "@jest/globals";
2-
31
describe("browserContext", () => {
42
const test = process.env.INCOGNITO ? it : it.skip;
53
test("incognito should isolate cookies (part 2)", async () => {

0 commit comments

Comments
 (0)