Skip to content

Commit 1109cb3

Browse files
committed
keep runtimeError out of shortSummary
1 parent 3225e8c commit 1109cb3

File tree

3 files changed

+78
-70
lines changed

3 files changed

+78
-70
lines changed

src/format.js

Lines changed: 19 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -33,47 +33,35 @@ const getError = (id, expected, categories, audits) => {
3333
return { message: categoryError, details: categoryAudits };
3434
};
3535

36-
const formatShortSummary = ({ categories, runtimeError }) => {
37-
if (runtimeError) {
38-
return runtimeError.message;
39-
}
36+
const formatShortSummary = (categories) => {
4037
return categories
4138
.map(({ title, score }) => `${title}: ${Math.round(score * 100)}`)
4239
.join(', ');
4340
};
4441

4542
const formatResults = ({ results, thresholds }) => {
46-
const hasScores = !results.lhr.runtimeError;
43+
const runtimeError = results.lhr.runtimeError;
4744

4845
const categories = Object.values(results.lhr.categories).map(
4946
({ title, score, id, auditRefs }) => ({ title, score, id, auditRefs }),
5047
);
5148

52-
const categoriesBelowThreshold =
53-
hasScores &&
54-
Object.entries(thresholds).filter(([id, expected]) =>
55-
belowThreshold(id, expected, categories),
56-
);
57-
58-
const errors =
59-
hasScores &&
60-
categoriesBelowThreshold.map(([id, expected]) =>
61-
getError(id, expected, categories, results.lhr.audits),
62-
);
63-
64-
const summary =
65-
hasScores &&
66-
categories.map(({ title, score, id }) => ({
67-
title,
68-
score,
69-
id,
70-
...(thresholds[id] ? { threshold: thresholds[id] } : {}),
71-
}));
72-
73-
const shortSummary = formatShortSummary({
74-
categories,
75-
runtimeError: results.lhr.runtimeError,
76-
});
49+
const categoriesBelowThreshold = Object.entries(thresholds).filter(
50+
([id, expected]) => belowThreshold(id, expected, categories),
51+
);
52+
53+
const errors = categoriesBelowThreshold.map(([id, expected]) =>
54+
getError(id, expected, categories, results.lhr.audits),
55+
);
56+
57+
const summary = categories.map(({ title, score, id }) => ({
58+
title,
59+
score,
60+
id,
61+
...(thresholds[id] ? { threshold: thresholds[id] } : {}),
62+
}));
63+
64+
const shortSummary = formatShortSummary(categories);
7765

7866
const formattedReport = makeReplacements(results.report);
7967

@@ -92,7 +80,7 @@ const formatResults = ({ results, thresholds }) => {
9280
minifyJS: true,
9381
});
9482

95-
return { summary, shortSummary, details, report, errors };
83+
return { summary, shortSummary, details, report, errors, runtimeError };
9684
};
9785

9886
module.exports = {

src/format.test.js

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -106,25 +106,13 @@ describe('format', () => {
106106
{ title: 'SEO', score: 0.7, id: 'seo' },
107107
{ title: 'PWA', score: 0.6, id: 'pwa' },
108108
];
109-
const runtimeError = {
110-
code: 'NO_FCP',
111-
message:
112-
'The page did not paint any content. Please ensure you keep the browser window in the foreground during the load and try again. (NO_FCP)',
113-
};
114109

115110
it('should return a shortSummary containing scores if available', () => {
116-
const shortSummary = formatShortSummary({ categories });
111+
const shortSummary = formatShortSummary(categories);
117112
expect(shortSummary).toEqual(
118113
'Performance: 100, Accessibility: 90, Best Practices: 80, SEO: 70, PWA: 60',
119114
);
120115
});
121-
122-
it('should return a shortSummary error message', () => {
123-
const shortSummary = formatShortSummary({
124-
runtimeError,
125-
});
126-
expect(shortSummary).toEqual(runtimeError.message);
127-
});
128116
});
129117

130118
describe('formatResults', () => {

src/index.js

Lines changed: 58 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -52,13 +52,21 @@ const persistResults = async ({ report, path }) => {
5252
};
5353

5454
const getUtils = ({ utils }) => {
55+
// This function checks to see if we're running within the Netlify Build system,
56+
// and if so, we use the util functions. If not, we're likely running locally
57+
// so fall back using console.log to emulate the output.
58+
59+
// If available, fails the Netlify build with the supplied message
60+
// https://docs.netlify.com/integrations/build-plugins/create-plugins/#error-reporting
5561
const failBuild =
5662
(utils && utils.build && utils.build.failBuild) ||
5763
((message, { error } = {}) => {
5864
console.error(message, error && error.message);
5965
process.exitCode = 1;
6066
});
6167

68+
// If available, displays the summary in the Netlify UI Deploy Summary section
69+
// https://docs.netlify.com/integrations/build-plugins/create-plugins/#logging
6270
const show =
6371
(utils && utils.status && utils.status.show) ||
6472
(({ summary }) => console.log(summary));
@@ -94,10 +102,11 @@ const runAudit = async ({
94102
if (error) {
95103
return { error };
96104
} else {
97-
const { summary, shortSummary, details, report, errors } = formatResults({
98-
results,
99-
thresholds,
100-
});
105+
const { summary, shortSummary, details, report, errors, runtimeError } =
106+
formatResults({
107+
results,
108+
thresholds,
109+
});
101110

102111
if (output_path) {
103112
await persistResults({ report, path: join(serveDir, output_path) });
@@ -109,6 +118,7 @@ const runAudit = async ({
109118
details,
110119
report,
111120
errors,
121+
runtimeError,
112122
};
113123
}
114124
} catch (error) {
@@ -161,28 +171,45 @@ const processResults = ({ data, errors }) => {
161171
return {
162172
error: err,
163173
summary: data
164-
.map(({ path, url, summary, shortSummary, details, report }) => {
165-
const obj = { report, details };
174+
.map(
175+
({
176+
path,
177+
url,
178+
summary,
179+
shortSummary,
180+
details,
181+
report,
182+
runtimeError,
183+
}) => {
184+
const obj = { report, details };
166185

167-
if (summary) {
168-
obj.summary = summary.reduce((acc, item) => {
169-
acc[item.id] = Math.round(item.score * 100);
170-
return acc;
171-
}, {});
172-
}
186+
if (!runtimeError && summary) {
187+
obj.summary = summary.reduce((acc, item) => {
188+
acc[item.id] = Math.round(item.score * 100);
189+
return acc;
190+
}, {});
191+
}
173192

174-
if (path) {
175-
obj.path = path;
176-
reports.push(obj);
177-
return `Summary for path '${chalk.magenta(path)}': ${shortSummary}`;
178-
}
179-
if (url) {
180-
obj.url = url;
181-
reports.push(obj);
182-
return `Summary for url '${chalk.magenta(url)}': ${shortSummary}`;
183-
}
184-
return `${shortSummary}`;
185-
})
193+
if (runtimeError) {
194+
reports.push(obj);
195+
return `Error testing '${chalk.magenta(path || url)}': ${
196+
runtimeError.message
197+
}`;
198+
}
199+
200+
if (path) {
201+
obj.path = path;
202+
reports.push(obj);
203+
return `Summary for path '${chalk.magenta(path)}': ${shortSummary}`;
204+
}
205+
if (url) {
206+
obj.url = url;
207+
reports.push(obj);
208+
return `Summary for url '${chalk.magenta(url)}': ${shortSummary}`;
209+
}
210+
return `${shortSummary}`;
211+
},
212+
)
186213
.join('\n'),
187214
extraData: reports,
188215
};
@@ -204,7 +231,7 @@ module.exports = {
204231
const allErrors = [];
205232
const data = [];
206233
for (const { serveDir, path, url, thresholds, output_path } of audits) {
207-
const { errors, summary, shortSummary, details, report } =
234+
const { errors, summary, shortSummary, details, report, runtimeError } =
208235
await runAudit({
209236
serveDir,
210237
path,
@@ -213,9 +240,13 @@ module.exports = {
213240
output_path,
214241
settings,
215242
});
216-
if (summary) {
243+
244+
if (summary && !runtimeError) {
217245
console.log({ results: summary });
218246
}
247+
if (runtimeError) {
248+
console.log({ runtimeError });
249+
}
219250

220251
const fullPath = [serveDir, path].join('/');
221252
if (report) {
@@ -239,6 +270,7 @@ module.exports = {
239270
shortSummary,
240271
details,
241272
report,
273+
runtimeError,
242274
});
243275
}
244276

0 commit comments

Comments
 (0)