Skip to content

Commit

Permalink
feat: skip perfect scores in stdout summary
Browse files Browse the repository at this point in the history
  • Loading branch information
hanna-skryl committed Oct 11, 2024
1 parent 02298e9 commit b738288
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 57 deletions.
31 changes: 14 additions & 17 deletions packages/core/src/lib/collect-and-persist.unit.test.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import { describe } from 'vitest';
import { type MockInstance, describe } from 'vitest';
import {
ISO_STRING_REGEXP,
MINIMAL_CONFIG_MOCK,
MINIMAL_REPORT_MOCK,
getLogMessages,
} from '@code-pushup/test-utils';
import {
type ScoredReport,
logStdoutSummary,
scoreReport,
sortReport,
Expand All @@ -29,12 +30,17 @@ vi.mock('./implementation/persist', () => ({
}));

describe('collectAndPersistReports', () => {
let logStdoutSpy: MockInstance<
[report: ScoredReport, verbose?: boolean],
void
>;

beforeEach(() => {
vi.spyOn(utils, 'logStdoutSummary');
logStdoutSpy = vi.spyOn(utils, 'logStdoutSummary');
});

afterEach(() => {
vi.clearAllMocks();
afterAll(() => {
logStdoutSpy.mockRestore();
});

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

it('should print a summary to stdout`', async () => {
const verboseConfig: CollectAndPersistReportsOptions = {
categories: [],
...MINIMAL_CONFIG_MOCK,
persist: {
outputDir: 'output',
filename: 'report',
format: ['md'],
},
verbose: true,
progress: false,
};
await collectAndPersistReports(verboseConfig);
it('should print a summary to stdout', async () => {
await collectAndPersistReports(
MINIMAL_CONFIG_MOCK as CollectAndPersistReportsOptions,
);
const logs = getLogMessages(ui().logger);
expect(logs.at(-2)).toContain('Made with ❤ by code-pushup.dev');
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ Code PushUp Report - @code-pushup/[email protected]

ESLint audits

● All audits have perfect scores
... All 47 audits have perfect scores ...


Lighthouse audits
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,20 +63,21 @@ describe('logStdoutSummary', () => {
const report = reportMock();
const reportWithPerfectScores = {
...report,
plugins: report.plugins.map((plugin, index) => ({
...plugin,
audits: plugin.audits.map(audit => ({
...audit,
score: index === 0 ? 1 : audit.score,
})),
})),
plugins: report.plugins.map((plugin, index) =>
index > 0
? plugin
: {
...plugin,
audits: plugin.audits.map(audit => ({ ...audit, score: 1 })),
},
),
};

logStdoutSummary(sortReport(scoreReport(reportWithPerfectScores)));

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

expect(output).toContain('All audits have perfect scores');
expect(output).toContain('All 47 audits have perfect scores');
await expect(removeColorCodes(output)).toMatchFileSnapshot(
'__snapshots__/report-stdout-all-perfect-scores.txt',
);
Expand Down
63 changes: 33 additions & 30 deletions packages/utils/src/lib/reports/log-stdout-summary.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,50 +44,53 @@ export function logPlugins(
: audits.filter(({ score }) => score !== 1);
const diff = audits.length - filteredAudits.length;

logAuditRows(title, filteredAudits);
logAudits(title, filteredAudits);

if (diff > 0) {
const message =
const notice =
filteredAudits.length === 0
? 'All audits have perfect scores'
? `... All ${diff} audits have perfect scores ...`
: `... ${diff} audits with perfect scores omitted for brevity ...`;

ui().row([
{ text: '●', width: 2, padding: [0, 1, 0, 0] },
// eslint-disable-next-line no-magic-numbers
{ text: message, padding: [0, 3, 0, 0] },
]);
logRow(1, notice);
}
log();
});
}

function logAuditRows(title: string, audits: AuditReport[]): void {
function logAudits(pluginTitle: string, audits: AuditReport[]): void {
log();
log(bold.magentaBright(`${title} audits`));
log(bold.magentaBright(`${pluginTitle} audits`));
log();
audits.forEach((audit: AuditReport) => {
ui().row([
{
text: applyScoreColor({ score: audit.score, text: '●' }),
width: 2,
padding: [0, 1, 0, 0],
},
{
text: audit.title,
// eslint-disable-next-line no-magic-numbers
padding: [0, 3, 0, 0],
},
{
text: cyanBright(audit.displayValue || `${audit.value}`),
// eslint-disable-next-line no-magic-numbers
width: 20,
padding: [0, 0, 0, 0],
},
]);
audits.forEach(({ score, title, displayValue, value }) => {
logRow(score, title, displayValue || `${value}`);

Check failure on line 65 in packages/utils/src/lib/reports/log-stdout-summary.ts

View workflow job for this annotation

GitHub Actions / Code PushUp

<✓> Code coverage | Branch coverage

1st branch is not taken in any test case.
});
}

function logRow(score: number, title: string, value?: string): void {
ui().row([
{
text: applyScoreColor({ score, text: '●' }),
width: 2,
padding: [0, 1, 0, 0],
},
{
text: title,
// eslint-disable-next-line no-magic-numbers
padding: [0, 3, 0, 0],
},
...(value
? [
{
text: cyanBright(value),
// eslint-disable-next-line no-magic-numbers
width: 20,
padding: [0, 0, 0, 0],
},
]
: []),
]);
}

export function logCategories({ categories, plugins }: ScoredReport): void {
const hAlign = (idx: number) => (idx === 0 ? 'left' : 'right');

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ describe('logPlugins', () => {
false,
);
const output = logs.join('\n');
expect(output).toContain('All audits have perfect scores');
expect(output).toContain('All 2 audits have perfect scores');
});

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

0 comments on commit b738288

Please sign in to comment.