Skip to content

Commit 2bee85e

Browse files
committed
feat(utils): improve reports diff formatting
1 parent 3fe94c4 commit 2bee85e

File tree

4 files changed

+69
-6
lines changed

4 files changed

+69
-6
lines changed

packages/utils/src/lib/reports/__snapshots__/generate-md-reports-diff.integration.test.ts.snap

+54-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html
22

3-
exports[`generateMdReportsDiff > should format Markdown comment summarizing changes between reports 1`] = `
3+
exports[`generateMdReportsDiff > should format Markdown comment for improved reports diff 1`] = `
44
"# Code PushUp
55
66
🙌 Code PushUp report has **improved** – compared target commit \`0123456\` with source commit \`abcdef0\`.
@@ -46,7 +46,59 @@ exports[`generateMdReportsDiff > should format Markdown comment summarizing chan
4646
|Lighthouse|Largest Contentful Paint|🟨 **1.4 s**|🟨 1.5 s|<span style="color: green">▼ **-8%**</span>|
4747
|Lighthouse|Speed Index|🟩 **1.1 s**|🟩 1.2 s|<span style="color: green">▼ **-4%**</span>|
4848
49-
Other 40 audits are unchanged.
49+
40 other audits are unchanged.
50+
51+
</details>
52+
"
53+
`;
54+
55+
exports[`generateMdReportsDiff > should format Markdown comment for mixed reports diff 1`] = `
56+
"# Code PushUp
57+
58+
😐 Code PushUp report has both **improved and regressed** – compared target commit \`0123456\` with source commit \`abcdef0\`.
59+
60+
## 🏷️ Categories
61+
62+
|🏷️ Category|⭐ Current score|⭐ Previous score|🗠 Score change|
63+
|:--|:--:|:--:|:--:|
64+
|Performance|🟢 **94**|🟢 92|<span style="color: green">▲ **+2**</span>|
65+
|Bug prevention|🟡 **63**|🟡 68|<span style="color: red">▼ **-5**</span>|
66+
67+
<details>
68+
<summary>1 other category is unchanged.</summary>
69+
70+
- Code style: 🟡 **54**
71+
72+
</details>
73+
74+
75+
## 🎗️ Groups
76+
77+
<details>
78+
<summary>👍 <strong>1</strong> group improved</summary>
79+
80+
|🔌 Plugin|🎗️ Group|⭐ Current score|⭐ Previous score|🗠 Score change|
81+
|:--|:--|:--:|:--:|:--:|
82+
|Lighthouse|Performance|🟢 **94**|🟢 92|<span style="color: green">▲ **+2**</span>|
83+
84+
1 other group is unchanged.
85+
86+
</details>
87+
88+
89+
## 🛡️ Audits
90+
91+
<details>
92+
<summary>👍 <strong>3</strong> audits improved, 👎 <strong>1</strong> audit regressed</summary>
93+
94+
|🔌 Plugin|🛡️ Audit|📏 Current value|📏 Previous value|🗠 Value change|
95+
|:--|:--|:--:|:--:|:--:|
96+
|ESLint|Disallow unused variables|🟥 **1 error**|🟩 passed|<span style="color: red">▲ **+∞%**</span>|
97+
|Lighthouse|First Contentful Paint|🟨 **1.1 s**|🟨 1.2 s|<span style="color: green">▼ **-4%**</span>|
98+
|Lighthouse|Largest Contentful Paint|🟨 **1.4 s**|🟨 1.5 s|<span style="color: green">▼ **-8%**</span>|
99+
|Lighthouse|Speed Index|🟩 **1.1 s**|🟩 1.2 s|<span style="color: green">▼ **-4%**</span>|
100+
101+
48 other audits are unchanged.
50102
51103
</details>
52104
"
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
1-
import { reportsDiffMock } from '@code-pushup/test-utils';
1+
import { reportsDiffAltMock, reportsDiffMock } from '@code-pushup/test-utils';
22
import { generateMdReportsDiff } from './generate-md-reports-diff';
33

44
describe('generateMdReportsDiff', () => {
5-
it('should format Markdown comment summarizing changes between reports', () => {
5+
it('should format Markdown comment for improved reports diff', () => {
66
expect(generateMdReportsDiff(reportsDiffMock())).toMatchSnapshot();
77
});
8+
9+
it('should format Markdown comment for mixed reports diff', () => {
10+
expect(generateMdReportsDiff(reportsDiffAltMock())).toMatchSnapshot();
11+
});
812
});

packages/utils/src/lib/reports/generate-md-reports-diff.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -198,8 +198,9 @@ function summarizeUnchanged(
198198
{ changed, unchanged }: { changed: unknown[]; unchanged: unknown[] },
199199
): string {
200200
return [
201-
changed.length > 0 ? 'Other' : 'All',
202-
pluralizeToken(token, unchanged.length),
201+
changed.length > 0
202+
? pluralizeToken(`other ${token}`, unchanged.length)
203+
: `All of ${pluralizeToken(token, unchanged.length)}`,
203204
unchanged.length > 1 ? 'are' : 'is',
204205
'unchanged.',
205206
].join(' ');

packages/utils/src/lib/reports/utils.ts

+6
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,12 @@ export function colorByScoreDiff(text: string, diff: number): string {
6868
}
6969

7070
export function formatDiffNumber(diff: number): string {
71+
if (diff === Number.POSITIVE_INFINITY) {
72+
return '+∞';
73+
}
74+
if (diff === Number.NEGATIVE_INFINITY) {
75+
return '-∞';
76+
}
7177
if (diff > 0) {
7278
return `+${diff}`;
7379
}

0 commit comments

Comments
 (0)