From 468c7f93705a4343a6575e16fee6a199d038c322 Mon Sep 17 00:00:00 2001 From: Adam Raine Date: Thu, 7 Mar 2024 18:26:19 -0800 Subject: [PATCH] faster test --- core/gather/gatherers/full-page-screenshot.js | 25 ++++++++++++------- .../gatherers/full-page-screenshot-test.js | 15 +++++++---- 2 files changed, 26 insertions(+), 14 deletions(-) diff --git a/core/gather/gatherers/full-page-screenshot.js b/core/gather/gatherers/full-page-screenshot.js index ad0861ee723e..1d8ca13deb73 100644 --- a/core/gather/gatherers/full-page-screenshot.js +++ b/core/gather/gatherers/full-page-screenshot.js @@ -58,6 +58,21 @@ class FullPageScreenshot extends BaseGatherer { supportedModes: ['snapshot', 'timespan', 'navigation'], }; + /** + * @param {LH.Gatherer.Context} context + */ + waitForNetworkIdle(context) { + const session = context.driver.defaultSession; + const networkMonitor = context.driver.networkMonitor; + return waitForNetworkIdle(session, networkMonitor, { + pretendDCLAlreadyFired: true, + networkQuietThresholdMs: 1000, + busyEvent: 'network-critical-busy', + idleEvent: 'network-critical-idle', + isIdle: recorder => recorder.isCriticalIdle(), + }); + } + /** * @param {LH.Gatherer.Context} context * @param {{height: number, width: number, mobile: boolean}} deviceMetrics @@ -75,15 +90,7 @@ class FullPageScreenshot extends BaseGatherer { ); const height = Math.min(fullHeight, MAX_WEBP_SIZE); - // Setup network monitor before we change the viewport. - const networkMonitor = context.driver.networkMonitor; - const waitForNetworkIdleResult = waitForNetworkIdle(session, networkMonitor, { - pretendDCLAlreadyFired: true, - networkQuietThresholdMs: 1000, - busyEvent: 'network-critical-busy', - idleEvent: 'network-critical-idle', - isIdle: recorder => recorder.isCriticalIdle(), - }); + const waitForNetworkIdleResult = this.waitForNetworkIdle(context); await session.sendCommand('Emulation.setDeviceMetricsOverride', { mobile: deviceMetrics.mobile, diff --git a/core/test/gather/gatherers/full-page-screenshot-test.js b/core/test/gather/gatherers/full-page-screenshot-test.js index 9a6675b4c814..bc0f5924d47f 100644 --- a/core/test/gather/gatherers/full-page-screenshot-test.js +++ b/core/test/gather/gatherers/full-page-screenshot-test.js @@ -5,6 +5,7 @@ import {createMockContext} from '../../gather/mock-driver.js'; import FullPageScreenshotGatherer from '../../../gather/gatherers/full-page-screenshot.js'; +import {fnAny} from '../../test-utils.js'; /** @type {{width: number, height: number}} */ let contentSize; @@ -15,8 +16,17 @@ let screenshotSize; /** @type {string[]} */ let screenshotData; let mockContext = createMockContext(); +let fpsGatherer = new FullPageScreenshotGatherer(); beforeEach(() => { + fpsGatherer = new FullPageScreenshotGatherer(); + + // Prevent `waitForNetworkIdle` from stalling the tests + fpsGatherer.waitForNetworkIdle = fnAny().mockImplementation(() => ({ + promise: Promise.resolve(), + cancel: fnAny(), + })); + contentSize = {width: 100, height: 100}; screenSize = {width: 100, height: 100, dpr: 1}; screenshotSize = contentSize; @@ -63,7 +73,6 @@ beforeEach(() => { describe('FullPageScreenshot gatherer', () => { it('captures a full-page screenshot', async () => { - const fpsGatherer = new FullPageScreenshotGatherer(); contentSize = {width: 412, height: 2000}; screenSize = {width: 412, height: 412}; screenshotSize = contentSize; @@ -92,7 +101,6 @@ describe('FullPageScreenshot gatherer', () => { }); it('resets the emulation correctly when Lighthouse controls it', async () => { - const fpsGatherer = new FullPageScreenshotGatherer(); contentSize = {width: 412, height: 2000}; screenSize = {width: 412, height: 412}; screenshotSize = contentSize; @@ -126,7 +134,6 @@ describe('FullPageScreenshot gatherer', () => { }); it('resets the emulation correctly when Lighthouse does not control it', async () => { - const fpsGatherer = new FullPageScreenshotGatherer(); contentSize = {width: 500, height: 1500}; screenSize = {width: 500, height: 500, dpr: 2}; screenshotSize = contentSize; @@ -172,8 +179,6 @@ describe('FullPageScreenshot gatherer', () => { }); it('limits the screenshot height to the max Chrome can capture', async () => { - const fpsGatherer = new FullPageScreenshotGatherer(); - contentSize = {width: 412, height: 100000}; screenSize = {width: 412, height: 412, dpr: 1}; screenshotSize = contentSize;