Skip to content

Commit

Permalink
report(csv): add overall category scores (#11404)
Browse files Browse the repository at this point in the history
  • Loading branch information
pujitha7 authored Sep 11, 2020
1 parent b142f83 commit a1571ba
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 8 deletions.
20 changes: 13 additions & 7 deletions lighthouse-core/report/report-generator.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,19 +66,25 @@ class ReportGenerator {
const separator = ',';
/** @param {string} value @return {string} */
const escape = value => `"${value.replace(/"/g, '""')}"`;
/** @param {Array<string | number>} row @return {string[]} */
const rowFormatter = row => row.map(value => value.toString()).map(escape);

// Possible TODO: tightly couple headers and row values
const header = ['requestedUrl', 'finalUrl', 'category', 'name', 'title', 'type', 'score'];
const table = Object.values(lhr.categories).map(category => {
return category.auditRefs.map(auditRef => {
const table = Object.keys(lhr.categories).map(categoryId => {
const rows = [];
const category = lhr.categories[categoryId];
const overallCategoryScore = category.score === null ? -1 : category.score;
rows.push(rowFormatter([lhr.requestedUrl, lhr.finalUrl, category.title,
`${categoryId}-score`, `Overall ${category.title} Category Score`, 'numeric',
overallCategoryScore]));
return rows.concat(category.auditRefs.map(auditRef => {
const audit = lhr.audits[auditRef.id];
// CSV validator wants all scores to be numeric, use -1 for now
const numericScore = audit.score === null ? -1 : audit.score;
return [lhr.requestedUrl, lhr.finalUrl, category.title, audit.id, audit.title,
audit.scoreDisplayMode, numericScore]
.map(value => value.toString())
.map(escape);
});
return rowFormatter([lhr.requestedUrl, lhr.finalUrl, category.title, audit.id, audit.title,
audit.scoreDisplayMode, numericScore]);
}));
});

return [header].concat(...table)
Expand Down
12 changes: 11 additions & 1 deletion lighthouse-core/test/report/report-generator-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,9 @@ describe('ReportGenerator', () => {

const lines = csvOutput.split('\n');
expect(lines.length).toBeGreaterThan(100);
expect(lines.slice(0, 2).join('\n')).toMatchInlineSnapshot(`
expect(lines.slice(0, 3).join('\n')).toMatchInlineSnapshot(`
"requestedUrl,finalUrl,category,name,title,type,score
\\"http://localhost:10200/dobetterweb/dbw_tester.html\\",\\"http://localhost:10200/dobetterweb/dbw_tester.html\\",\\"Performance\\",\\"performance-score\\",\\"Overall Performance Category Score\\",\\"numeric\\",\\"0.64\\"
\\"http://localhost:10200/dobetterweb/dbw_tester.html\\",\\"http://localhost:10200/dobetterweb/dbw_tester.html\\",\\"Performance\\",\\"first-contentful-paint\\",\\"First Contentful Paint\\",\\"numeric\\",\\"0.51\\"
"
`);
Expand All @@ -117,6 +118,15 @@ describe('ReportGenerator', () => {
}
});

it('creates CSV for results including overall category scores', () => {
const csvOutput = ReportGenerator.generateReport(sampleResults, 'csv');
expect(csvOutput).toContain('performance-score');
expect(csvOutput).toContain('accessibility-score');
expect(csvOutput).toContain('best-practices-score');
expect(csvOutput).toContain('seo-score');
expect(csvOutput).toContain('pwa-score');
});

it('writes extended info', () => {
const htmlOutput = ReportGenerator.generateReport(sampleResults, 'html');
const outputCheck = new RegExp('dobetterweb/dbw_tester.css', 'i');
Expand Down

0 comments on commit a1571ba

Please sign in to comment.