From 72f12a81c7445fdeea18abce0fce171ec9c258bd Mon Sep 17 00:00:00 2001 From: ChristopherPHolder Date: Fri, 13 Nov 2026 10:16:16 +0100 Subject: [PATCH] fix(cli): only use the mobile fallback viewport for dry runs The fallback launch options gave every run a mobile defaultViewport, although it was only meant for --dry-run. A replayed DevTools recording starts with setViewport and isMobile: false, which makes Puppeteer reload the page inside the timespan. Lighthouse then logs a TypeError for the resulting about:blank navigation. Real runs now use Puppeteer's default viewport. Dry runs are unchanged. Closes #352 --- .../collect/utils/user-flow/collect-flow.ts | 14 ++++++++----- .../utils/user-flow/collect-flow.unit.test.ts | 20 +++++++++++++++++++ 2 files changed, 29 insertions(+), 5 deletions(-) diff --git a/packages/cli/src/lib/commands/collect/utils/user-flow/collect-flow.ts b/packages/cli/src/lib/commands/collect/utils/user-flow/collect-flow.ts index ca8792d9..ef000c49 100644 --- a/packages/cli/src/lib/commands/collect/utils/user-flow/collect-flow.ts +++ b/packages/cli/src/lib/commands/collect/utils/user-flow/collect-flow.ts @@ -26,7 +26,7 @@ export async function collectFlow( const lhConfig = mergeLhConfig(globalLhCfg, flowOptions?.config); flowOptions.config = lhConfig; - const browser: Browser = await puppeteer.launch(parseLaunchOptions(launchOptions)); + const browser: Browser = await puppeteer.launch(parseLaunchOptions(launchOptions, argv)); const page: Page = await browser.newPage(); logVerbose(`Collect: ${flowOptions.name} from URL ${cliOption.url}`); @@ -43,15 +43,19 @@ export async function collectFlow( return flow; } -export function parseLaunchOptions(launchOptions?: LaunchOptions): LaunchOptions { +export function parseLaunchOptions( + launchOptions?: LaunchOptions, + { dryRun }: { dryRun?: boolean } = {} +): LaunchOptions { // object containing the options for puppeteer/chromium launchOptions = launchOptions || { // has to be false to run in the CI because of a bug :( // https://github.com/puppeteer/puppeteer/issues/8148 headless: false, - // hack for dryRun => should get fixed inside user flow in future - defaultViewport: { isMobile: true, isLandscape: false, width: 800, height: 600 } - } as LaunchOptions; + // Only for dry runs. In a real run, a mobile viewport makes Puppeteer reload the page + // when a recording's setViewport step sets isMobile: false. + ...(dryRun && { defaultViewport: { isMobile: true, isLandscape: false, width: 800, height: 600 } }), + }; const cliMode = detectCliMode(); // cli mode is "CI" or "SANDBOX" if (cliMode !== 'DEFAULT' && launchOptions) { diff --git a/packages/cli/src/lib/commands/collect/utils/user-flow/collect-flow.unit.test.ts b/packages/cli/src/lib/commands/collect/utils/user-flow/collect-flow.unit.test.ts index 0d4856b7..c5164277 100644 --- a/packages/cli/src/lib/commands/collect/utils/user-flow/collect-flow.unit.test.ts +++ b/packages/cli/src/lib/commands/collect/utils/user-flow/collect-flow.unit.test.ts @@ -14,6 +14,26 @@ describe('parseLaunchOptions', () => { expect(parseLaunchOptions().headless).toBe(false); }); + it('should use the default viewport of Puppeteer if no launch options are given', () => { + vi.spyOn(cliMode, 'detectCliMode').mockReturnValue('DEFAULT'); + expect(parseLaunchOptions().defaultViewport).toBeUndefined(); + }); + + it('should use a mobile viewport for a dry run if no launch options are given', () => { + vi.spyOn(cliMode, 'detectCliMode').mockReturnValue('DEFAULT'); + expect(parseLaunchOptions(undefined, { dryRun: true }).defaultViewport).toEqual({ + isMobile: true, + isLandscape: false, + width: 800, + height: 600, + }); + }); + + it('should keep the given launch options for a dry run', () => { + vi.spyOn(cliMode, 'detectCliMode').mockReturnValue('DEFAULT'); + expect(parseLaunchOptions({ headless: 'shell' }, { dryRun: true })).toEqual({ headless: 'shell' }); + }); + it('should keep the given launch options in DEFAULT mode', () => { vi.spyOn(cliMode, 'detectCliMode').mockReturnValue('DEFAULT'); expect(parseLaunchOptions({ headless: 'shell' })).toEqual({ headless: 'shell' });