Skip to content

Commit

Permalink
Merge pull request #1 from peterSirotnak/add-results-callback
Browse files Browse the repository at this point in the history
Adds results callback
  • Loading branch information
peterSirotnak authored Feb 16, 2024
2 parents 73bbfc0 + bfc9c3c commit 1a2f8c6
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 22 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,10 @@ The `retries` key is an integer that specifies how many times to retry the check

Filtering based on impact in combination with the `skipFailures` argument allows you to introduce `cypress-axe` into tests for a legacy application without failing in CI before you have an opportunity to address accessibility issues. Ideally, you would steadily move towards stricter testing as you address issues.

##### resultsCallback (optional)

Allows you to define a callback that receives the results for custom side-effects, such as adding custom output to the terminal.

##### violationCallback (optional)

Allows you to define a callback that receives the violations for custom side-effects, such as adding custom output to the terminal.
Expand Down
8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
{
"name": "cypress-axe",
"name": "cypress-axe-with-results-callback",
"version": "1.2.0",
"license": "MIT",
"description": "Test accessibility with axe-core in Cypress",
"homepage": "https://github.com/component-driven/cypress-axe",
"repository": "component-driven/cypress-axe",
"homepage": "https://github.com/peterSirotnak/cypress-axe",
"repository": "peterSirotnak/cypress-axe",
"files": [
"dist"
],
Expand Down Expand Up @@ -73,4 +73,4 @@
"*.js": "eslint --cache --fix",
"*.{js,md}": "prettier --write"
}
}
}
41 changes: 23 additions & 18 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ function summarizeResults(
const checkA11y = (
context?: axe.ElementContext,
options?: Options,
resultsCallback?: (results: axe.AxeResults) => void,
violationCallback?: (violations: axe.Result[]) => void,
skipFailures = false
) => {
Expand All @@ -78,35 +79,42 @@ const checkA11y = (
if (isEmptyObjectorNull(options)) {
options = undefined;
}
if (isEmptyObjectorNull(resultsCallback)) {
violationCallback = undefined;
}
if (isEmptyObjectorNull(violationCallback)) {
violationCallback = undefined;
}
const { includedImpacts, interval, retries, ...axeOptions } =
options || {};
let remainingRetries = retries || 0;
function runAxeCheck(): Promise<axe.Result[]> {
function runAxeCheck(): Promise<{ results: axe.AxeResults, violationResults: axe.Result[] }> {
return win.axe
.run(context || win.document, axeOptions)
.then(({ violations }) => {
const results = summarizeResults(includedImpacts, violations);
if (results.length > 0 && remainingRetries > 0) {
.then((results) => {
const violationResults = summarizeResults(includedImpacts, results.violations);
if (violationResults.length > 0 && remainingRetries > 0) {
remainingRetries--;
return new Promise((resolve) => {
setTimeout(resolve, interval || 1000);
}).then(runAxeCheck);
} else {
return results;
return { results, violationResults };
}
});
}
return runAxeCheck();
})
.then((violations) => {
if (violations.length) {
.then(({ results, violationResults }) => {
if (resultsCallback) {
resultsCallback(results)
}

if (violationResults.length) {
if (violationCallback) {
violationCallback(violations);
violationCallback(violationResults);
}
violations.forEach((v) => {
violationResults.forEach((v) => {
const selectors = v.nodes
.reduce<string[]>((acc, node) => acc.concat(node.target), [])
.join(', ');
Expand All @@ -115,30 +123,27 @@ const checkA11y = (
$el: Cypress.$(selectors),
name: 'a11y error!',
consoleProps: () => v,
message: `${v.id} on ${v.nodes.length} Node${
v.nodes.length === 1 ? '' : 's'
}`,
message: `${v.id} on ${v.nodes.length} Node${v.nodes.length === 1 ? '' : 's'
}`,
});
});
}

return cy.wrap(violations, { log: false });
return cy.wrap(violationResults, { log: false });
})
.then((violations) => {
if (!skipFailures) {
assert.equal(
violations.length,
0,
`${violations.length} accessibility violation${
violations.length === 1 ? '' : 's'
`${violations.length} accessibility violation${violations.length === 1 ? '' : 's'
} ${violations.length === 1 ? 'was' : 'were'} detected`
);
} else if (violations.length) {
Cypress.log({
name: 'a11y violation summary',
message: `${violations.length} accessibility violation${
violations.length === 1 ? '' : 's'
} ${violations.length === 1 ? 'was' : 'were'} detected`,
message: `${violations.length} accessibility violation${violations.length === 1 ? '' : 's'
} ${violations.length === 1 ? 'was' : 'were'} detected`,
});
}
});
Expand Down

0 comments on commit 1a2f8c6

Please sign in to comment.