-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathaudit-output.unit.test.ts
More file actions
198 lines (185 loc) · 4.99 KB
/
Copy pathaudit-output.unit.test.ts
File metadata and controls
198 lines (185 loc) · 4.99 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
import { describe, expect, it } from 'vitest';
import {
type AuditOutput,
type AuditOutputs,
auditOutputSchema,
auditOutputsSchema,
} from './audit-output.js';
describe('auditOutputSchema', () => {
it('should accept a valid audit output without details', () => {
expect(() =>
auditOutputSchema.parse({
slug: 'cypress-e2e-test-results',
score: 0.8,
value: 80,
displayValue: '80 %',
} satisfies AuditOutput),
).not.toThrow();
});
it('should accept a valid audit output with details issues', () => {
expect(() =>
auditOutputSchema.parse({
slug: 'speed-index',
score: 0.3,
value: 4500,
displayValue: '4.5 s',
details: {
issues: [
{
message: 'The progress chart was blocked for 4 seconds.',
severity: 'info',
},
],
},
} satisfies AuditOutput),
).not.toThrow();
});
it('should accept a valid audit output with details table', () => {
expect(() =>
auditOutputSchema.parse({
slug: 'largest-contentful-paint',
score: 0.83,
value: 3090,
displayValue: '3.1 s',
details: {
table: {
rows: [
{
selector:
'#title-card-3-1 > div.ptrack-content > a > div > img',
html: '<img class="boxart-image boxart-image-in-padded-container" src="https://my.images/P93zeAjL9blBKk5xKz.jpg?r=942" alt="How to change your mind">',
},
],
},
},
} satisfies AuditOutput),
).not.toThrow();
});
it('should accept a valid audit output with details table and issues', () => {
expect(() =>
auditOutputSchema.parse({
slug: 'speed-index',
score: 0.3,
value: 4500,
displayValue: '4.5 s',
details: {
issues: [
{
message: 'The progress chart was blocked for 4 seconds.',
severity: 'info',
},
],
table: {
rows: [
{
selector:
'#title-card-3-1 > div.ptrack-content > a > div > img',
html: '<img class="boxart-image boxart-image-in-padded-container" src="https://my.images/P93zeAjL9blBKk5xKz.jpg?r=942" alt="How to change your mind">',
},
],
},
},
} satisfies AuditOutput),
).not.toThrow();
});
it('should accept a valid audit output with a score target', () => {
expect(() =>
auditOutputSchema.parse({
slug: 'total-blocking-time',
score: 0.91,
scoreTarget: 0.9,
value: 183.5,
displayValue: '180 ms',
} satisfies AuditOutput),
).not.toThrow();
});
it('should accept a decimal value', () => {
expect(() =>
auditOutputSchema.parse({
slug: 'first-meaningful-paint',
score: 1,
value: 883.4785,
} satisfies AuditOutput),
).not.toThrow();
});
it('should throw for a negative value', () => {
expect(() =>
auditOutputSchema.parse({
slug: 'speed-index',
score: 1,
value: -100,
} satisfies AuditOutput),
).toThrow('too_small');
});
it('should throw for a score outside 0-1 range', () => {
expect(() =>
auditOutputSchema.parse({
slug: 'maximum-layout-shift',
score: 9,
value: 90,
} satisfies AuditOutput),
).toThrow('too_big');
});
it('should throw for a missing score', () => {
expect(() =>
auditOutputSchema.parse({
slug: 'total-blocking-time',
value: 2500,
}),
).toThrow('invalid_type');
});
it('should throw for an invalid slug', () => {
expect(() =>
auditOutputSchema.parse({
slug: 'Lighthouse',
value: 2500,
}),
).toThrow('slug has to follow the pattern');
});
});
describe('auditOutputsSchema', () => {
it('should accept a valid audit output array', () => {
expect(() =>
auditOutputsSchema.parse([
{
slug: 'total-blocking-time',
value: 2500,
score: 0.8,
},
{
slug: 'speed-index',
score: 1,
value: 250,
},
] satisfies AuditOutputs),
).not.toThrow();
});
it('should accept an empty output array', () => {
expect(() =>
auditOutputsSchema.parse([] satisfies AuditOutputs),
).not.toThrow();
});
it('should throw for duplicate outputs', () => {
expect(() =>
auditOutputsSchema.parse([
{
slug: 'total-blocking-time',
value: 2500,
score: 0.8,
},
{
slug: 'speed-index',
score: 1,
value: 250,
},
{
slug: 'total-blocking-time',
value: 4300,
score: 0.75,
},
] satisfies AuditOutputs),
).toThrow(
String.raw`Audit slugs must be unique, but received duplicates: \"total-blocking-time\"`,
);
});
});