Last active
March 26, 2018 21:28
-
-
Save dfreedm/f5a3f23bc2bba29706e4747710f3bbdf to your computer and use it in GitHub Desktop.
Axe A11y Reporting for Mocha
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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')); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!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