Last active
October 20, 2022 12:00
-
-
Save straker/5a3310ae0c0a78e2310724c83db6a9e1 to your computer and use it in GitHub Desktop.
Example of running axe-core using Jest and Enzyme
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 { mount, render } from 'enzyme'; | |
import axe from 'axe-core'; | |
import assert from 'assert'; | |
const App = () => <h1>Hello World</h1> | |
describe("axe-core, jest, enzyme", () => { | |
// axe can only run on connected DOM nodes so we need to mount each component | |
// into the DOM tree | |
let fixture = document.createElement('div'); | |
document.body.appendChild(fixture); | |
let component; | |
afterEach(() => { | |
component?.unmount(); | |
}); | |
it('should test enzyme component', async () => { | |
component = mount(<App />, { attachTo: fixture }); | |
const results = await axe.run(fixture); | |
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
const Enzyme = require('enzyme'); | |
const Adapter = require('@wojtekmaj/enzyme-adapter-react-17'); | |
Enzyme.configure({ adapter: new Adapter() }); |
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", | |
"@wojtekmaj/enzyme-adapter-react-17": "^0.6.6", | |
"axe-core": "^4.4.1", | |
"enzyme": "^3.11.0", | |
"jest": "^27.5.1", | |
"react": "^17.0.2", | |
"react-dom": "^17.0.2" | |
}, | |
"jest": { | |
"testEnvironment": "jsdom", | |
"setupFilesAfterEnv": [ | |
"<rootDir>/jest.setup.js" | |
] | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment