Skip to content

Instantly share code, notes, and snippets.

@straker
Last active October 20, 2022 12:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save straker/5a3310ae0c0a78e2310724c83db6a9e1 to your computer and use it in GitHub Desktop.
Save straker/5a3310ae0c0a78e2310724c83db6a9e1 to your computer and use it in GitHub Desktop.
Example of running axe-core using Jest and Enzyme
{
"presets": [
[
"@babel/preset-env",
{
"targets": {
"node": "current"
}
}
],
"@babel/preset-react"
]
}
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);
});
});
const Enzyme = require('enzyme');
const Adapter = require('@wojtekmaj/enzyme-adapter-react-17');
Enzyme.configure({ adapter: new Adapter() });
{
"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