|
| 1 | +/** |
| 2 | + * Lighthouse audits of the demo site, against the production build. |
| 3 | + * |
| 4 | + * See THRESHOLDS below for why performance is the one category not held at 100. |
| 5 | + */ |
| 6 | +import { chromium, test } from '@playwright/test'; |
| 7 | +import { playAudit } from 'playwright-lighthouse'; |
| 8 | +import desktopConfig from 'lighthouse/core/config/desktop-config.js'; |
| 9 | + |
| 10 | +import { PREVIEW_URL } from '../playwright.lighthouse.config'; |
| 11 | + |
| 12 | +/** |
| 13 | + * The public pages only — what a visitor to state-in-url.dev actually lands on. |
| 14 | + * |
| 15 | + * The app also serves `/useUrlState`, `/test-ssr`, `/useSharedState` and the |
| 16 | + * rest of the `(tests)` route group. Those are fixtures for the Playwright e2e |
| 17 | + * suite that happen to be deployed with everything else: they exist to be |
| 18 | + * driven by assertions, not read, and several deliberately render in ways no |
| 19 | + * real page would. Auditing them would hold the library's test harness to a |
| 20 | + * marketing page's standards and produce failures nobody should act on. |
| 21 | + */ |
| 22 | +const PAGES = [ |
| 23 | + { path: '/', name: '/ (useUrlState demo)' }, |
| 24 | + { path: '/react-router', name: '/react-router' }, |
| 25 | + { path: '/remix', name: '/remix' }, |
| 26 | +]; |
| 27 | + |
| 28 | +/** |
| 29 | + * Accessibility, best-practices and SEO are deterministic audits of the |
| 30 | + * document and every page scores 100, so they are held there. |
| 31 | + * |
| 32 | + * Performance is not 100 and setting it there would be wishful. Measured |
| 33 | + * August 2026 on all three pages: **99**, held across repeated runs, and the |
| 34 | + * shortfall is entirely one metric — LCP at ~1030 ms, scoring 0.94 of a |
| 35 | + * weight-25 audit. Everything else is perfect (FCP ~290 ms, total blocking |
| 36 | + * time 6-13 ms, speed index ~400 ms). |
| 37 | + * |
| 38 | + * 95 is therefore a real bar rather than a rubber stamp: it is ~4 points below |
| 39 | + * what the pages reliably measure, which absorbs runner variance while still |
| 40 | + * failing on anything that actually regresses — an unoptimised image, a |
| 41 | + * client bundle that grows, a blocking third-party script. Because total |
| 42 | + * blocking time is near zero, almost no main-thread work is in play, so a |
| 43 | + * slower CI runner moves this number far less than it would for a |
| 44 | + * hydration-heavy app. |
| 45 | + * |
| 46 | + * If it starts flaking anyway, lower this one number rather than deleting the |
| 47 | + * assertion. The real performance signal is field data and PageSpeed Insights |
| 48 | + * against production. |
| 49 | + */ |
| 50 | +const THRESHOLDS = { |
| 51 | + performance: 95, |
| 52 | + accessibility: 100, |
| 53 | + 'best-practices': 100, |
| 54 | + seo: 100, |
| 55 | +}; |
| 56 | + |
| 57 | +test.describe('Lighthouse', () => { |
| 58 | + // Serial: two Chrome instances auditing at once skew each other's |
| 59 | + // performance numbers, and there is nothing to gain by racing three runs. |
| 60 | + test.describe.configure({ mode: 'serial' }); |
| 61 | + |
| 62 | + for (const { path, name } of PAGES) { |
| 63 | + // No fixture parameter: this test drives its own browser, and Playwright |
| 64 | + // rejects a named first argument ("First argument must use the object |
| 65 | + // destructuring pattern"), leaving `{}` as the only spelling — which is |
| 66 | + // itself a lint error. `test.info()` is the way out of both. |
| 67 | + test(`${name} meets Lighthouse thresholds`, async () => { |
| 68 | + // Lighthouse drives the browser over CDP, which needs a debugging port |
| 69 | + // Playwright's own `page` fixture does not expose. Offset by worker index |
| 70 | + // so a future parallel run cannot collide on it. |
| 71 | + const port = 9333 + test.info().workerIndex; |
| 72 | + const browser = await chromium.launch({ |
| 73 | + args: [`--remote-debugging-port=${port}`], |
| 74 | + }); |
| 75 | + |
| 76 | + try { |
| 77 | + const page = await browser.newPage(); |
| 78 | + await page.goto(`${PREVIEW_URL}${path}`, { waitUntil: 'networkidle' }); |
| 79 | + |
| 80 | + await playAudit({ |
| 81 | + page, |
| 82 | + port, |
| 83 | + thresholds: THRESHOLDS, |
| 84 | + // Desktop, not Lighthouse's mobile default: mobile applies a 4x CPU |
| 85 | + // slowdown, which turns the performance score into a measurement of |
| 86 | + // the runner rather than the site. |
| 87 | + config: desktopConfig, |
| 88 | + disableLogs: false, |
| 89 | + }); |
| 90 | + } finally { |
| 91 | + await browser.close(); |
| 92 | + } |
| 93 | + }); |
| 94 | + } |
| 95 | +}); |
0 commit comments