-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathgenerate-md-report.unit.test.ts
More file actions
883 lines (839 loc) · 23.9 KB
/
generate-md-report.unit.test.ts
File metadata and controls
883 lines (839 loc) · 23.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
import { describe, expect, it } from 'vitest';
import type { AuditReport, Issue, Table } from '@code-pushup/models';
import { tableSection } from './formatting.js';
import {
aboutSection,
auditDetails,
auditDetailsAuditValue,
auditDetailsIssues,
auditsSection,
generateMdReport,
} from './generate-md-report.js';
import type { ScoredReport } from './types.js';
const baseScoredReport = {
date: '2025.01.01',
duration: 4200,
version: 'v1.0.0',
commit: {
message: 'ci: update action',
author: 'Michael <michael.hladky@push-based.io>',
date: new Date('2025.01.01'),
hash: '535b8e9e557336618a764f3fa45609d224a62837',
},
plugins: [
{
slug: 'lighthouse',
version: '1.0.1',
duration: 15_365,
title: 'Lighthouse',
audits: [
{
slug: 'largest-contentful-paint',
title: 'Largest Contentful Paint',
score: 0.6,
value: 2700,
},
],
},
],
categories: [
{
title: 'Performance',
slug: 'performance',
score: 0.93,
refs: [{ slug: 'largest-contentful-paint', plugin: 'lighthouse' }],
},
],
} as ScoredReport;
const baseScoredReport2 = {
date: '2025.01.01',
duration: 4200,
version: 'v1.0.0',
commit: {
message: 'ci: update action',
author: 'Michael <michael.hladky@push-based.io>',
date: new Date('2025.01.01'),
hash: '535b8e9e557336618a764f3fa45609d224a62837',
},
plugins: [
{
slug: 'lighthouse',
version: '1.0.1',
duration: 15_365,
title: 'Lighthouse',
audits: [
{
slug: 'largest-contentful-paint',
title: 'Largest Contentful Paint',
score: 0.6,
value: 2700,
},
{
slug: 'cumulative-layout-shift',
title: 'Cumulative Layout Shift',
score: 1,
value: 0,
},
],
},
],
categories: [
{
title: 'Speed',
slug: 'speed',
score: 0.93,
refs: [{ slug: 'largest-contentful-paint', plugin: 'lighthouse' }],
},
{
title: 'Visual Stability',
slug: 'visual-stability',
score: 1,
refs: [{ slug: 'cumulative-layout-shift', plugin: 'lighthouse' }],
},
],
} as ScoredReport;
// === Audit Details
describe('auditDetailsAuditValue', () => {
it('should include score', () => {
expect(
auditDetailsAuditValue({ score: 0.77 } as AuditReport).toString(),
).toMatch('(score: 77)');
});
it('should include value', () => {
expect(
auditDetailsAuditValue({ value: 125 } as AuditReport).toString(),
).toMatch('**125**');
});
it('should add score icon for scores at the beginning', () => {
expect(
auditDetailsAuditValue({ score: 0 } as AuditReport).toString(),
).toMatch(/^🟥/);
});
it('should include both display value and score when provided', () => {
expect(
auditDetailsAuditValue({
score: 1,
displayValue: '100ms',
} as AuditReport).toString(),
).toBe('🟩 **100ms** (score: 100)');
});
});
describe('auditDetailsIssues', () => {
it('should render complete section', () => {
expect(
auditDetailsIssues([
{
severity: 'info',
message: 'File `index.js` is 56Kb.',
source: {
file: 'index.js',
},
},
{
severity: 'warning',
message: 'Package license is has to be "MIT"',
source: {
file: 'package.json',
position: {
startLine: 4,
},
},
},
{
severity: 'error',
message: 'no unused vars',
source: {
file: 'index.js',
position: {
startLine: 400,
endLine: 200,
},
},
},
])?.toString(),
).toMatchSnapshot();
});
it('should return empty string for missing issues', () => {
expect(auditDetailsIssues()).toBeNull();
});
it('should include message', () => {
expect(
auditDetailsIssues([
{ message: 'File `index.js` is 56Kb too big.', severity: 'error' },
])?.toString(),
).toMatch('File `index.js` is 56Kb too big.');
});
it('should include correct severity icon', () => {
expect(
auditDetailsIssues([{ severity: 'info' } as Issue])?.toString(),
).toMatch('ℹ️');
});
it('should include source file', () => {
expect(
auditDetailsIssues([
{ source: { file: 'src/index.js' }, severity: 'error' } as Issue,
])?.toString(),
).toMatch('`src/index.js`');
});
it('should include source file linked with position (VS Code)', () => {
vi.stubEnv('TERM_PROGRAM', 'vscode');
vi.stubEnv('GITHUB_ACTIONS', 'false');
expect(
auditDetailsIssues(
[
{
source: {
file: 'src/index.js',
position: {
startLine: 4,
startColumn: 7,
},
},
severity: 'warning',
} as Issue,
],
{ outputDir: '.code-pushup' },
)?.toString(),
).toMatch('[`src/index.js`](../src/index.js#L4)');
});
it('should include formatted line information', () => {
expect(
auditDetailsIssues([
{
source: {
file: 'index.js',
position: {
startLine: 4,
endLine: 7,
},
},
severity: 'warning',
} as Issue,
])?.toString(),
).toContainMarkdownTableRow(['⚠️ _warning_', '', '`index.js`', '4-7']);
});
});
describe('tableSection', () => {
it('should render complete section', () => {
expect(
tableSection({
columns: [
{ key: 'phase', label: 'Phase' },
{ key: 'percentageLcp', label: '% of LCP' },
{ key: 'timing', label: 'Timing' },
],
rows: [
{
phase: 'TTFB',
percentageLcp: '27%',
timing: '620 ms',
},
{
phase: 'Load Delay',
percentageLcp: '25%',
timing: '580 ms',
},
{
phase: 'Load Time',
percentageLcp: '41%',
timing: '940 ms',
},
{
phase: 'Render Delay',
percentageLcp: '6%',
timing: '140 ms',
},
],
})?.toString(),
).toMatchSnapshot();
});
});
describe('auditDetails', () => {
it('should only return audit value if no details are given', () => {
expect(
auditDetails({ score: 0, value: 125 } as AuditReport).toString(),
).toBe('🟥 **125** (score: 0)\n');
});
it('should wrap details into an HTML details element if details are present', () => {
const md = auditDetails({
score: 0,
value: 0,
details: {
issues: [{ severity: 'error' }],
table: { rows: [['']] },
},
} as AuditReport).toString();
expect(md).toMatch('<details>');
expect(md).toMatch('<summary>🟥 <b>0</b> (score: 0)</summary>');
expect(md).toMatch('</details>');
});
it('should display issue section if issues are present', () => {
const md = auditDetails({
score: 0,
value: 0,
details: {
issues: [{ severity: 'warning' }],
},
} as AuditReport).toString();
expect(md).toMatch('<details>');
expect(md).toMatch('#### Issues');
expect(md).not.toMatch('#### Additional Information');
});
it('should skip issue section if empty issues array is present', () => {
const md = auditDetails({
score: 0,
value: 0,
details: {
issues: [] as Issue[],
},
} as AuditReport).toString();
expect(md).not.toMatch('<details>');
expect(md).not.toMatch('#### Issues');
expect(md).not.toMatch('#### Additional Information');
});
it('should display table section if table is present', () => {
const md = auditDetails({
slug: 'prefer-design-system-over-css-classes',
title: 'Prefer the design system over CSS classes',
score: 0.99,
value: 0,
displayValue: '190ms',
details: {
table: {
title: 'Elements',
rows: [
{
element: 'button',
},
{
element: 'div',
},
],
},
},
} as AuditReport).toString();
expect(md).toMatch('<details>');
expect(md).toMatch('#### Elements');
expect(md).toContainMarkdownTableRow(['button']);
expect(md).toContainMarkdownTableRow(['div']);
expect(md).not.toMatch('#### Issues');
});
it('should render complete details section', () => {
expect(
auditDetails({
slug: 'prefer-design-system-over-css-classes',
title: 'Prefer the design system over CSS classes',
score: 0.99,
value: 0,
displayValue: '190ms',
details: {
table: {
columns: [
{ key: 'classNames', label: 'Class Names' },
{ key: 'element' },
],
rows: [
{
classNames: '.btn, .icon',
element: 'button',
},
{
classNames: '.badge, .badge-icon',
element: 'div',
},
],
},
issues: [
{
message: 'Use design system components instead of classes',
severity: 'error',
source: {
file: 'list.component.ts',
position: {
startLine: 400,
endLine: 200,
},
},
},
{
message: 'File size is 20KB too big',
severity: 'error',
source: {
file: 'list.component.ts',
},
},
],
},
} as AuditReport).toString(),
).toMatchSnapshot();
});
});
describe('auditsSection', () => {
it('should render section heading', () => {
expect(
auditsSection({
plugins: [{ audits: [] as AuditReport[] }],
} as ScoredReport).toString(),
).toMatch('## 🛡️ Audits');
});
it('should render audit result', () => {
expect(
auditsSection({
plugins: [{ audits: [{ score: 1, value: 0 }] }],
} as ScoredReport).toString(),
).toMatch('🟩 **0** (score: 100)');
});
it('should render filtered result', () => {
const auditSection = auditsSection(
{
plugins: [
{ audits: [{ score: 1, value: 0 }] },
{ audits: [{ score: 0, value: 1 }] },
],
} as ScoredReport,
{
isScoreListed: (score: number) => score === 1,
},
).toString();
expect(auditSection).toMatch('(score: 100)');
expect(auditSection).not.toMatch('(score: 0)');
});
it('should render audit details', () => {
const md = auditsSection({
plugins: [
{
audits: [
{
details: {
issues: [{ severity: 'error' }] as Issue[],
table: { rows: [{ value: 42 }] } as Table,
},
},
],
},
],
} as ScoredReport).toString();
expect(md).toMatch('#### Issues');
expect(md).toContainMarkdownTableRow([
'Severity',
'Message',
'Source file',
'Line(s)',
]);
expect(md).toContainMarkdownTableRow(['value']);
});
it('should render audit meta information', () => {
expect(
auditsSection({
plugins: [
{
slug: 'lighthouse',
title: 'Lighthouse',
audits: [
{
slug: 'interaction-to-next-paint',
title: 'Interaction to next paint',
score: 0.74,
value: 2163,
description: 'Measures responsiveness.',
docsUrl: 'https://web.dev/inp',
},
],
},
],
} as ScoredReport).toString(),
).toMatch(`Measures responsiveness. [📖 Docs](https://web.dev/inp)`);
});
it('should render complete audit section', () => {
expect(
auditsSection({
plugins: [
{
slug: 'eslint',
title: 'ESLint',
audits: [
{
slug: 'no-any',
title: 'No any',
description: 'No unsafe any assignment',
score: 1,
value: 0,
},
{
slug: 'no-let',
title: 'No let',
score: 0,
value: 5,
displayValue: '5 errors',
},
],
},
{
slug: 'lighthouse',
title: 'Lighthouse',
audits: [
{
slug: 'largest-contentful-paint',
title: 'Largest contentful paint',
description: 'No unsafe any assignment',
docsUrl: 'https://web.dev/lcp',
score: 0.6,
value: 0,
},
],
},
],
} as ScoredReport).toString(),
).toMatchSnapshot();
});
});
// === About
describe('aboutSection', () => {
it('should return about section with h2 and created by in plain test', () => {
const md = aboutSection(baseScoredReport).toString();
expect(md).toMatch('## About');
expect(md).toMatch(
'Report was created by [Code PushUp](https://github.com/code-pushup/cli#readme) on Wed, Jan 1, 2025, 12:00 AM UTC.',
);
});
it('should return commit section', () => {
const md = aboutSection({
...baseScoredReport,
plugins: [
{
version: '1.1.1',
duration: 4200,
title: 'Lighthouse',
audits: Array.from({ length: 3 }),
},
],
categories: Array.from({ length: 3 }),
} as ScoredReport).toString();
expect(md).toContainMarkdownTableRow([
'Commit',
'Version',
'Duration',
'Plugins',
'Categories',
'Audits',
]);
expect(md).toContainMarkdownTableRow([
'ci: update action (535b8e9e557336618a764f3fa45609d224a62837)',
'`v1.0.0`',
'4.20 s',
'1',
'3',
'3',
]);
});
it('should return plugins section with content', () => {
const md = aboutSection({
...baseScoredReport,
plugins: [
{
version: '1.0.1',
duration: 15_365,
title: 'Lighthouse',
audits: Array.from({ length: 78 }),
},
{
version: '0.3.12',
duration: 260,
title: 'File Size',
audits: Array.from({ length: 2 }),
},
],
} as ScoredReport).toString();
expect(md).toContainMarkdownTableRow([
'Plugin',
'Audits',
'Version',
'Duration',
]);
expect(md).toContainMarkdownTableRow([
'Lighthouse',
'78',
'`1.0.1`',
'15.37 s',
]);
expect(md).toContainMarkdownTableRow([
'File Size',
'2',
'`0.3.12`',
'260 ms',
]);
});
it('should return full about section', () => {
const md = aboutSection({
...baseScoredReport,
plugins: [
{
version: '1.1.1',
duration: 42,
title: 'Lighthouse',
audits: Array.from({ length: 3 }),
},
],
categories: Array.from({ length: 3 }),
} as ScoredReport).toString();
expect(md).toMatchSnapshot();
});
});
// FULL REPORT
describe('generateMdReport', () => {
it('should render all sections of the report', () => {
const md = generateMdReport(baseScoredReport);
// report title
expect(md).toMatch('# Code PushUp Report');
// categories section heading
expect(md).toContainMarkdownTableRow([
'🏷 Category',
'⭐ Score',
'🛡 Audits',
]);
// categories section heading
expect(md).toMatch('## 🏷 Categories');
// audits heading
expect(md).toMatch('## 🛡️ Audits');
// about section heading
expect(md).toMatch('## About');
// plugin table
expect(md).toContainMarkdownTableRow([
'Plugin',
'Audits',
'Version',
'Duration',
]);
// made with <3
expect(md).toMatch('Made with ❤ by [Code PushUp]');
});
it('should render sections filtered by isScoreListed of the report', () => {
const md = generateMdReport(baseScoredReport2, {
isScoreListed: (score: number) => score === 1,
});
expect(md).toMatch('Visual Stability');
expect(md).toMatch('Cumulative Layout Shift');
expect(md).not.toMatch('Speed');
expect(md).not.toMatch('Largest Contentful Paint');
});
it('should skip categories section when categories are missing', () => {
const md = generateMdReport({ ...baseScoredReport, categories: undefined });
expect(md).not.toMatch('## 🏷 Categories');
expect(md).toMatch('# Code PushUp Report\n\n## 🛡️ Audits');
});
it('should render complete md report', () => {
expect(
generateMdReport({
packageName: '@code-pushup/cli',
version: 'v1.0.0',
date: 'Wed, Apr 17, 2024, 2:37 PM GMT+2',
duration: 42_356,
commit: {
message: 'ci: update action',
author: 'Michael <michael.hladky@push-based.io>',
date: new Date('2025.01.01'),
hash: '535b8e9e557336618a764f3fa45609d224a62837',
},
plugins: [
{
date: 'Wed, Apr 17, 2024, 2:38 PM GMT+2',
slug: 'lighthouse',
title: 'Lighthouse',
packageName: '@code-pushup/lighthouse',
version: '1.0.1.beta-1',
duration: 17_968,
icon: 'lighthouse',
audits: [
{
slug: 'largest-contentful-paint',
title: 'Largest Contentful Paint',
score: 0.6728,
value: 2705,
displayValue: '2,7 s',
description:
'This is the largest contentful element painted within the viewport. [Learn more about the Largest Contentful Paint element](https://developer.chrome.com/docs/lighthouse/performance/lighthouse-largest-contentful-paint/)',
docsUrl: 'https://web.dev/lcp',
details: {
table: {
columns: [
{ key: 'phase', label: 'Phase' },
{
key: 'percentageLcp',
label: '% of LCP',
align: 'left',
},
{ key: 'timing', label: 'Timing', align: 'right' },
],
rows: [
{
phase: 'TTFB',
percentageLcp: '27%',
timing: '620 ms',
},
{
phase: 'Load Delay',
percentageLcp: '25%',
timing: '580 ms',
},
{
phase: 'Load Time',
percentageLcp: '41%',
timing: '940 ms',
},
{
phase: 'Render Delay',
percentageLcp: '6%',
timing: '140 ms',
},
],
},
},
},
{
slug: 'splash-screen',
title: 'Splash Screen',
score: 1,
value: 1,
},
{
slug: 'fast-images',
title: 'Fast Images',
score: 0.97,
value: 1,
},
{
slug: 'is-crawlable',
title: 'Website is crawlable',
score: 0,
value: 0,
description:
"Search engines are unable to include your pages in search results if they don't have permission to crawl them. [Learn more about crawler directives](https://developer.chrome.com/docs/lighthouse/seo/is-crawlable/).",
},
],
groups: [
{
slug: 'performance-group',
title: 'Performance Group',
description: 'Collection of performance focused rules.',
score: 0,
docsUrl: 'https://web.dev/lighthouse#performance-group',
refs: [
{ slug: 'largest-contentful-paint', weight: 721 },
{ slug: 'fast-images', weight: 1 },
],
},
],
},
{
date: 'Wed, Apr 17, 2024, 2:38 PM GMT+2',
slug: 'eslint',
title: 'ESLint',
packageName: '@code-pushup/eslint',
version: '3.71.8',
duration: 17_968,
icon: 'eslint',
audits: [
{
slug: 'no-explicit-any',
title: 'No explicit any',
score: 0,
value: 63,
displayValue: '63 errors',
description: `The any type in TypeScript is a dangerous "escape hatch" from the type system. Using any disables many type checking rules and is generally best used only as a last resort or when prototyping code. This rule reports on explicit uses of the any keyword as a type annotation.
Preferable alternatives to any include:
If the type is known, describing it in an interface or type
If the type is not known, using the safer unknown type
TypeScript's --noImplicitAny compiler option prevents an implied any, but doesn't prevent any from being explicitly used the way this rule does.
\`\`\`ts
.eslintrc.cjs
module.exports = {
"rules": {
"@typescript-eslint/no-explicit-any": "error"
}
};
\`\`\`
`,
docsUrl: 'https://typescript-eslint.io/rules/no-explicit-any/',
details: {
issues: [
{
severity: 'info',
message: 'File `index.js` is 56Kb.',
source: {
file: 'index.js',
},
},
{
severity: 'warning',
message: 'Package license is has to be "MIT"',
source: {
file: 'package.json',
position: {
startLine: 4,
},
},
},
{
severity: 'error',
message: 'no unused vars',
source: {
file: 'index.js',
position: {
startLine: 400,
endLine: 200,
},
},
},
],
},
},
],
},
],
categories: [
{
title: 'Performance',
slug: 'performance',
score: 0.61,
refs: [
{
slug: 'performance-group',
plugin: 'lighthouse',
type: 'group',
weight: 81,
},
],
},
{
title: 'SEO',
slug: 'seo',
score: 1,
isBinary: true,
refs: [
{
slug: 'is-crawlable',
plugin: 'lighthouse',
type: 'audit',
weight: 2,
},
],
},
{
title: 'PWA',
slug: 'pwa',
score: 0,
isBinary: true,
refs: [
{
slug: 'splash-screen',
plugin: 'lighthouse',
type: 'audit',
weight: 1,
},
],
},
],
}),
).toMatchSnapshot();
});
});