-
Notifications
You must be signed in to change notification settings - Fork 429
/
Copy pathaxe-puppeteer-matcher.js
77 lines (69 loc) · 2.04 KB
/
axe-puppeteer-matcher.js
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
/*
* Jest expect.extend
*
* Please add the following to your Jest config.
*
* setupFilesAfterEnv: ["<rootDir>/tests/axe-puppeteer.js"]
*
* // Deprecated Jest < 24
* "setupTestFrameworkScriptFile": "<rootDir>/tests/axe-puppeteer.js",
*
*/
import AxePuppeteer from 'axe-puppeteer';
function outputViolations(violations) {
return violations
.map(({ help, helpUrl, id, nodes }) => {
let output =
`RULE: ${id}: ${help}\n` +
'EXPECTED no violations in the following DOM nodes:\n';
nodes.forEach((node) => {
if (node.any.length !== 0) {
output += ` ${node.target}\n`;
output +=
' This is a list of checks that, if none pass, will generate a violation. Fix ANY of the following to pass:\n';
node.any.forEach((check) => {
output += ` - ${check.message}\n`;
});
}
if (node.all.length !== 0) {
output += ` ${node.target}\n`;
output +=
' This is a list of checks that, if any fail, will generate a violation. Fix ALL of the following to pass:\n';
node.all.forEach((check) => {
output += ` - ${check.message}.\n`;
});
}
if (node.none.length) {
output += ` ${node.target}\n`;
output +=
' This is a list of checks that, if any pass, will generate a violation. Fix ALL of the following to pass:\n';
node.none.forEach((check) => {
output += ` - ${check.message}.\n`;
});
}
});
output += `Need more help? Please visit: ${helpUrl}\n`;
return output;
})
.join('\n');
}
async function toHaveNoViolations(page, { config = {} } = {}) {
const axe = new AxePuppeteer(page);
axe.include('#root').configure(config);
const { violations } = await axe.analyze();
const pass = violations.length === 0;
const message = pass
? () => {}
: () =>
`${this.utils.matcherHint('.toHaveNoViolations')}\n\n` +
`Expected story to pass aXe accessibility tests.\n` +
`The following violations were found:\n` +
`${outputViolations(violations)}`;
return {
message,
pass,
};
}
expect.extend({
toHaveNoViolations,
});