|
1 | 1 | import { describe, expect } from 'vitest'; |
2 | | -import { report } from '@code-pushup/models/testing'; |
| 2 | +import { REPORT_MOCK } from '@code-pushup/testing-utils'; |
3 | 3 | import { calculateScore, scoreReport } from './scoring'; |
4 | 4 |
|
5 | 5 | describe('calculateScore', () => { |
6 | | - // https://googlechrome.github.io/lighthouse/scorecalc/#FCP=1200&LCP=1450&TBT=0&CLS=0&SI=1200&TTI=1200&FMP=1200&device=desktop&version=10.3.0 |
7 | | - const refs = [ |
8 | | - { slug: 'first-contentful-paint', weight: 1 }, |
9 | | - { slug: 'largest-contentful-paint', weight: 2.5 }, |
10 | | - { slug: 'speed-index', weight: 1 }, |
11 | | - { slug: 'total-blocking-time', weight: 3 }, |
12 | | - { slug: 'cumulative-layout-shift', weight: 2.5 }, |
13 | | - ]; |
14 | | - const scores = { |
15 | | - 'first-contentful-paint': 0.75, |
16 | | - 'largest-contentful-paint': 0.82, |
17 | | - 'speed-index': 0.93, |
18 | | - 'total-blocking-time': 1, |
19 | | - 'cumulative-layout-shift': 1, |
20 | | - 'unminified-javascript': 1, |
21 | | - 'uses-long-cache-ttl': 0, |
22 | | - }; |
23 | | - const scoreFn = (ref: (typeof refs)[number]) => |
24 | | - scores[ref.slug as keyof typeof scores]; |
| 6 | + it('should calculate the same score for one reference', () => { |
| 7 | + expect( |
| 8 | + calculateScore( |
| 9 | + [{ slug: 'first-contentful-paint', weight: 1, score: 0.75 }], |
| 10 | + ref => ref.score, |
| 11 | + ), |
| 12 | + ).toBe(0.75); |
| 13 | + }); |
25 | 14 |
|
26 | | - it('Lighthouse performance group', () => { |
27 | | - expect(calculateScore(refs, scoreFn)).toBeCloseTo(0.92); |
| 15 | + it('should calculate correct score for multiple references', () => { |
| 16 | + expect( |
| 17 | + calculateScore( |
| 18 | + [ |
| 19 | + { slug: 'first-contentful-paint', weight: 3, score: 0 }, |
| 20 | + { slug: 'cumulative-layout-shift', weight: 1, score: 1 }, |
| 21 | + ], |
| 22 | + ref => ref.score, |
| 23 | + ), |
| 24 | + ).toBe(0.25); |
28 | 25 | }); |
29 | 26 |
|
30 | | - it('ignore refs with weight 0', () => { |
| 27 | + it('should remove zero-weight reference from score calculation', () => { |
31 | 28 | expect( |
32 | 29 | calculateScore( |
33 | 30 | [ |
34 | | - ...refs, |
35 | | - { slug: 'unminified-javascript', weight: 0 }, |
36 | | - { slug: 'uses-long-cache-ttl', weight: 0 }, |
| 31 | + { slug: 'first-contentful-paint', weight: 3, score: 0 }, |
| 32 | + { slug: 'cumulative-layout-shift', weight: 1, score: 1 }, |
| 33 | + { slug: 'speed-index', weight: 0, score: 1 }, |
37 | 34 | ], |
38 | | - scoreFn, |
| 35 | + ref => ref.score, |
| 36 | + ), |
| 37 | + ).toBe(0.25); |
| 38 | + }); |
| 39 | + |
| 40 | + it('should calculate correct score for realistic Lighthouse values', () => { |
| 41 | + // https://googlechrome.github.io/lighthouse/scorecalc/#FCP=1200&LCP=1450&TBT=0&CLS=0&SI=1200&TTI=1200&FMP=1200&device=desktop&version=10.3.0 |
| 42 | + expect( |
| 43 | + calculateScore( |
| 44 | + [ |
| 45 | + { slug: 'first-contentful-paint', weight: 1, score: 0.75 }, |
| 46 | + { slug: 'largest-contentful-paint', weight: 2.5, score: 0.82 }, |
| 47 | + { slug: 'speed-index', weight: 1, score: 0.93 }, |
| 48 | + { slug: 'total-blocking-time', weight: 3, score: 1 }, |
| 49 | + { slug: 'cumulative-layout-shift', weight: 2.5, score: 1 }, |
| 50 | + ], |
| 51 | + ref => ref.score, |
39 | 52 | ), |
40 | 53 | ).toBeCloseTo(0.92); |
41 | 54 | }); |
42 | 55 |
|
43 | | - it('throws for empty refs (weight 0 is ignored internally)', () => { |
| 56 | + it('should throw for an empty reference array', () => { |
44 | 57 | expect(() => |
45 | | - calculateScore([{ slug: 'uses-long-cache-ttl', weight: 0 }], scoreFn), |
46 | | - ).toThrow( |
47 | | - '0 division for score. This can be caused by refs only weighted with 0 or empty refs', |
48 | | - ); |
| 58 | + calculateScore<{ weight: number }>([], ref => ref.weight), |
| 59 | + ).toThrow('0 division for score'); |
49 | 60 | }); |
50 | 61 |
|
51 | | - it('works for 0 scores', () => { |
52 | | - expect( |
53 | | - calculateScore([{ slug: 'uses-long-cache-ttl', weight: 1 }], scoreFn), |
54 | | - ).toBe(0); |
| 62 | + it('should throw for a reference array full of zero weights', () => { |
| 63 | + expect(() => |
| 64 | + calculateScore( |
| 65 | + [ |
| 66 | + { slug: 'first-contentful-paint', weight: 0, score: 0 }, |
| 67 | + { slug: 'cumulative-layout-shift', weight: 0, score: 1 }, |
| 68 | + ], |
| 69 | + ref => ref.weight, |
| 70 | + ), |
| 71 | + ).toThrow('0 division for score'); |
55 | 72 | }); |
56 | 73 | }); |
57 | 74 |
|
58 | 75 | describe('scoreReport', () => { |
59 | | - it('should score valid report', () => { |
60 | | - const reportA = report(); |
61 | | - const scoredReport = scoreReport(reportA); |
| 76 | + it('should correctly score a valid report', () => { |
| 77 | + /* |
| 78 | + The report mock has the following data: |
| 79 | + Test results |
| 80 | + -> Cypress E2E: score 0.5 | weight 2 |
| 81 | + -> CyCT: score 1 | weight 1 |
| 82 | + => Overall score = 1.5/3 = 0.5 |
| 83 | + |
| 84 | + Bug prevention |
| 85 | + -> TypeScript ESLint group: score 0.25 weight 8 |
| 86 | + -> ts-eslint-typing score 0 weight 3 |
| 87 | + -> ts-eslint-enums score 1 weight 1 |
| 88 | + -> ts-eslint-experimental score 0 weight 0 |
| 89 | +
|
| 90 | + -> ESLint Jest naming: score 1 weight 1 |
| 91 | + -> TypeScript ESLint functional: score 0 weight 1 |
| 92 | + -> ESLint Cypress: score 1 weight 0 |
| 93 | + => Overall score = 3/10 = 0.3 |
| 94 | + */ |
62 | 95 |
|
63 | | - // enriched audits |
64 | | - expect(scoredReport.plugins[1]?.audits[0]?.plugin).toBe('lighthouse'); |
65 | | - expect(scoredReport.plugins[1]?.audits[0]?.slug).toBe( |
66 | | - 'first-contentful-paint', |
| 96 | + expect(scoreReport(REPORT_MOCK)).toEqual( |
| 97 | + expect.objectContaining({ |
| 98 | + categories: [ |
| 99 | + expect.objectContaining({ slug: 'test-results', score: 0.625 }), |
| 100 | + expect.objectContaining({ slug: 'bug-prevention', score: 0.3 }), |
| 101 | + ], |
| 102 | + }), |
67 | 103 | ); |
68 | | - expect(scoredReport.plugins[1]?.audits[0]?.score).toBeCloseTo(0.76); |
69 | | - // enriched and scored groups |
70 | | - expect(scoredReport.plugins[1]?.groups[0]?.plugin).toBe('lighthouse'); |
71 | | - expect(scoredReport.plugins[1]?.groups[0]?.slug).toBe('performance'); |
72 | | - expect(scoredReport.plugins[1]?.groups[0]?.score).toBeCloseTo(0.92); |
73 | | - // enriched and scored categories |
74 | | - expect(scoredReport.categories[0]?.slug).toBe('performance'); |
75 | | - expect(scoredReport?.categories?.[0]?.score).toBeCloseTo(0.92); |
76 | | - expect(scoredReport.categories[1]?.slug).toBe('bug-prevention'); |
77 | | - expect(scoredReport?.categories?.[1]?.score).toBeCloseTo(0.68); |
78 | | - expect(scoredReport.categories[2]?.slug).toBe('code-style'); |
79 | | - expect(scoredReport?.categories?.[2]?.score).toBeCloseTo(0.54); |
80 | 104 | }); |
81 | 105 | }); |
0 commit comments