diff --git a/lighthouse-core/report/report-generator.js b/lighthouse-core/report/report-generator.js index 8127686aa144..9996be60f9ea 100644 --- a/lighthouse-core/report/report-generator.js +++ b/lighthouse-core/report/report-generator.js @@ -66,19 +66,25 @@ class ReportGenerator { const separator = ','; /** @param {string} value @return {string} */ const escape = value => `"${value.replace(/"/g, '""')}"`; + /** @param {Array} 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) diff --git a/lighthouse-core/test/report/report-generator-test.js b/lighthouse-core/test/report/report-generator-test.js index 5f4e86dc2539..bcc649a83205 100644 --- a/lighthouse-core/test/report/report-generator-test.js +++ b/lighthouse-core/test/report/report-generator-test.js @@ -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\\" " `); @@ -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');