Skip to content

Commit 3c086c6

Browse files
committed
refactor(utils): return string from reportToStdout
1 parent a82538b commit 3c086c6

File tree

4 files changed

+43
-43
lines changed

4 files changed

+43
-43
lines changed

packages/core/src/lib/implementation/persist.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ export async function persistReport(
3737
let scoredReport;
3838
if (format.includes('stdout')) {
3939
scoredReport = scoreReport(report);
40-
reportToStdout(scoredReport);
40+
console.log(reportToStdout(scoredReport));
4141
}
4242

4343
// collect physical format outputs

packages/utils/src/lib/__snapshots__/report-to-stdout.spec.ts.snap

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html
22

33
exports[`report-to-stdout > should contain all sections when using the fixture report 1`] = `
4-
"
5-
Code PushUp Report - @code-pushup/[email protected]
4+
"Code PushUp Report - @code-pushup/[email protected]
5+
66
77
ESLint audits
88
@@ -62,6 +62,8 @@ ESLint audits
6262
● Disallow unescaped HTML entities from appearing in markup 0
6363
● Disallow usage of unknown DOM property 0
6464
● Enforce ES5 or ES6 class for returning value in render function 0
65+
66+
6567
Lighthouse audits
6668
6769
● First Contentful Paint 1.2 s
@@ -70,6 +72,7 @@ Lighthouse audits
7072
● Cumulative Layout Shift 0
7173
● Speed Index 1.2 s
7274
75+
7376
Categories
7477
7578
┌────────────────┬───────┬────────┐

packages/utils/src/lib/report-to-stdout.spec.ts

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,11 @@
1-
import { afterEach, beforeEach, describe } from 'vitest';
1+
import { describe } from 'vitest';
22
import { report } from '@code-pushup/models/testing';
3-
import { mockConsole, unmockConsole } from '../../test';
43
import { reportToStdout } from './report-to-stdout';
54
import { scoreReport } from './scoring';
65

7-
let logs: string[];
8-
96
describe('report-to-stdout', () => {
10-
beforeEach(async () => {
11-
logs = [];
12-
mockConsole(msg => logs.push(msg));
13-
});
14-
afterEach(() => {
15-
unmockConsole();
16-
});
17-
187
it('should contain all sections when using the fixture report', () => {
19-
reportToStdout(scoreReport(report()));
20-
const logOutput = logs.join('\n');
8+
const logOutput = reportToStdout(scoreReport(report()));
219
// eslint-disable-next-line no-control-regex
2210
expect(logOutput.replace(/\u001B\[\d+m/g, '')).toMatchSnapshot();
2311
});

packages/utils/src/lib/report-to-stdout.ts

Lines changed: 35 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -13,32 +13,33 @@ import {
1313
reportRawOverviewTableHeaders,
1414
} from './utils';
1515

16-
let output = '';
17-
18-
const addLine = (text = '') => (output += text + '\n');
19-
const print = (text: string) => console.log(text);
20-
21-
export function reportToStdout(report: ScoredReport): void {
22-
addLine();
23-
reportToHeaderSection(report);
24-
addLine();
25-
reportToDetailSection(report);
26-
addLine();
27-
reportToOverviewSection(report);
28-
addLine();
29-
addLine(`${FOOTER_PREFIX} ${CODE_PUSHUP_DOMAIN}`);
30-
31-
print(output);
16+
function addLine(line = ''): string {
17+
return line + '\n';
3218
}
3319

34-
function reportToHeaderSection(report: ScoredReport): void {
20+
export function reportToStdout(report: ScoredReport): string {
21+
let output = '';
22+
23+
output += addLine(reportToHeaderSection(report));
24+
output += addLine();
25+
output += addLine(reportToDetailSection(report));
26+
output += addLine(reportToOverviewSection(report));
27+
output += addLine(`${FOOTER_PREFIX} ${CODE_PUSHUP_DOMAIN}`);
28+
29+
return output;
30+
}
31+
32+
function reportToHeaderSection(report: ScoredReport): string {
3533
const { packageName, version } = report;
36-
addLine(`${chalk.bold(reportHeadlineText)} - ${packageName}@${version}`);
34+
return `${chalk.bold(reportHeadlineText)} - ${packageName}@${version}`;
3735
}
3836

39-
function reportToOverviewSection({ categories, plugins }: ScoredReport): void {
40-
addLine(chalk.magentaBright.bold('Categories'));
41-
addLine();
37+
function reportToOverviewSection({
38+
categories,
39+
plugins,
40+
}: ScoredReport): string {
41+
let output = addLine(chalk.magentaBright.bold('Categories'));
42+
output += addLine();
4243

4344
const table = new Table({
4445
head: reportRawOverviewTableHeaders,
@@ -56,15 +57,20 @@ function reportToOverviewSection({ categories, plugins }: ScoredReport): void {
5657
]),
5758
);
5859

59-
addLine(table.toString());
60+
output += addLine(table.toString());
61+
62+
return output;
6063
}
6164

62-
function reportToDetailSection(report: ScoredReport): void {
65+
function reportToDetailSection(report: ScoredReport): string {
6366
const { plugins } = report;
6467

68+
let output = '';
69+
6570
plugins.forEach(({ title, audits }) => {
66-
addLine(chalk.magentaBright.bold(`${title} audits`));
67-
addLine();
71+
output += addLine();
72+
output += addLine(chalk.magentaBright.bold(`${title} audits`));
73+
output += addLine();
6874

6975
const ui = cliui({ width: 80 });
7076

@@ -87,8 +93,11 @@ function reportToDetailSection(report: ScoredReport): void {
8793
);
8894
});
8995

90-
addLine(ui.toString());
96+
output += addLine(ui.toString());
97+
output += addLine();
9198
});
99+
100+
return output;
92101
}
93102

94103
function withColor({ score, text }: { score: number; text?: string }) {

0 commit comments

Comments
 (0)