Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

report: show overall category scores in csv output #11404

Merged
merged 3 commits into from
Sep 11, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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