@sa11y/jest
Version:
Accessibility testing matcher for Jest
89 lines • 3.99 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.resultsProcessorManualChecks = exports.resultsProcessor = void 0;
const common_1 = require("@sa11y/common");
const format_1 = require("@sa11y/format");
const matcher_1 = require("@sa11y/matcher");
/**
* Convert any a11y errors from test failures into their own test suite, results
*/
function processA11yErrors(results, testSuite, testResult) {
const a11yFailureDetails = [];
const a11yFailureMessages = [];
let a11yErrorsExist = false;
testResult.failureDetails.forEach((failure) => {
let error = failure.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;
if (error.name === format_1.A11yError.name) {
a11yErrorsExist = true;
a11yFailureDetails.push({ ...failure });
(0, matcher_1.processA11yDetailsAndMessages)(error, a11yFailureMessages);
}
});
if (!a11yErrorsExist) {
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;
}
function processA11yMatcherErrors(results, testSuite, testResult) {
const a11yFailureMessages = [];
testResult.failureDetails.forEach((failure) => {
const error = failure?.error?.matcherResult?.a11yError;
if (error !== undefined) {
(0, matcher_1.processA11yDetailsAndMessages)(error, a11yFailureMessages);
testResult.failureMessages = [...a11yFailureMessages];
}
});
}
/**
* Custom results processor for a11y results grouping the violations. Only affects JSON results file output.
* To be used with jest cli options --json --outputFile
* e.g. jest --json --outputFile jestResults.json --testResultsProcessor `node_modules/@sa11y/jest/dist/groupViolationResultsProcessor.js`
* Ref: https://jestjs.io/docs/configuration#testresultsprocessor-string
* - Mapping of AggregatedResult to JSON format to https://github.com/facebook/jest/blob/master/packages/jest-test-result/src/formatTestResults.ts
*/
function resultsProcessor(results) {
(0, common_1.log)(`Processing ${results.numTotalTests} tests ..`);
results.testResults // suite results
.filter((testSuite) => testSuite.numFailingTests > 0)
.forEach((testSuite) => {
testSuite.testResults // individual test results
.filter((testResult) => testResult.status === 'failed')
.forEach((testResult) => {
processA11yErrors(results, testSuite, testResult);
});
});
return results;
}
exports.resultsProcessor = resultsProcessor;
function resultsProcessorManualChecks(results) {
(0, common_1.log)(`Processing ${results.numTotalTests} tests ..`);
results.testResults // suite results
.filter((testSuite) => testSuite.numFailingTests > 0)
.forEach((testSuite) => {
testSuite.testResults // individual test results
.filter((testResult) => testResult.status === 'failed')
.forEach((testResult) => {
processA11yMatcherErrors(results, testSuite, testResult);
});
});
return results;
}
exports.resultsProcessorManualChecks = resultsProcessorManualChecks;
// The processor must be a node module that exports a function
// Explicitly typing the exports for Node.js compatibility
const exportedFunctions = {
resultsProcessor,
resultsProcessorManualChecks,
};
module.exports = exportedFunctions;
//# sourceMappingURL=groupViolationResultsProcessor.js.map