From 5f9db96baa7eb9256c9ed1b9656944261a7ca11a Mon Sep 17 00:00:00 2001 From: Mumtahin Farabi Date: Mon, 2 Dec 2024 22:12:14 -0500 Subject: [PATCH] test(docs-e2e): add lighthouse audit --- apps/docs-e2e/playwright.config.ts | 2 ++ apps/docs-e2e/src/lighthouse.spec.ts | 52 ++++++++++++++++++++++++++++ 2 files changed, 54 insertions(+) create mode 100644 apps/docs-e2e/src/lighthouse.spec.ts diff --git a/apps/docs-e2e/playwright.config.ts b/apps/docs-e2e/playwright.config.ts index f16c3da8..54006453 100644 --- a/apps/docs-e2e/playwright.config.ts +++ b/apps/docs-e2e/playwright.config.ts @@ -22,6 +22,7 @@ const baseURL = process.env.BASE_URL || 'http://127.0.0.1:3000' */ export default defineConfig({ ...nxE2EPreset(__filename, { testDir: './src' }), + fullyParallel: true, /* Shared settings for all the projects below. See https://playwright.dev/docs/api/class-testoptions. */ retries: 2, use: { @@ -37,6 +38,7 @@ export default defineConfig({ cwd: workspaceRoot, timeout: 120 * 1000, }, + reporter: [['html']], projects: [ { name: 'chromium (desktop)', diff --git a/apps/docs-e2e/src/lighthouse.spec.ts b/apps/docs-e2e/src/lighthouse.spec.ts new file mode 100644 index 00000000..1f67ad36 --- /dev/null +++ b/apps/docs-e2e/src/lighthouse.spec.ts @@ -0,0 +1,52 @@ +import type { Browser } from 'playwright' +import { chromium, test } from '@playwright/test' +import getPort from 'get-port' +import { playAudit } from 'playwright-lighthouse' + +const lighthouseTest = test.extend<{ Page }, { port: number, browser: Browser }>({ + port: [ + async (use) => { + // Assign a unique port for each playwright worker to support parallel tests + const port = await getPort() + await use(port) + }, + { scope: 'worker' }, + ], + + browser: [ + async ({ port }, use) => { + const browser = await chromium.launch({ + args: [`--remote-debugging-port=${port}`], + }) + await use(browser) + }, + { scope: 'worker' }, + ], +}) + +const thresholdsConfig = { + 'performance': 90, + 'accessibility': 90, + 'best-practices': 90, + 'seo': 90, + // 'pwa': 50, +} + +lighthouseTest('should pass lighthouse audits', async ({ page, port }) => { + await page.goto('/') + + await playAudit({ + page, + port, + thresholds: thresholdsConfig, + reports: { + formats: { + // json: true, // defaults to false + html: true, // defaults to false + // csv: true, // defaults to false + }, + name: `latest-report`, // defaults to `lighthouse-${new Date().getTime()}` + directory: `${process.cwd()}../../../lighthouse-report`, // defaults to `${process.cwd()}/lighthouse` + }, + }) +})