zl程序教程

您现在的位置是:首页 >  前端

当前栏目

[React ARIA Testing] Test Accessibility of Rendered React Components with jest-axe

React of with test Testing Components jest ARIA
2023-09-14 09:00:47 时间

While not all of accessibility testing of a web application can be automated, much of it can be and quite easily using axe-core and jest-axe. Let’s see how to integrate this with React Testing Library to help catch common accessibility issues.

 

import React from 'react'
import 'jest-axe/extend-expect'
import { axe } from 'jest-axe'
import { render } from '@testing-library/react'

function InaccessibleForm() {
  return (
    <form>
      <input placeholder="email" />
    </form>
  )
}

function AccessibleForm() {
  return (
    <form>
      <label htmlFor="username">Username</label>
      <input id="username" placeholder="username" />
    </form>
  )
}

test('inaccessible forms fail axe', async () => {
  const { container } = render(<InaccessibleForm />)
  expect(await axe(container)).not.toHaveNoViolations()
})

test('accessible forms pass axe', async () => {
  const { container } = render(<AccessibleForm />)
  expect(await axe(container)).toHaveNoViolations()
})