@@ -47,32 +47,47 @@ const belowThreshold = (id, expected, categories) => {
47
47
return actual < expected ;
48
48
} ;
49
49
50
- const getError = ( id , expected , results ) => {
51
- const category = results . find ( ( c ) => c . id === id ) ;
52
- return `Expected category ${ chalk . magenta (
50
+ const getError = ( id , expected , categories , audits ) => {
51
+ const category = categories . find ( ( c ) => c . id === id ) ;
52
+
53
+ const categoryError = `Expected category ${ chalk . cyan (
53
54
category . title ,
54
55
) } to be greater or equal to ${ chalk . green ( expected ) } but got ${ chalk . red (
55
56
category . score !== null ? category . score : 'unknown' ,
56
57
) } `;
58
+
59
+ const categoryAudits = category . auditRefs
60
+ . filter ( ( { weight, id } ) => weight > 0 && audits [ id ] . score < 1 )
61
+ . map ( ( ref ) => {
62
+ const audit = audits [ ref . id ] ;
63
+ return ` '${ chalk . cyan (
64
+ audit . title ,
65
+ ) } ' received a score of ${ chalk . yellow ( audit . score ) } `;
66
+ } )
67
+ . join ( '\n' ) ;
68
+
69
+ return { message : categoryError , details : categoryAudits } ;
57
70
} ;
58
71
59
72
const formatResults = ( { results, thresholds } ) => {
60
73
const categories = Object . values (
61
74
results . lhr . categories ,
62
- ) . map ( ( { title, score, id } ) => ( { title, score, id } ) ) ;
75
+ ) . map ( ( { title, score, id, auditRefs } ) => ( { title, score, id, auditRefs } ) ) ;
63
76
64
77
const categoriesBelowThreshold = Object . entries (
65
78
thresholds ,
66
79
) . filter ( ( [ id , expected ] ) => belowThreshold ( id , expected , categories ) ) ;
67
80
68
81
const errors = categoriesBelowThreshold . map ( ( [ id , expected ] ) =>
69
- getError ( id , expected , categories ) ,
82
+ getError ( id , expected , categories , results . lhr . audits ) ,
70
83
) ;
71
84
72
85
const summary = {
73
- results : categories . map ( ( cat ) => ( {
74
- ...cat ,
75
- ...( thresholds [ cat . id ] ? { threshold : thresholds [ cat . id ] } : { } ) ,
86
+ results : categories . map ( ( { title, score, id } ) => ( {
87
+ title,
88
+ score,
89
+ id,
90
+ ...( thresholds [ id ] ? { threshold : thresholds [ id ] } : { } ) ,
76
91
} ) ) ,
77
92
} ;
78
93
@@ -86,8 +101,8 @@ const formatResults = ({ results, thresholds }) => {
86
101
const getUtils = ( { utils } ) => {
87
102
const failBuild =
88
103
( utils && utils . build && utils . build . failBuild ) ||
89
- ( ( message , { error } ) => {
90
- console . error ( message , error . message ) ;
104
+ ( ( message , { error } = { } ) => {
105
+ console . error ( message , error && error . message ) ;
91
106
process . exitCode = 1 ;
92
107
} ) ;
93
108
@@ -126,34 +141,53 @@ const runAudit = async ({ path, url, thresholds }) => {
126
141
return {
127
142
summary,
128
143
shortSummary,
129
- error : errors . length > 0 ? new Error ( `\n ${ errors . join ( '\n' ) } ` ) : false ,
144
+ errors,
130
145
} ;
131
146
}
132
147
} catch ( error ) {
133
148
return { error } ;
134
149
}
135
150
} ;
136
151
152
+ const prefixString = ( { path, url, str } ) => {
153
+ if ( path ) {
154
+ return `\n${ chalk . red ( 'Error' ) } for directory '${ chalk . cyan (
155
+ path ,
156
+ ) } ':\n${ str } `;
157
+ } else if ( url ) {
158
+ return `\n${ chalk . red ( 'Error' ) } for url '${ chalk . cyan ( url ) } ':\n${ str } ` ;
159
+ } else {
160
+ return `\n${ str } ` ;
161
+ }
162
+ } ;
163
+
137
164
const processResults = ( { summaries, errors } ) => {
138
165
if ( errors . length > 0 ) {
166
+ const error = errors . reduce (
167
+ ( acc , { path, url, errors } ) => {
168
+ const message = prefixString ( {
169
+ path,
170
+ url,
171
+ str : errors . map ( ( e ) => e . message ) . join ( '\n' ) ,
172
+ } ) ;
173
+ const details = prefixString ( {
174
+ path,
175
+ url,
176
+ str : errors . map ( ( e ) => `${ e . message } \n${ e . details } ` ) . join ( '\n' ) ,
177
+ } ) ;
178
+
179
+ return {
180
+ message : `${ acc . message } \n${ message } ` ,
181
+ details : `${ acc . details } \n${ details } ` ,
182
+ } ;
183
+ } ,
184
+ {
185
+ message : '' ,
186
+ details : '' ,
187
+ } ,
188
+ ) ;
139
189
return {
140
- error : new Error (
141
- errors
142
- . map ( ( { path, url, error } ) => {
143
- if ( path ) {
144
- return `\n${ chalk . red ( 'Error' ) } for directory '${ chalk . magenta (
145
- path ,
146
- ) } ': ${ error . message } `;
147
- }
148
- if ( url ) {
149
- return `\n${ chalk . red ( 'Error' ) } for url '${ chalk . magenta (
150
- url ,
151
- ) } ': ${ error . message } `;
152
- }
153
- return `\n${ error . message } ` ;
154
- } )
155
- . join ( '\n' ) ,
156
- ) ,
190
+ error,
157
191
} ;
158
192
} else {
159
193
return {
@@ -182,31 +216,41 @@ module.exports = {
182
216
inputs,
183
217
} ) ;
184
218
185
- const errors = [ ] ;
219
+ const allErrors = [ ] ;
186
220
const summaries = [ ] ;
187
221
for ( const { path, url, thresholds } of audits ) {
188
- const { error , summary, shortSummary } = await runAudit ( {
222
+ const { errors , summary, shortSummary } = await runAudit ( {
189
223
path,
190
224
url,
191
225
thresholds,
192
226
} ) ;
193
227
if ( summary ) {
194
228
console . log ( summary ) ;
195
229
}
196
- if ( error ) {
197
- errors . push ( { path, url, error } ) ;
230
+ if ( Array . isArray ( errors ) && errors . length > 0 ) {
231
+ allErrors . push ( { path, url, errors } ) ;
198
232
} else {
199
233
summaries . push ( { path, url, summary : shortSummary } ) ;
200
234
}
201
235
}
202
236
203
- const { error, summary } = processResults ( { summaries, errors, show } ) ;
237
+ const { error, summary } = processResults ( {
238
+ summaries,
239
+ errors : allErrors ,
240
+ show,
241
+ } ) ;
242
+
204
243
if ( error ) {
205
244
throw error ;
206
245
}
207
246
show ( { summary } ) ;
208
247
} catch ( error ) {
209
- failBuild ( chalk . red ( 'Failed with error:\n' ) , { error } ) ;
248
+ if ( error . details ) {
249
+ console . error ( error . details ) ;
250
+ failBuild ( `${ chalk . red ( 'Failed with error:\n' ) } ${ error . message } ` ) ;
251
+ } else {
252
+ failBuild ( `${ chalk . red ( 'Failed with error:\n' ) } ` , { error } ) ;
253
+ }
210
254
}
211
255
} ,
212
256
} ;
0 commit comments