Last active
July 16, 2021 18:56
-
-
Save craigcarlyle/46274f9c93c369495d19868462898931 to your computer and use it in GitHub Desktop.
Cypress Accessibility Linter
This file contains hidden or 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
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); | |
}); |
This file contains hidden or 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
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