Skip to content

Commit b738288

Browse files
committed
feat: skip perfect scores in stdout summary
1 parent 02298e9 commit b738288

File tree

5 files changed

+58
-57
lines changed

5 files changed

+58
-57
lines changed

packages/core/src/lib/collect-and-persist.unit.test.ts

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
1-
import { describe } from 'vitest';
1+
import { type MockInstance, describe } from 'vitest';
22
import {
33
ISO_STRING_REGEXP,
44
MINIMAL_CONFIG_MOCK,
55
MINIMAL_REPORT_MOCK,
66
getLogMessages,
77
} from '@code-pushup/test-utils';
88
import {
9+
type ScoredReport,
910
logStdoutSummary,
1011
scoreReport,
1112
sortReport,
@@ -29,12 +30,17 @@ vi.mock('./implementation/persist', () => ({
2930
}));
3031

3132
describe('collectAndPersistReports', () => {
33+
let logStdoutSpy: MockInstance<
34+
[report: ScoredReport, verbose?: boolean],
35+
void
36+
>;
37+
3238
beforeEach(() => {
33-
vi.spyOn(utils, 'logStdoutSummary');
39+
logStdoutSpy = vi.spyOn(utils, 'logStdoutSummary');
3440
});
3541

36-
afterEach(() => {
37-
vi.clearAllMocks();
42+
afterAll(() => {
43+
logStdoutSpy.mockRestore();
3844
});
3945

4046
it('should call collect and persistReport with correct parameters in non-verbose mode', async () => {
@@ -105,19 +111,10 @@ describe('collectAndPersistReports', () => {
105111
expect(logPersistedResults).toHaveBeenCalled();
106112
});
107113

108-
it('should print a summary to stdout`', async () => {
109-
const verboseConfig: CollectAndPersistReportsOptions = {
110-
categories: [],
111-
...MINIMAL_CONFIG_MOCK,
112-
persist: {
113-
outputDir: 'output',
114-
filename: 'report',
115-
format: ['md'],
116-
},
117-
verbose: true,
118-
progress: false,
119-
};
120-
await collectAndPersistReports(verboseConfig);
114+
it('should print a summary to stdout', async () => {
115+
await collectAndPersistReports(
116+
MINIMAL_CONFIG_MOCK as CollectAndPersistReportsOptions,
117+
);
121118
const logs = getLogMessages(ui().logger);
122119
expect(logs.at(-2)).toContain('Made with ❤ by code-pushup.dev');
123120
});

packages/utils/src/lib/reports/__snapshots__/report-stdout-all-perfect-scores.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ Code PushUp Report - @code-pushup/[email protected]
33

44
ESLint audits
55

6-
● All audits have perfect scores
6+
... All 47 audits have perfect scores ...
77

88

99
Lighthouse audits

packages/utils/src/lib/reports/log-stdout-summary.integration.test.ts

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -63,20 +63,21 @@ describe('logStdoutSummary', () => {
6363
const report = reportMock();
6464
const reportWithPerfectScores = {
6565
...report,
66-
plugins: report.plugins.map((plugin, index) => ({
67-
...plugin,
68-
audits: plugin.audits.map(audit => ({
69-
...audit,
70-
score: index === 0 ? 1 : audit.score,
71-
})),
72-
})),
66+
plugins: report.plugins.map((plugin, index) =>
67+
index > 0
68+
? plugin
69+
: {
70+
...plugin,
71+
audits: plugin.audits.map(audit => ({ ...audit, score: 1 })),
72+
},
73+
),
7374
};
7475

7576
logStdoutSummary(sortReport(scoreReport(reportWithPerfectScores)));
7677

7778
const output = logs.join('\n');
7879

79-
expect(output).toContain('All audits have perfect scores');
80+
expect(output).toContain('All 47 audits have perfect scores');
8081
await expect(removeColorCodes(output)).toMatchFileSnapshot(
8182
'__snapshots__/report-stdout-all-perfect-scores.txt',
8283
);

packages/utils/src/lib/reports/log-stdout-summary.ts

Lines changed: 33 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -44,50 +44,53 @@ export function logPlugins(
4444
: audits.filter(({ score }) => score !== 1);
4545
const diff = audits.length - filteredAudits.length;
4646

47-
logAuditRows(title, filteredAudits);
47+
logAudits(title, filteredAudits);
4848

4949
if (diff > 0) {
50-
const message =
50+
const notice =
5151
filteredAudits.length === 0
52-
? 'All audits have perfect scores'
52+
? `... All ${diff} audits have perfect scores ...`
5353
: `... ${diff} audits with perfect scores omitted for brevity ...`;
54-
55-
ui().row([
56-
{ text: '●', width: 2, padding: [0, 1, 0, 0] },
57-
// eslint-disable-next-line no-magic-numbers
58-
{ text: message, padding: [0, 3, 0, 0] },
59-
]);
54+
logRow(1, notice);
6055
}
6156
log();
6257
});
6358
}
6459

65-
function logAuditRows(title: string, audits: AuditReport[]): void {
60+
function logAudits(pluginTitle: string, audits: AuditReport[]): void {
6661
log();
67-
log(bold.magentaBright(`${title} audits`));
62+
log(bold.magentaBright(`${pluginTitle} audits`));
6863
log();
69-
audits.forEach((audit: AuditReport) => {
70-
ui().row([
71-
{
72-
text: applyScoreColor({ score: audit.score, text: '●' }),
73-
width: 2,
74-
padding: [0, 1, 0, 0],
75-
},
76-
{
77-
text: audit.title,
78-
// eslint-disable-next-line no-magic-numbers
79-
padding: [0, 3, 0, 0],
80-
},
81-
{
82-
text: cyanBright(audit.displayValue || `${audit.value}`),
83-
// eslint-disable-next-line no-magic-numbers
84-
width: 20,
85-
padding: [0, 0, 0, 0],
86-
},
87-
]);
64+
audits.forEach(({ score, title, displayValue, value }) => {
65+
logRow(score, title, displayValue || `${value}`);
8866
});
8967
}
9068

69+
function logRow(score: number, title: string, value?: string): void {
70+
ui().row([
71+
{
72+
text: applyScoreColor({ score, text: '●' }),
73+
width: 2,
74+
padding: [0, 1, 0, 0],
75+
},
76+
{
77+
text: title,
78+
// eslint-disable-next-line no-magic-numbers
79+
padding: [0, 3, 0, 0],
80+
},
81+
...(value
82+
? [
83+
{
84+
text: cyanBright(value),
85+
// eslint-disable-next-line no-magic-numbers
86+
width: 20,
87+
padding: [0, 0, 0, 0],
88+
},
89+
]
90+
: []),
91+
]);
92+
}
93+
9194
export function logCategories({ categories, plugins }: ScoredReport): void {
9295
const hAlign = (idx: number) => (idx === 0 ? 'left' : 'right');
9396

packages/utils/src/lib/reports/log-stdout-summary.unit.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,7 @@ describe('logPlugins', () => {
234234
false,
235235
);
236236
const output = logs.join('\n');
237-
expect(output).toContain('All audits have perfect scores');
237+
expect(output).toContain('All 2 audits have perfect scores');
238238
});
239239

240240
it('should log original audits when verbose is false and no audits have perfect scores', () => {

0 commit comments

Comments
 (0)