DEV Community

Robert Pearce
Robert Pearce

Posted on • Updated on

Catch Low-Hanging Accessibility Fruit with axe-core

Deque Systems, in addition to having an accessibility-testing browser plugin, has an open source package, axe-core, that can help you determine if HTML contains obvious accessibility issues.

That tool can be used on its own in your tests, or you can turn it into a Promise and use it like this!

import axe from 'axe-core'

const isA11y = html =>
  new Promise((resolve, reject) => {, {}, (err, result={}) => {
      const { violations=[] } = result

      if (err) {
      } else if (violations.length > 0) {
      } else {
        // Uncomment to view incomplete/unavailable tests & why

test('bad form', async () => {
  const wrap = document.createElement('div')
  wrap.innerHTML = `
      <div>Enter your name</div>
      <input type="text" />
      <button type="submit">Submit</button>

  expect(await isA11y(wrap)).toEqual(true)
// Failed: Array [
//   Object {
//     "description": "Ensures every form element has a label",
//     "help": "Form elements must have labels",
//     "helpUrl": "",
//     "id": "label",
//     "impact": "critical",
//     "nodes": Array [
//       [Object],
//     ],
//     "tags": Array [
//       "cat.forms",
//       "wcag2a",
//       "wcag332",
//       "wcag131",
//       "section508",
//       "section508.22.n"
//     ],
//   }
//  ]
Enter fullscreen mode Exit fullscreen mode

It can detect all sorts of accessibility issues, so long as the environment in which it's being tested supports the browser features used in axe-core's tests. For example, jsdom, which jest uses as its browser mocking engine, only recently added some support for Range, it seems there are still some aspects missing, and this prevents axe-core from being able to test things like the color contrast accessibility of text color on certain backgrounds.

That said, the sheer number of issues that can be caught with this tool is staggering. If you work with tools like React and combine this with Deque's react-axe tool and eslint-plugin-jsx-a11y, you are sure to catch heaps of low-hanging fruit.

Here is an example in a real OSS project of mine that uses this axe-core technique with @testing-library/react:

What are you waiting for?

This is also posted on

Top comments (0)