feat: integrate Lighthouse in MobilePerformanceMonitor

This commit is contained in:
张翔
2026-03-05 15:15:22 +08:00
parent 8194317a20
commit 06c5fe8f82
4 changed files with 2552 additions and 0 deletions
@@ -12,4 +12,14 @@ test.describe('MobilePerformanceMonitor', () => {
expect(vitals.LCP).toBeGreaterThan(0);
expect(vitals.CLS).toBeGreaterThanOrEqual(0);
});
test('should run Lighthouse audit', async ({ page }) => {
await page.goto('/');
const monitor = new MobilePerformanceMonitor(page);
const result = await monitor.runLighthouseAudit(page.url());
expect(result.score).toBeGreaterThan(0);
expect(result.audits).toBeDefined();
});
});
+24
View File
@@ -1,4 +1,6 @@
import { Page } from '@playwright/test';
import lighthouse from 'lighthouse';
import * as chromeLauncher from 'chrome-launcher';
export interface CoreWebVitals {
FCP: number;
@@ -53,4 +55,26 @@ export class MobilePerformanceMonitor {
TTI: 0,
};
}
async runLighthouseAudit(url: string): Promise<LighthouseResult> {
const chrome = await chromeLauncher.launch({ chromeFlags: ['--headless'] });
const options = {
logLevel: 'info' as const,
output: 'json' as const,
onlyCategories: ['performance'],
port: chrome.port,
};
const runnerResult = await lighthouse(url, options);
await chrome.kill();
if (!runnerResult) {
throw new Error('Lighthouse audit failed');
}
return {
score: runnerResult.lhr.categories.performance.score * 100,
audits: runnerResult.lhr.audits,
};
}
}