Skip to content

Instantly share code, notes, and snippets.

@straker
Created March 7, 2022 17:34
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save straker/f2dc03112bc01b0b3d6f44f6132d0e31 to your computer and use it in GitHub Desktop.
Example of running axe-core using Jest and @testing-library/react
{
"presets": [
[
"@babel/preset-env",
{
"targets": {
"node": "current"
}
}
],
"@babel/preset-react"
]
}
import React from 'react'
import { render, cleanup } from '@testing-library/react'
import axe from 'axe-core';
import assert from 'assert';
const App = () => <h1>Hello World</h1>
describe("axe-core, jest, @testing-library/react", () => {
afterEach(() => {
cleanup();
});
it('should test @testing-library/react component', async () => {
// @testing-library/react automatically appends the container to the DOM
const { container } = render(<App />);
const results = await axe.run(container);
assert.equal(results.violations.length, 0);
});
});
{
"scripts": {
"test": "jest"
},
"devDependencies": {
"@babel/preset-env": "^7.16.11",
"@babel/preset-react": "^7.16.7",
"@testing-library/react": "^12.1.3",
"axe-core": "^4.4.1",
"jest": "^27.5.1",
"react": "^17.0.2",
"react-dom": "^17.0.2"
},
"jest": {
"testEnvironment": "jsdom"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment