Skip to content

Commit 0ab31e4

Browse files
authored
report: fix compat for older lighthouse reports (#14617)
1 parent 3686757 commit 0ab31e4

File tree

9 files changed

+29067
-10
lines changed

9 files changed

+29067
-10
lines changed

core/util.cjs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,10 @@ class Util {
154154
}
155155
}
156156
}
157+
158+
// TODO: convert printf-style displayValue.
159+
// Added: #5099, v3
160+
// Removed: #6767, v4
157161
}
158162
}
159163

@@ -216,6 +220,20 @@ class Util {
216220
});
217221
}
218222

223+
// Add some minimal stuff so older reports still work.
224+
if (!clone.environment) {
225+
// @ts-expect-error
226+
clone.environment = {benchmarkIndex: 0};
227+
}
228+
if (!clone.configSettings.screenEmulation) {
229+
// @ts-expect-error
230+
clone.configSettings.screenEmulation = {};
231+
}
232+
if (!clone.i18n) {
233+
// @ts-expect-error
234+
clone.i18n = {};
235+
}
236+
219237
// In 10.0, full-page-screenshot became a top-level property on the LHR.
220238
if (clone.audits['full-page-screenshot']) {
221239
const details = /** @type {LH.Result.FullPageScreenshot=} */ (

report/renderer/util.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,10 @@ class Util {
150150
}
151151
}
152152
}
153+
154+
// TODO: convert printf-style displayValue.
155+
// Added: #5099, v3
156+
// Removed: #6767, v4
153157
}
154158
}
155159

@@ -212,6 +216,20 @@ class Util {
212216
});
213217
}
214218

219+
// Add some minimal stuff so older reports still work.
220+
if (!clone.environment) {
221+
// @ts-expect-error
222+
clone.environment = {benchmarkIndex: 0};
223+
}
224+
if (!clone.configSettings.screenEmulation) {
225+
// @ts-expect-error
226+
clone.configSettings.screenEmulation = {};
227+
}
228+
if (!clone.i18n) {
229+
// @ts-expect-error
230+
clone.i18n = {};
231+
}
232+
215233
// In 10.0, full-page-screenshot became a top-level property on the LHR.
216234
if (clone.audits['full-page-screenshot']) {
217235
const details = /** @type {LH.Result.FullPageScreenshot=} */ (

report/test-assets/lhr-3.0.0.json

Lines changed: 2332 additions & 0 deletions
Large diffs are not rendered by default.

report/test-assets/lhr-4.3.0.json

Lines changed: 4754 additions & 0 deletions
Large diffs are not rendered by default.

report/test-assets/lhr-5.0.0.json

Lines changed: 5994 additions & 0 deletions
Large diffs are not rendered by default.

report/test-assets/lhr-6.0.0.json

Lines changed: 7222 additions & 0 deletions
Large diffs are not rendered by default.

report/test-assets/lhr-8.5.0.json

Lines changed: 8679 additions & 0 deletions
Large diffs are not rendered by default.

viewer/app/src/lighthouse-report-viewer.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -340,6 +340,8 @@ export class LighthouseReportViewer {
340340
} catch (err) {
341341
logger.error(err.message);
342342
}
343+
344+
document.dispatchEvent(new CustomEvent('lh-file-upload-test-ack'));
343345
}
344346

345347
/**

viewer/test/viewer-test-pptr.js

Lines changed: 48 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ describe('Lighthouse Viewer', () => {
3232
let browser;
3333
/** @type {import('puppeteer').Page} */
3434
let viewerPage;
35-
const pageErrors = [];
35+
let pageErrors = [];
3636

3737
const selectors = {
3838
audits: '.lh-audit, .lh-metric',
@@ -69,16 +69,29 @@ describe('Lighthouse Viewer', () => {
6969
});
7070

7171
after(async function() {
72-
// Log any page load errors encountered in case before() failed.
73-
// eslint-disable-next-line no-console
74-
if (pageErrors.length > 0) console.error(pageErrors);
75-
7672
await Promise.all([
7773
server.close(),
7874
browser && browser.close(),
7975
]);
8076
});
8177

78+
beforeEach(async function() {
79+
pageErrors = [];
80+
});
81+
82+
async function ensureNoErrors() {
83+
await viewerPage.evaluate(() => new Promise(window.requestAnimationFrame));
84+
const theErrors = pageErrors;
85+
pageErrors = [];
86+
expect(theErrors).toHaveLength(0);
87+
}
88+
89+
afterEach(async function() {
90+
// Tests should call this themselves so the failure is associated with them in the test report,
91+
// but just in case one is missed it won't hurt to repeat the check here.
92+
await ensureNoErrors();
93+
});
94+
8295
describe('Renders the flow report', () => {
8396
before(async () => {
8497
await viewerPage.goto(viewerUrl, {waitUntil: 'networkidle2', timeout: 30000});
@@ -88,7 +101,7 @@ describe('Lighthouse Viewer', () => {
88101
});
89102

90103
it('should load with no errors', async () => {
91-
assert.deepStrictEqual(pageErrors, []);
104+
await ensureNoErrors();
92105
});
93106

94107
it('renders the summary page', async () => {
@@ -100,7 +113,7 @@ describe('Lighthouse Viewer', () => {
100113
);
101114
assert.equal(scores.length, 14);
102115

103-
assert.deepStrictEqual(pageErrors, []);
116+
await ensureNoErrors();
104117
});
105118
});
106119

@@ -113,7 +126,7 @@ describe('Lighthouse Viewer', () => {
113126
});
114127

115128
it('should load with no errors', async () => {
116-
assert.deepStrictEqual(pageErrors, []);
129+
await ensureNoErrors();
117130
});
118131

119132
it('should contain all categories', async () => {
@@ -251,6 +264,30 @@ describe('Lighthouse Viewer', () => {
251264
});
252265
});
253266

267+
describe('Renders old reports', () => {
268+
[
269+
'lhr-3.0.0.json',
270+
'lhr-4.3.0.json',
271+
'lhr-5.0.0.json',
272+
'lhr-6.0.0.json',
273+
'lhr-8.5.0.json',
274+
].forEach((testFilename) => {
275+
it(`[${testFilename}] should load with no errors`, async () => {
276+
await viewerPage.goto(viewerUrl, {waitUntil: 'networkidle2', timeout: 30000});
277+
const fileInput = await viewerPage.$('#hidden-file-input');
278+
const waitForAck = viewerPage.evaluate(() =>
279+
new Promise(resolve =>
280+
document.addEventListener('lh-file-upload-test-ack', resolve, {once: true})));
281+
await fileInput.uploadFile(`${LH_ROOT}/report/test-assets/${testFilename}`);
282+
await Promise.race([
283+
waitForAck,
284+
new Promise((resolve, reject) => setTimeout(reject, 5_000)),
285+
]);
286+
await ensureNoErrors();
287+
});
288+
});
289+
});
290+
254291
describe('PSI', () => {
255292
/** @type {Partial<puppeteer.ResponseForRequest>} */
256293
let interceptedRequest;
@@ -351,7 +388,7 @@ describe('Lighthouse Viewer', () => {
351388
expect(interceptedUrl.searchParams.getAll('category').sort()).toEqual(defaultCategories);
352389

353390
// No errors.
354-
assert.deepStrictEqual(pageErrors, []);
391+
await ensureNoErrors();
355392

356393
// All categories.
357394
const categoryElementIds = await getCategoryElementsIds();
@@ -395,7 +432,7 @@ describe('Lighthouse Viewer', () => {
395432
});
396433

397434
// No errors.
398-
assert.deepStrictEqual(pageErrors, []);
435+
await ensureNoErrors();
399436
});
400437

401438
it('should handle errors from the API', async () => {
@@ -412,6 +449,7 @@ describe('Lighthouse Viewer', () => {
412449
// One error.
413450
expect(pageErrors).toHaveLength(1);
414451
expect(pageErrors[0].message).toContain('badPsiResponse error');
452+
pageErrors = [];
415453
});
416454
});
417455
});

0 commit comments

Comments
 (0)