Skip to content

Commit 9e2f0c3

Browse files
authored
Merge pull request #87 from mfrances17/fail-a11y-on-ci
Ensure CI fails if there are a11y violations or incomplete test runs
2 parents 7d3a88e + a38dbe6 commit 9e2f0c3

File tree

1 file changed

+41
-80
lines changed

1 file changed

+41
-80
lines changed

lib/reporter.js

Lines changed: 41 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -5,21 +5,20 @@ const { getHTMLReport } = require("./htmlReport");
55

66
const report = {};
77
let completedUrls = 0;
8+
let exitCode = 0;
89

910
function makeReportMessage(type, error) {
10-
const messages = error.nodes.map((node) => node.any[0]);
11+
const messages = error.nodes?.map((node) => node.any?.[0]) ?? [];
1112

1213
return {
1314
message: JSON.stringify(messages, null, 2),
1415
type: `${type}: ${error.id}`,
1516
};
1617
}
18+
const getIdReport = (errors = []) => errors.map((err) => err.id).sort().join(", ") || "None";
1719

1820
function makeReport(aggregate) {
19-
const totalTime = Object.values(report).reduce(
20-
(prev, cur) => (prev += cur.time),
21-
0
22-
);
21+
const totalTime = Object.values(report).reduce((prev, cur) => prev + cur.time, 0);
2322
const suites = [];
2423

2524
if (aggregate) {
@@ -41,7 +40,7 @@ function makeReport(aggregate) {
4140
suites.push({
4241
name: key,
4342
timestamp: new Date(),
44-
time: val.reduce((prev, cur) => (prev += cur.time), 0),
43+
time: val.reduce((prev, cur) => prev + cur.time, 0),
4544
testCases: Object.values(val).map((testCase) => ({
4645
name: testCase.name,
4746
failures: testCase.violations.map((error) =>
@@ -81,37 +80,20 @@ function makeReport(aggregate) {
8180
}
8281

8382
function writeConsoleError(suites) {
84-
const failedSuites = suites.filter((suite) => {
85-
for (let testCase of suite.testCases) {
86-
if (testCase.failures.length > 0) {
87-
return true;
88-
}
89-
if (testCase.incomplete && testCase.incomplete.length > 0) {
90-
return true;
91-
}
92-
}
93-
return false;
94-
});
83+
const failedSuites = suites.filter(suite =>
84+
suite.testCases.some(testCase => testCase.failures.length > 0 || testCase.incomplete?.length > 0)
85+
);
9586
if (failedSuites.length > 0) {
9687
console.log("\nReport summary");
97-
failedSuites.forEach((suite) => {
88+
failedSuites.forEach(suite => {
9889
console.error(`================= ${suite.name} ===================`);
99-
suite.testCases.forEach((testCase) => {
100-
const hasFailures = testCase.failures.length > 0;
101-
const hasIncomplete =
102-
testCase.incomplete && testCase.incomplete.length > 0;
103-
if (hasFailures || hasIncomplete) {
90+
suite.testCases
91+
.filter(testCase => testCase.failures.length || testCase.incomplete?.length)
92+
.forEach(testCase => {
10493
console.error(` ${testCase.name}`);
105-
}
106-
if (hasFailures) {
107-
console.error(` Violations`);
108-
testCase.failures.forEach((error) => console.error(error.message));
109-
}
110-
if (hasIncomplete) {
111-
console.error(` Incomplete`);
112-
testCase.incomplete.forEach((error) => console.error(error.message));
113-
}
114-
});
94+
if (testCase.failures.length) console.error(" Violations", testCase.failures.map(e => e.message));
95+
if (testCase.incomplete?.length) console.error(" Incomplete", testCase.incomplete.map(e => e.message));
96+
});
11597
});
11698
}
11799
}
@@ -124,44 +106,29 @@ function writeCoverage(aggregate, exitCodeOnFailure) {
124106
reportTitle: "Patternfly axe report",
125107
});
126108

127-
// writeConsoleError(suites);
128-
129109
fs.copySync(path.join(__dirname, '../report/dist'), "coverage", { recursive: true });
130110
fs.writeFileSync("coverage/results.json", JSON.stringify(report, null, 2));
131111
fs.writeFileSync("coverage/coverage.xml", junitXml);
132112
fs.writeFileSync("coverage/report.html", htmlReport);
133113

134-
// Exit code
135-
if (exitCodeOnFailure) {
136-
if (
137-
suites.find((suite) =>
138-
suite.testCases.find((test) => test.failures.length > 0)
139-
)
140-
) {
141-
return 1;
142-
}
143-
if (
144-
suites.find((suite) =>
145-
suite.testCases.find(
146-
(test) => test.incomplete && test.incomplete.length > 0
147-
)
148-
)
149-
) {
150-
return 2;
151-
}
114+
// Only force exit code here so fail occurs after all files are tested, otherwise will fail on first violation
115+
if (exitCode > 0) {
116+
console.error(`\nExiting with code ${exitCode}`);
117+
process.exit(exitCode);
152118
}
153-
154-
return 0;
155119
}
156120

157-
const addAnyField = (error) =>
158-
error.nodes.forEach((node) => {
121+
const addAnyField = (error) => {
122+
if (!error.nodes) return;
123+
124+
error.nodes.forEach(node => {
159125
// Usually the any field is prefilled with some useful stuff, but if not, add it
160-
node.any = node.any || [];
126+
node.any = Array.isArray(node.any) ? node.any : [];
161127
node.any[0] = node.any[0] || { relatedNodes: [] };
162-
// Add the html that most erorrs report
128+
// Add the html that most errors report
163129
node.any[0].relatedNodes.push({ html: node.html });
164130
});
131+
};
165132

166133
function recordPage(
167134
prefix,
@@ -174,23 +141,20 @@ function recordPage(
174141
index,
175142
screenshotFile,
176143
axeOptions,
177-
context
144+
context,
178145
) {
179-
const elapsed = process.hrtime(startTime);
180146
// Report program is doing something
181147
console.log(`\n${++completedUrls}/${numUrls}`.padEnd(10, " "), url);
182148

183-
if (axeResults.incomplete) {
184-
axeResults.incomplete.forEach(addAnyField);
185-
}
186-
if (axeResults.violations) {
187-
axeResults.violations.forEach(addAnyField);
188-
}
149+
axeResults?.incomplete?.forEach(addAnyField);
150+
axeResults?.violations?.forEach(addAnyField);
189151

152+
const elapsedTime = process.hrtime(startTime).reduce((s, ns) => s + ns / 1e9);
153+
190154
const urlReport = {
191-
incomplete: axeResults.incomplete,
192-
violations: axeResults.violations,
193-
time: elapsed[0] + elapsed[1] / 1000000000,
155+
incomplete: axeResults.incomplete || [],
156+
violations: axeResults.violations || [],
157+
time: elapsedTime,
194158
screenshotFile,
195159
prefix,
196160
url,
@@ -202,19 +166,16 @@ function recordPage(
202166

203167
report[`${index}_${url}`] = urlReport;
204168

205-
const getIdReport = (errors) =>
206-
errors
207-
.map((err) => err.id)
208-
.sort()
209-
.join(", ");
210-
211-
if (urlReport.violations.length > 0) {
169+
const hasViolations = urlReport.violations.length > 0;
170+
const hasIncomplete = !ignoreIncomplete && urlReport.incomplete.length > 0;
171+
172+
if (hasViolations) {
212173
console.log("violations:", getIdReport(urlReport.violations));
174+
exitCode = 1;
213175
}
214-
const numIncomplete =
215-
!ignoreIncomplete && urlReport.incomplete && urlReport.incomplete.length;
216-
if (numIncomplete) {
176+
if (hasIncomplete) {
217177
console.error("incomplete:", getIdReport(urlReport.incomplete));
178+
exitCode = exitCode === 1 ? 1 : 2;
218179
}
219180
}
220181

0 commit comments

Comments
 (0)