Skip to content

Commit 0f1084e

Browse files
committed
feat: list relevant audits on category failure
1 parent 176049b commit 0f1084e

File tree

1 file changed

+23
-8
lines changed

1 file changed

+23
-8
lines changed

src/index.js

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -47,32 +47,47 @@ const belowThreshold = (id, expected, categories) => {
4747
return actual < expected;
4848
};
4949

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.magenta(
5354
category.title,
5455
)} to be greater or equal to ${chalk.green(expected)} but got ${chalk.red(
5556
category.score !== null ? category.score : 'unknown',
5657
)}`;
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.magenta(
64+
audit.title,
65+
)}' received a score of ${chalk.yellow(audit.score)}`;
66+
})
67+
.join('\n');
68+
69+
return categoryAudits ? `${categoryError}\n${categoryAudits}` : categoryError;
5770
};
5871

5972
const formatResults = ({ results, thresholds }) => {
6073
const categories = Object.values(
6174
results.lhr.categories,
62-
).map(({ title, score, id }) => ({ title, score, id }));
75+
).map(({ title, score, id, auditRefs }) => ({ title, score, id, auditRefs }));
6376

6477
const categoriesBelowThreshold = Object.entries(
6578
thresholds,
6679
).filter(([id, expected]) => belowThreshold(id, expected, categories));
6780

6881
const errors = categoriesBelowThreshold.map(([id, expected]) =>
69-
getError(id, expected, categories),
82+
getError(id, expected, categories, results.lhr.audits),
7083
);
7184

7285
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] } : {}),
7691
})),
7792
};
7893

0 commit comments

Comments
 (0)