diff --git a/README.md b/README.md index 3061f9a..15af9e6 100644 --- a/README.md +++ b/README.md @@ -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. @@ -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. diff --git a/src/index.js b/src/index.js index 0135c01..2f3a0e2 100644 --- a/src/index.js +++ b/src/index.js @@ -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), [])