Skip to content

Instantly share code, notes, and snippets.

@dfreedm
Last active March 26, 2018 21:28
Show Gist options
  • Save dfreedm/f5a3f23bc2bba29706e4747710f3bbdf to your computer and use it in GitHub Desktop.
Save dfreedm/f5a3f23bc2bba29706e4747710f3bbdf to your computer and use it in GitHub Desktop.
Axe A11y Reporting for Mocha
async function axeReport(dom, config = {}) {
const {cleanup, axeConfig} = config;
const {violations} = await axe.run(dom, axeConfig || {
runOnly: ['wcag2a', 'wcag2aa', 'section508'],
// we don't care about passing tests
resultTypes: ['violations']
});
await cleanup();
if (!violations.length) {
return;
}
const errorMessage = ['Accessibility Violations', '---'];
for (const violation of violations) {
errorMessage.push(violation.help);
for (const node of violation.nodes) {
errorMessage.push(node.failureSummary);
errorMessage.push(node.html);
}
errorMessage.push('---');
}
throw new Error(errorMessage.join('\n'));
}
<!doctype html>
<html>
<script src="path/to/mocha.js"></script>
<script src="path/to/axe.min.js"></script>
<script src="path/to/axe-report.js"></script>
<template id="basic">
<my-element></my-element>
</template>
<script>
suite('a11y tests', function() {
test('basic', function() {
const dom = basic.content.cloneNode(true);
document.body.appendChild(dom);
return axeReport(dom, {cleanup(){dom.remove();}});
});
});
</script>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment