Skip to content

Instantly share code, notes, and snippets.

@craigcarlyle
Last active July 16, 2021 18:56
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save craigcarlyle/46274f9c93c369495d19868462898931 to your computer and use it in GitHub Desktop.
Save craigcarlyle/46274f9c93c369495d19868462898931 to your computer and use it in GitHub Desktop.
Cypress Accessibility Linter
function a11yLinter(tagName: string) {
return cy.window().then((win: any) => {
return new Cypress.Promise(function(resolve: any, reject: any) {
win.axe.run(tagName, (err: any, results: any) => {
if (err) {
reject(err);
}
resolve(results.violations);
});
});
});
}
function lintComponent(tagName: string) {
return a11yLinter(tagName).then((violations: any) => {
if (violations.length > 0) {
console.table(violations);
assert.equal(violations.length, 0, violations.length + ` a11y violation(s).
Please use the aXe Chrome Extension to debug the failures.`);
}
});
}
Cypress.Commands.add("lintComponent", (tagName: string) => {
lintComponent(tagName);
});
describe("Component: Foo", () => {
describe("a11y", () => {
describe("aXe Engine", () => {
it("passes the automated tests", () => {
cy.lintComponent("#axe-test");
});
});
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment