/
index.js
93 lines (83 loc) · 2.65 KB
/
index.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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
const fs = require('fs')
const axe = fs.readFileSync('node_modules/axe-core/axe.min.js', 'utf8')
export const injectAxe = () => {
cy.window({ log: false }).then(window => {
window.eval(axe)
})
}
export const configureAxe = (configurationOptions = {}) => {
cy.window({ log: false }).then(win => {
return win.axe.configure(configurationOptions)
})
}
const checkA11y = (
context,
options,
violationCallback,
skipFailures = false
) => {
cy.window({ log: false })
.then(win => {
if (isEmptyObjectorNull(context)) context = undefined
if (isEmptyObjectorNull(options)) options = undefined
if (isEmptyObjectorNull(violationCallback)) violationCallback = undefined
const { includedImpacts, ...axeOptions } = options || {}
return win.axe
.run(context || win.document, axeOptions)
.then(({ violations }) => {
return includedImpacts &&
Array.isArray(includedImpacts) &&
Boolean(includedImpacts.length)
? violations.filter(v => includedImpacts.includes(v.impact))
: violations
})
})
.then(violations => {
if (violations.length) {
if (violationCallback) {
violationCallback(violations)
}
cy.wrap(violations, { log: false }).each(v => {
const selectors = v.nodes
.reduce((acc, node) => acc.concat(node.target), [])
.join(', ')
Cypress.log({
$el: Cypress.$(selectors),
name: 'a11y error!',
consoleProps: () => v,
message: `${v.id} on ${v.nodes.length} Node${
v.nodes.length === 1 ? '' : 's'
}`
})
})
}
return cy.wrap(violations, { log: false })
})
.then(violations => {
if (!skipFailures) {
assert.equal(
violations.length,
0,
`${violations.length} accessibility violation${
violations.length === 1 ? '' : 's'
} ${violations.length === 1 ? 'was' : 'were'} detected`
)
} else {
if (violations.length) {
Cypress.log({
name: 'a11y violation summary',
message: `${violations.length} accessibility violation${
violations.length === 1 ? '' : 's'
} ${violations.length === 1 ? 'was' : 'were'} detected`
})
}
}
})
}
Cypress.Commands.add('injectAxe', injectAxe)
Cypress.Commands.add('configureAxe', configureAxe)
Cypress.Commands.add('checkA11y', checkA11y)
function isEmptyObjectorNull(value) {
if (value == null) return true
return Object.entries(value).length === 0 && value.constructor === Object
}