UNPKG

2.76 kBJavaScriptView Raw
1/**
2@license
3Copyright (c) 2018 The Polymer Project Authors. All rights reserved.
4This code may only be used under the BSD style license found at http://polymer.github.io/LICENSE.txt
5The complete set of authors may be found at http://polymer.github.io/AUTHORS.txt
6The complete set of contributors may be found at http://polymer.github.io/CONTRIBUTORS.txt
7Code distributed by Google as part of the polymer project is also
8subject to an additional IP rights grant found at http://polymer.github.io/PATENTS.txt
9*/
10/**
11 This is an [axe-core](https://github.com/dequelabs/axe-core) reporter that returns an
12 Error containing every a11y violation for an element. Use this if you want to
13 include `axe-core` in automated Mocha tests, etc. Note that this helper does not
14 include `axe-core` for you; you must do this separately.
15
16 The `axeReport` function takes an optional second argument:
17 {cleanup: {...}, axeConfig: {...}}, where `cleanup` is a callback to be
18 called after the test is ran (so that you can remove the element from the DOM, etc)
19 and `axeConfig` are the optional extra config parameters to pass to axe.
20
21 Example (in a Mocha test):
22
23 import 'axe-core/axe.min.js';
24 import { axeReport } from 'pwa-helpers/axe-report.js';
25
26 describe('button', function() {
27
28 it('is accessible', function() {
29 const button = document.createElement('button');
30 button.textContent = 'click this'; // Test should fail without this line.
31 return axeReport(button);
32
33 // If you need to run any cleanup code after the test is run, you
34 // can use the `cleanup` object. For example,
35 // document.body.appendChild(button);
36 // return axeReport(el, { cleanup() { el.remove(); } });
37 });
38 });
39*/
40export async function axeReport(dom, config = {}) {
41 const { cleanup, axeConfig } = config;
42 const defaultConfig = {
43 runOnly: {
44 type: 'tag',
45 values: ['wcag2a', 'wcag2aa', 'section508']
46 },
47 // Ignore tests that are passing.
48 resultTypes: ['violations']
49 };
50 const { violations } = await axe.run(dom, axeConfig || defaultConfig);
51 if (cleanup) {
52 await cleanup();
53 }
54 if (!violations.length) {
55 return;
56 }
57 const errorMessage = ['Accessibility Violations', '---'];
58 for (const violation of violations) {
59 errorMessage.push(violation.help);
60 for (const node of violation.nodes) {
61 if (node.failureSummary) {
62 errorMessage.push(node.failureSummary);
63 }
64 errorMessage.push(node.html);
65 }
66 errorMessage.push('---');
67 }
68 throw new Error(errorMessage.join('\n'));
69}
70//# sourceMappingURL=axe-report.js.map
\No newline at end of file