Repository URL to install this package:
|
Version:
0.0.9 ▾
|
const fs = require('fs');
const pa11y = require('pa11y');
const htmlReporter = require('pa11y-reporter-html');
const { pa11y: baseConfig, reportsPath } = require('../config').get();
/**
* Runs an a11y test using pa11y.
* @param {A11yTestDefinition} testDefinition The test to run
* @return {*} The raw test results
*/
async function test(testDefinition) {
const result = await pa11y(testDefinition.url, Object.assign({
screenCapture: reportsPath ? `${reportsPath}/capture-${testDefinition.id}.png` : undefined,
}, baseConfig, testDefinition.config));
if (reportsPath) {
// Generate file report
await reportToFile(result, testDefinition.id);
}
return result;
}
/**
* Generates the HTML reports from the pa11y result.
* @param {Object} pa11yResult The result generated by pa11y
* @param {string} name The name of the result file
*/
async function reportToFile(pa11yResult, name) {
const htmlResult = await htmlReporter.results(pa11yResult);
const reportsFile = fs.openSync(reportsPath + `/report-${name}.html`, 'w');
fs.writeSync(reportsFile, htmlResult);
fs.closeSync(reportsFile);
}
/**
* Generates the A11yTestResult from the pa11y result.
* @param {Object} pa11yResult The result generated by pa11y
* @returns {A11yTestResult}
*/
function resultCount(pa11yResult) {
return {
errors: pa11yResult.issues.filter(issue => issue.type === 'error').length,
warnings: pa11yResult.issues.filter(issue => issue.type === 'warning').length,
notices: pa11yResult.issues.filter(issue => issue.type === 'notice').length,
};
}
module.exports = {
test,
resultCount
};