Created
March 7, 2022 17:34
-
-
Save straker/f2dc03112bc01b0b3d6f44f6132d0e31 to your computer and use it in GitHub Desktop.
Example of running axe-core using Jest and @testing-library/react
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
Show hidden characters
{ | |
"presets": [ | |
[ | |
"@babel/preset-env", | |
{ | |
"targets": { | |
"node": "current" | |
} | |
} | |
], | |
"@babel/preset-react" | |
] | |
} |
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
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); | |
}); | |
}); |
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
{ | |
"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