Skip to content

Commit e925298

Browse files
committed
fix!(testing): puppeteer v10 support (#2934)
- Update puppeteer to v10.0.0 - Remove @types/puppeteer from root package.json file - Update programmatic library checks - Remove @types/puppeteer - Make puppeteer@10 the recommended version - Replace @types/puppeteer instances of PageCloseOptions - This interface was originally defined in v5.4.3 of the DefinitelyTyped type declaration file for Puppeteer https://github.com/DefinitelyTyped/DefinitelyTyped/blob/34edf5fb8fdf54f57ed6584f77f1611767af7f6b/types/puppeteer/index.d.ts#L1423 - It has since been removed, and replace with an object literal of the same shape https://github.com/puppeteer/puppeteer/blob/main/src/common/Page.ts#L2097 - Replace usages of NavigationOptions for WaitForOptions - WaitForOptions is structurally the same as @types/puppeteer's [NavigationOptions](https://github.com/DefinitelyTyped/DefinitelyTyped/blob/34edf5fb8fdf54f57ed6584f77f1611767af7f6b/types/puppeteer/index.d.ts#L616) and is used as a drop in replacement - The page#goTo method in Puppeteer's definition in both v5.4.3 and v10 allow an optional 'referer' string. Since it was omitted in the original implementation, I did not include it in this update - Replace Response with HTTPResponse - Update ElementHandle#press to use KeyInput instead of string - Make typings of executionContext.evaluate explicit - Migrate EmulateOptions - Puppeteer no longer exports a type of name EmulateOptions, in favor of an object literal. BREAKING CHANGE: Requires Puppeteer v10.0.0
1 parent 91541b2 commit e925298

8 files changed

+83
-55
lines changed

package-lock.json

+26-13
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@
7676
"@types/pixelmatch": "^5.2.4",
7777
"@types/pngjs": "^3.4.2",
7878
"@types/prompts": "^2.0.9",
79-
"@types/semver": "^7.3.12",
79+
"@types/semver": "^7.3.4",
8080
"@types/sizzle": "^2.3.2",
8181
"@types/webpack": "^4.41.26",
8282
"@types/ws": "^7.4.0",

src/cli/task-test.ts

+35
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,41 @@ export const taskTest = async (config: ValidatedConfig): Promise<void> => {
4747
}
4848

4949
try {
50+
config.buildDocs = false;
51+
const testingRunOpts: TestingRunOptions = {
52+
e2e: !!config.flags.e2e,
53+
screenshot: !!config.flags.screenshot,
54+
spec: !!config.flags.spec,
55+
updateScreenshot: !!config.flags.updateScreenshot,
56+
};
57+
58+
// always ensure we have jest modules installed
59+
const ensureModuleIds = ['@types/jest', 'jest', 'jest-cli'];
60+
61+
if (testingRunOpts.e2e) {
62+
// if it's an e2e test, also make sure we're got
63+
// puppeteer modules installed and if browserExecutablePath is provided don't download Chromium use only puppeteer-core instead
64+
const puppeteer = config.testing.browserExecutablePath ? 'puppeteer-core' : 'puppeteer';
65+
66+
ensureModuleIds.push(puppeteer);
67+
68+
if (testingRunOpts.screenshot) {
69+
// ensure we've got pixelmatch for screenshots
70+
config.logger.warn(
71+
config.logger.yellow(
72+
`EXPERIMENTAL: screenshot visual diff testing is currently under heavy development and has not reached a stable status. However, any assistance testing would be appreciated.`,
73+
),
74+
);
75+
}
76+
}
77+
78+
// ensure we've got the required modules installed
79+
const diagnostics = await config.sys.lazyRequire.ensure(config.rootDir, ensureModuleIds);
80+
if (diagnostics.length > 0) {
81+
config.logger.printDiagnostics(diagnostics);
82+
return config.sys.exit(1);
83+
}
84+
5085
// let's test!
5186
const { createTesting } = await import('@stencil/core/testing');
5287
const testing = await createTesting(config);

src/sys/node/node-sys.ts

+8-6
Original file line numberDiff line numberDiff line change
@@ -589,12 +589,14 @@ export function createNodeSys(c: { process?: any } = {}) {
589589
const nodeResolve = new NodeResolveModule();
590590

591591
sys.lazyRequire = new NodeLazyRequire(nodeResolve, {
592-
'@types/jest': { minVersion: '24.9.1', recommendedVersion: '27.0.3', maxVersion: '27.0.0' },
593-
jest: { minVersion: '24.9.1', recommendedVersion: '27.0.3', maxVersion: '27.0.0' },
594-
'jest-cli': { minVersion: '24.9.0', recommendedVersion: '27.4.5', maxVersion: '27.0.0' },
595-
puppeteer: { minVersion: '1.19.0', recommendedVersion: '10.0.0' },
596-
'puppeteer-core': { minVersion: '1.19.0', recommendedVersion: '5.2.1' },
597-
'workbox-build': { minVersion: '4.3.1', recommendedVersion: '4.3.1' },
592+
// [minimumVersion, recommendedVersion]
593+
'@types/jest': ['24.9.1', '26.0.21'],
594+
'jest': ['24.9.0', '26.6.3'],
595+
'jest-cli': ['24.9.0', '26.6.3'],
596+
'pixelmatch': ['4.0.2', '4.0.2'],
597+
'puppeteer': ['1.19.0', '10.0.0'],
598+
'puppeteer-core': ['1.19.0', '5.2.1'],
599+
'workbox-build': ['4.3.1', '4.3.1'],
598600
});
599601

600602
prcs.on('SIGINT', runInterruptsCallbacks);

src/testing/puppeteer/puppeteer-browser.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ export async function startPuppeteerBrowser(config: ValidatedConfig) {
4646
if (config.testing.browserWSEndpoint) {
4747
browser = await puppeteer.connect({
4848
browserWSEndpoint: config.testing.browserWSEndpoint,
49-
...connectOpts,
49+
...connectOpts
5050
});
5151
} else {
5252
const launchOpts: puppeteer.BrowserLaunchArgumentOptions & puppeteer.LaunchOptions & puppeteer.ConnectOptions = {

src/testing/puppeteer/puppeteer-declarations.ts

+2-30
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,19 @@
11
import type { EventInitDict, EventSpy, ScreenshotDiff, ScreenshotOptions } from '@stencil/core/internal';
22
import type {
33
ClickOptions,
4-
HTTPResponse as PuppeteerHTTPResponse,
4+
HTTPResponse,
55
Page,
66
ScreenshotOptions as PuppeteerScreenshotOptions,
77
WaitForOptions,
88
} from 'puppeteer';
99

10-
/**
11-
* This type helps with declaration merging as a part of Stencil's migration from Puppeteer v5.4.3 to v10.0.0. In
12-
* v5.4.3, `HttpResponse` was an interface whereas v10.0.0 declares it as a class. It is redeclared here to help teams
13-
* migrate to a newer minor version of Stencil without requiring a Puppeteer upgrade/major version of Stencil. This type
14-
* should be removed as a part of the Stencil 3.0 release.
15-
*/
16-
export type HTTPResponse = PuppeteerHTTPResponse;
17-
18-
/**
19-
* These types help with declaration merging as a part of Stencil's migration from Puppeteer v5.4.3 to v10.0.0. In
20-
* v10.0.0, `WaitForOptions` is a renamed version of `NavigationOptions` from v5.4.3, who has had its type hierarchy
21-
* flattened.
22-
*
23-
* See {@link https://github.com/DefinitelyTyped/DefinitelyTyped/blob/8290e943f6b398acf39ee1b2e486824144e15bc8/types/puppeteer/index.d.ts#L605-L622}
24-
* for the v5.4.3 types.
25-
*
26-
* These types are redeclared here to help teams migrate to a newer minor version of Stencil without requiring a
27-
* Puppeteer upgrade/major version of Stencil. These type additions should be removed as a part of the Stencil 3.0
28-
* release.
29-
*/
30-
declare module 'puppeteer' {
31-
type LifeCycleEvent = 'load' | 'domcontentloaded' | 'networkidle0' | 'networkidle2';
32-
interface WaitForOptions {
33-
timeout?: number;
34-
waitUntil?: LifeCycleEvent | LifeCycleEvent[];
35-
}
36-
}
37-
3810
/**
3911
* This type was once exported by Puppeteer, but has since moved to an object literal in (Puppeteer’s) native types.
4012
* Re-create it here as a named type to use across multiple Stencil-related testing files.
4113
*/
4214
export type PageCloseOptions = {
4315
runBeforeUnload?: boolean;
44-
};
16+
}
4517

4618
export interface NewE2EPageOptions extends WaitForOptions {
4719
url?: string;

src/testing/puppeteer/puppeteer-emulate.ts

+2-4
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,8 @@ export function setScreenshotEmulateData(userEmulateConfig: EmulateConfig, env:
1818
try {
1919
const deviceDescriptors = require(env.__STENCIL_PUPPETEER_MODULE__ + '/DeviceDescriptors');
2020

21-
const puppeteerEmulateOpts = deviceDescriptors[userEmulateConfig.device] as {
22-
userAgent: string;
23-
viewport: puppeteer.Viewport;
24-
};
21+
const puppeteerEmulateOpts =
22+
deviceDescriptors[userEmulateConfig.device] as { userAgent: string, viewport: puppeteer.Viewport };
2523
if (!puppeteerEmulateOpts) {
2624
console.error(`invalid emulate device: ${userEmulateConfig.device}`);
2725
return;

src/testing/puppeteer/puppeteer-page.ts

+8
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,14 @@ import type {
99
PageCloseOptions,
1010
PageDiagnostic,
1111
} from './puppeteer-declarations';
12+
13+
import type {
14+
ConsoleMessage,
15+
ConsoleMessageLocation,
16+
JSHandle,
17+
Page,
18+
WaitForOptions,
19+
} from 'puppeteer';
1220
import { find, findAll } from './puppeteer-element';
1321
import { initPageEvents, waitForEvent } from './puppeteer-events';
1422
import { initPageScreenshot } from './puppeteer-screenshot';

0 commit comments

Comments
 (0)