Skip to content

Chrome DevTools for coding agents

fix(lighthouse): fail the audit when Lighthouse reports a runtime error (#3188)

Musa Toktas53ea78db7eab
diff --git a/src/tools/lighthouse.ts b/src/tools/lighthouse.ts
index f022d18f..ffd3870a 100644
--- a/src/tools/lighthouse.ts
+++ b/src/tools/lighthouse.ts
@@ -298,6 +298,13 @@ export const lighthouseAudit = definePageTool((args: ParsedArguments) => ({
reportPaths.push(res.value);
}
+ // Lighthouse sets runtimeError when it could not load the page; the categories are unscored.
+ if (lhr.runtimeError) {
+ throw new Error(
+ `Lighthouse could not audit the page (${lhr.runtimeError.code}): ${lhr.runtimeError.message} Reports: ${reportPaths.join(', ')}`,
+ );
+ }
+
const categoryScores = Object.values(lhr.categories).map(c => ({
id: c.id,
title: c.title,
diff --git a/tests/tools/lighthouse.test.ts b/tests/tools/lighthouse.test.ts
index ec0348d9..93fd6f0f 100644
--- a/tests/tools/lighthouse.test.ts
+++ b/tests/tools/lighthouse.test.ts
@@ -187,6 +187,42 @@ describe('lighthouse', () => {
sinon.assert.calledOnceWithExactly(page.restoreEmulation);
});
+ it('fails when Lighthouse reports a runtime error', async () => {
+ const {page, context, response, args} = createHandlerMocks();
+ context.saveTemporaryFile.resolves({filepath: 'report.json'});
+ sinon.stub(lighthouseRunner, 'navigation').resolves(
+ createMockRunnerResult({
+ runtimeError: {
+ code: 'ERRORED_DOCUMENT_REQUEST',
+ message: 'Lighthouse was unable to reliably load the page.',
+ },
+ }),
+ );
+
+ await assert.rejects(
+ () =>
+ lighthouseAudit(args).handler(
+ {
+ params: {
+ mode: 'navigation',
+ device: 'desktop',
+ },
+ page,
+ },
+ response,
+ context,
+ ),
+ {
+ message:
+ 'Lighthouse could not audit the page (ERRORED_DOCUMENT_REQUEST): Lighthouse was unable to reliably load the page. Reports: report.json, report.json',
+ },
+ );
+
+ sinon.assert.calledTwice(context.saveTemporaryFile);
+ sinon.assert.notCalled(response.attachLighthouseResult);
+ sinon.assert.calledOnceWithExactly(page.restoreEmulation);
+ });
+
it('emulates a desktop user agent for desktop audits', async () => {
const {page, context, response, args} = createHandlerMocks();
context.saveTemporaryFile.resolves({filepath: 'report.json'});