From c2244d301a7663c6bc998fadc5de22253be04026 Mon Sep 17 00:00:00 2001 From: Nikolay Vitkov <34244704+Lightning00Blade@users.noreply.github.com> Date: Fri, 26 Apr 2024 12:21:21 +0200 Subject: [PATCH] test: reduce flakiness and types (#12347) --- test/src/network.spec.ts | 2 +- test/src/utils.ts | 37 ++++++++++++++++--------------------- 2 files changed, 17 insertions(+), 22 deletions(-) diff --git a/test/src/network.spec.ts b/test/src/network.spec.ts index fb9eedce612..2ff3daff120 100644 --- a/test/src/network.spec.ts +++ b/test/src/network.spec.ts @@ -520,7 +520,7 @@ describe('network', function () { const responses: HTTPResponse[] = []; page.on('response', response => { - return responses.push(response); + return !isFavicon(response) && responses.push(response); }); await page.goto(server.EMPTY_PAGE); expect(responses).toHaveLength(1); diff --git a/test/src/utils.ts b/test/src/utils.ts index 8a68781ef6f..97c82b55d78 100644 --- a/test/src/utils.ts +++ b/test/src/utils.ts @@ -6,7 +6,6 @@ import {rm} from 'fs/promises'; import {tmpdir} from 'os'; -import path from 'path'; import expect from 'expect'; import type {Frame} from 'puppeteer-core/internal/api/Frame.js'; @@ -16,8 +15,6 @@ import {Deferred} from 'puppeteer-core/internal/util/Deferred.js'; import {compare} from './golden-utils.js'; -const PROJECT_ROOT = path.join(__dirname, '..', '..'); - declare module 'expect' { interface Matchers { toBeGolden(pathOrBuffer: string | Buffer): R; @@ -56,28 +53,26 @@ export const extendExpectWithToBeGolden = ( }); }; -export const projectRoot = (): string => { - return PROJECT_ROOT; -}; - export const attachFrame = async ( pageOrFrame: Page | Frame, frameId: string, url: string -): Promise => { - using handle = await pageOrFrame.evaluateHandle(attachFrame, frameId, url); - return (await handle.asElement()?.contentFrame()) ?? undefined; - - async function attachFrame(frameId: string, url: string) { - const frame = document.createElement('iframe'); - frame.src = url; - frame.id = frameId; - document.body.appendChild(frame); - await new Promise(x => { - return (frame.onload = x); - }); - return frame; - } +): Promise => { + using handle = await pageOrFrame.evaluateHandle( + async (frameId, url) => { + const frame = document.createElement('iframe'); + frame.src = url; + frame.id = frameId; + document.body.appendChild(frame); + await new Promise(x => { + return (frame.onload = x); + }); + return frame; + }, + frameId, + url + ); + return await handle.contentFrame(); }; export const isFavicon = (request: {url: () => string | string[]}): boolean => {