diff --git a/packages/core/src/lib/implementation/persist.ts b/packages/core/src/lib/implementation/persist.ts index 624fdece2..f138da0ac 100644 --- a/packages/core/src/lib/implementation/persist.ts +++ b/packages/core/src/lib/implementation/persist.ts @@ -37,7 +37,7 @@ export async function persistReport( let scoredReport; if (format.includes('stdout')) { scoredReport = scoreReport(report); - reportToStdout(scoredReport); + console.log(reportToStdout(scoredReport)); } // collect physical format outputs diff --git a/packages/utils/src/lib/__snapshots__/report-to-stdout.spec.ts.snap b/packages/utils/src/lib/__snapshots__/report-to-stdout.spec.ts.snap index 59a25a3a1..896c29ce4 100644 --- a/packages/utils/src/lib/__snapshots__/report-to-stdout.spec.ts.snap +++ b/packages/utils/src/lib/__snapshots__/report-to-stdout.spec.ts.snap @@ -1,8 +1,8 @@ // Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html exports[`report-to-stdout > should contain all sections when using the fixture report 1`] = ` -" -Code PushUp Report - @code-pushup/core@0.0.1 +"Code PushUp Report - @code-pushup/core@0.0.1 + ESLint audits @@ -62,6 +62,8 @@ ESLint audits ● Disallow unescaped HTML entities from appearing in markup 0 ● Disallow usage of unknown DOM property 0 ● Enforce ES5 or ES6 class for returning value in render function 0 + + Lighthouse audits ● First Contentful Paint 1.2 s @@ -70,6 +72,7 @@ Lighthouse audits ● Cumulative Layout Shift 0 ● Speed Index 1.2 s + Categories ┌────────────────┬───────┬────────┐ diff --git a/packages/utils/src/lib/report-to-stdout.spec.ts b/packages/utils/src/lib/report-to-stdout.spec.ts index 999d51943..adaa6a406 100644 --- a/packages/utils/src/lib/report-to-stdout.spec.ts +++ b/packages/utils/src/lib/report-to-stdout.spec.ts @@ -1,23 +1,11 @@ -import { afterEach, beforeEach, describe } from 'vitest'; +import { describe } from 'vitest'; import { report } from '@code-pushup/models/testing'; -import { mockConsole, unmockConsole } from '../../test'; import { reportToStdout } from './report-to-stdout'; import { scoreReport } from './scoring'; -let logs: string[]; - describe('report-to-stdout', () => { - beforeEach(async () => { - logs = []; - mockConsole(msg => logs.push(msg)); - }); - afterEach(() => { - unmockConsole(); - }); - it('should contain all sections when using the fixture report', () => { - reportToStdout(scoreReport(report())); - const logOutput = logs.join('\n'); + const logOutput = reportToStdout(scoreReport(report())); // eslint-disable-next-line no-control-regex expect(logOutput.replace(/\u001B\[\d+m/g, '')).toMatchSnapshot(); }); diff --git a/packages/utils/src/lib/report-to-stdout.ts b/packages/utils/src/lib/report-to-stdout.ts index 40e6920b5..2a8dd4368 100644 --- a/packages/utils/src/lib/report-to-stdout.ts +++ b/packages/utils/src/lib/report-to-stdout.ts @@ -13,32 +13,33 @@ import { reportRawOverviewTableHeaders, } from './utils'; -let output = ''; - -const addLine = (text = '') => (output += text + '\n'); -const print = (text: string) => console.log(text); - -export function reportToStdout(report: ScoredReport): void { - addLine(); - reportToHeaderSection(report); - addLine(); - reportToDetailSection(report); - addLine(); - reportToOverviewSection(report); - addLine(); - addLine(`${FOOTER_PREFIX} ${CODE_PUSHUP_DOMAIN}`); - - print(output); +function addLine(line = ''): string { + return line + '\n'; } -function reportToHeaderSection(report: ScoredReport): void { +export function reportToStdout(report: ScoredReport): string { + let output = ''; + + output += addLine(reportToHeaderSection(report)); + output += addLine(); + output += addLine(reportToDetailSection(report)); + output += addLine(reportToOverviewSection(report)); + output += addLine(`${FOOTER_PREFIX} ${CODE_PUSHUP_DOMAIN}`); + + return output; +} + +function reportToHeaderSection(report: ScoredReport): string { const { packageName, version } = report; - addLine(`${chalk.bold(reportHeadlineText)} - ${packageName}@${version}`); + return `${chalk.bold(reportHeadlineText)} - ${packageName}@${version}`; } -function reportToOverviewSection({ categories, plugins }: ScoredReport): void { - addLine(chalk.magentaBright.bold('Categories')); - addLine(); +function reportToOverviewSection({ + categories, + plugins, +}: ScoredReport): string { + let output = addLine(chalk.magentaBright.bold('Categories')); + output += addLine(); const table = new Table({ head: reportRawOverviewTableHeaders, @@ -56,15 +57,20 @@ function reportToOverviewSection({ categories, plugins }: ScoredReport): void { ]), ); - addLine(table.toString()); + output += addLine(table.toString()); + + return output; } -function reportToDetailSection(report: ScoredReport): void { +function reportToDetailSection(report: ScoredReport): string { const { plugins } = report; + let output = ''; + plugins.forEach(({ title, audits }) => { - addLine(chalk.magentaBright.bold(`${title} audits`)); - addLine(); + output += addLine(); + output += addLine(chalk.magentaBright.bold(`${title} audits`)); + output += addLine(); const ui = cliui({ width: 80 }); @@ -87,8 +93,11 @@ function reportToDetailSection(report: ScoredReport): void { ); }); - addLine(ui.toString()); + output += addLine(ui.toString()); + output += addLine(); }); + + return output; } function withColor({ score, text }: { score: number; text?: string }) {