Skip to content

Commit

Permalink
refactor(utils): return string from reportToStdout
Browse files Browse the repository at this point in the history
  • Loading branch information
IKatsuba committed Nov 9, 2023
1 parent a82538b commit 3c086c6
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 43 deletions.
2 changes: 1 addition & 1 deletion packages/core/src/lib/implementation/persist.ts
Expand Up @@ -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
Expand Down
@@ -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
Expand Down Expand Up @@ -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
Expand All @@ -70,6 +72,7 @@ Lighthouse audits
● Cumulative Layout Shift 0
● Speed Index 1.2 s
Categories
┌────────────────┬───────┬────────┐
Expand Down
16 changes: 2 additions & 14 deletions 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();
});
Expand Down
61 changes: 35 additions & 26 deletions packages/utils/src/lib/report-to-stdout.ts
Expand Up @@ -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,
Expand All @@ -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 });

Expand All @@ -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 }) {
Expand Down

0 comments on commit 3c086c6

Please sign in to comment.