Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue 67 - Add callback to return full axe-core results object #68

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,10 @@ Allows you to define a callback that receives the violations for custom side-eff

**NOTE:** _This respects the `includedImpacts` filter and will only execute with violations that are included._

##### resultsCallback (optional)

Allows you to define a callback that receives the full axe-core results object.

###### skipFailures (optional, defaults to false)

Disables assertions based on violations and only logs violations to the console output. This enabled you to see violations while allowing your tests to pass. This should be used as a temporary measure while you address accessibility violations.
Expand Down Expand Up @@ -151,6 +155,10 @@ The violation callback parameter accepts a function and allows you to add custom

This example adds custom logging to the terminal running Cypress, using `cy.task` and the `violationCallback` argument for `cy.checkA11y`

#### Using the resultsCallback argument

The results callback parameter accepts a function and allows you to add custom behavior after receiving the full axe-core results object.

##### In Cypress plugins file

This registers a `log` task as seen in the [Cypress docs for cy.task](https://docs.cypress.io/api/commands/task.html#Usage) as well as a `table` task for sending tabular data to the terminal.
Expand Down
21 changes: 13 additions & 8 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,29 +18,34 @@ const checkA11y = (
context,
options,
violationCallback,
skipFailures = false
resultsCallback,
skipFailures = false,
) => {
cy.window({ log: false })
.then(win => {
if (isEmptyObjectorNull(context)) context = undefined
if (isEmptyObjectorNull(options)) options = undefined
if (isEmptyObjectorNull(violationCallback)) violationCallback = undefined
if (isEmptyObjectorNull(resultsCallback)) resultsCallback = undefined
const { includedImpacts, ...axeOptions } = options || {}
return win.axe
.run(context || win.document, axeOptions)
.then(({ violations }) => {
return includedImpacts &&
Array.isArray(includedImpacts) &&
Boolean(includedImpacts.length)
? violations.filter(v => includedImpacts.includes(v.impact))
: violations
.then((results) => {
if (includedImpacts && Array.isArray(includedImpacts) && Boolean(includedImpacts.length)) {
results.violations.filter(v => includedImpacts.includes(v.impact))
}
return results
})
})
.then(violations => {
.then(results => {
const violations = results.violations
if (violations.length) {
if (violationCallback) {
violationCallback(violations)
}
if (resultsCallback) {
resultsCallback(results)
}
cy.wrap(violations, { log: false }).each(v => {
const selectors = v.nodes
.reduce((acc, node) => acc.concat(node.target), [])
Expand Down