From 6a9837747985c3c4e77490cd0288642e07975916 Mon Sep 17 00:00:00 2001 From: Peter Sirotnak Date: Fri, 16 Feb 2024 10:09:27 +0100 Subject: [PATCH 1/2] Adds results callback --- README.md | 4 ++++ src/index.ts | 41 +++++++++++++++++++++++------------------ 2 files changed, 27 insertions(+), 18 deletions(-) diff --git a/README.md b/README.md index 4e76507..e96a2b8 100644 --- a/README.md +++ b/README.md @@ -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. diff --git a/src/index.ts b/src/index.ts index 70654db..13506dd 100644 --- a/src/index.ts +++ b/src/index.ts @@ -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 ) => { @@ -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 { + 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((acc, node) => acc.concat(node.target), []) .join(', '); @@ -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`, }); } }); From bfc9c3c44a12c947ac658fb4c068701db70a0856 Mon Sep 17 00:00:00 2001 From: Peter Sirotnak Date: Fri, 16 Feb 2024 10:27:23 +0100 Subject: [PATCH 2/2] Updates package json --- package.json | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/package.json b/package.json index 5683439..69c1195 100644 --- a/package.json +++ b/package.json @@ -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" ], @@ -73,4 +73,4 @@ "*.js": "eslint --cache --fix", "*.{js,md}": "prettier --write" } -} +} \ No newline at end of file