Skip to content

Commit

Permalink
fix: axeError feature (#716)
Browse files Browse the repository at this point in the history
* Update format.ts

* Update index.ts

* Update automatic.ts

* Update groupViolationResultsProcessor.ts
  • Loading branch information
navateja-alagam authored Jul 15, 2024
1 parent a3e1f7b commit 1e56a39
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 13 deletions.
16 changes: 16 additions & 0 deletions packages/format/src/format.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,3 +103,19 @@ export class A11yError extends Error {
.join('\n\n');
}
}

export class AxeError extends Error {
/**
* Throw error with Axe error
*/

constructor(message: string) {
super(message);
this.name = AxeError.name;
this.message = message;
}

static throwAxeError(e: Error): void {
throw new AxeError(`${e.message}`);
}
}
2 changes: 1 addition & 1 deletion packages/format/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@
* For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/BSD-3-Clause
*/

export { A11yError, Options } from './format';
export { A11yError, AxeError, Options } from './format';
export { exceptionListFilter, exceptionListFilterSelectorKeywords } from './filter';
export { A11yResult, A11yResults, appendWcag } from './result';
4 changes: 3 additions & 1 deletion packages/jest/src/automatic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

import { AxeResults, log, useCustomRules } from '@sa11y/common';
import { getViolationsJSDOM } from '@sa11y/assert';
import { A11yError, exceptionListFilterSelectorKeywords } from '@sa11y/format';
import { A11yError, AxeError, exceptionListFilterSelectorKeywords } from '@sa11y/format';
import { isTestUsingFakeTimer } from './matcher';
import { expect } from '@jest/globals';
import { adaptA11yConfig, adaptA11yConfigCustomRules } from './setup';
Expand Down Expand Up @@ -132,6 +132,8 @@ export async function automaticCheck(opts: AutoCheckOpts = defaultAutoCheckOpts)
}
}
}
} catch (e) {
AxeError.throwAxeError(e as Error);
} finally {
if (opts.runDOMMutationObserver) {
mutatedNodes = [];
Expand Down
37 changes: 26 additions & 11 deletions packages/jest/src/groupViolationResultsProcessor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,16 @@
*/
import { AggregatedResult, AssertionResult, TestResult } from '@jest/test-result';
import { log } from '@sa11y/common';
import { A11yError } from '@sa11y/format';
import { A11yError, AxeError } from '@sa11y/format';

type FailureDetail = {
type a11yFailureDetail = {
error?: A11yError;
};

type axeFailureDetail = {
error?: AxeError;
};

interface FailureMatcherDetail {
error?: {
matcherResult?: {
Expand Down Expand Up @@ -171,30 +176,40 @@ For guidance on accessibility related specifics, contact our A11y team: http://s
* Convert any a11y errors from test failures into their own test suite, results
*/
function processA11yErrors(results: AggregatedResult, testSuite: TestResult, testResult: AssertionResult) {
const a11yFailureDetails: FailureDetail[] = [];
const a11yFailureDetails: a11yFailureDetail[] = [];
const axeFailureDetails: axeFailureDetail[] = [];
const a11yFailureMessages: string[] = [];
let a11yErrorsExist = false;
const axeFailureMessages: string[] = [];
let a11yAxeErrorsExist = false;

testResult.failureDetails.forEach((failure) => {
let error = (failure as FailureDetail).error;
let error = (failure as a11yFailureDetail).error;
// If using circus test runner https://github.com/facebook/jest/issues/11405#issuecomment-843549606
// TODO (code cov): Add test data covering the case for circus test runner
/* istanbul ignore next */
if (error === undefined) error = failure as A11yError;
if (error.name === A11yError.name) {
a11yErrorsExist = true;
a11yFailureDetails.push({ ...(failure as FailureDetail) } as FailureDetail);
a11yAxeErrorsExist = true;
a11yFailureDetails.push({ ...(failure as a11yFailureDetail) } as a11yFailureDetail);
processA11yDetailsAndMessages(error, a11yFailureMessages);
}
if (error.name === AxeError.name) {
a11yAxeErrorsExist = true;
axeFailureDetails.push({ ...(failure as axeFailureDetail) } as axeFailureDetail);
axeFailureMessages.push(`
The test has failed to execute the accessibility check.
Please contact our Sa11y team: http://sfdc.co/sa11y-users
`);
}
});
if (!a11yErrorsExist) {
if (!a11yAxeErrorsExist) {
testSuite.numFailingTests -= 1;
results.numFailedTests -= 1;
if (testSuite.numFailingTests === 0) results.numFailedTestSuites -= 1;
}
testResult.failureDetails = [...a11yFailureDetails];
testResult.failureMessages = [...a11yFailureMessages];
testResult.status = a11yFailureDetails.length === 0 ? 'passed' : testResult.status;
testResult.failureDetails = [...a11yFailureDetails, ...axeFailureDetails];
testResult.failureMessages = [...a11yFailureMessages, ...axeFailureMessages];
testResult.status = testResult.failureDetails.length === 0 ? 'passed' : testResult.status;
}

function processA11yMatcherErrors(results: AggregatedResult, testSuite: TestResult, testResult: AssertionResult) {
Expand Down

0 comments on commit 1e56a39

Please sign in to comment.