Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Save duplicate results #11

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 28 additions & 4 deletions lib/pa11y-ci.js
Original file line number Diff line number Diff line change
@@ -40,7 +40,7 @@ module.exports.defaults = defaultCfg;
// against the passed in URLs. It accepts options in the form
// of an object and returns a Promise
function pa11yCi(urls, options) {
// eslint-disable-next-line no-async-promise-executor
// eslint-disable-next-line no-async-promise-executor, max-statements
return new Promise(async resolve => {
// Create a test browser to assign to tests
let testBrowser;
@@ -90,21 +90,45 @@ function pa11yCi(urls, options) {
results: {}
};

// Map of duplicate result keys (URL - description) to track counts
const duplicateUrls = new Map();

// Common function to save results to encapsulate duplicate URL
// processing logic, which will append the count to the URL so
// it is saved.
function saveResults(url, results, config) {
// If a description is provided, add to the URL as the key
const resultKey = config.description ? `${url} - ${config.description}` : url;
let formattedResultKey;
if (Object.keys(report.results).includes(resultKey)) {
// If results exist for this key, update the duplicate
// count and append.
const currentCount = duplicateUrls.get(resultKey) || 1;
const newCount = currentCount + 1;
duplicateUrls.set(resultKey, newCount);
formattedResultKey = `${resultKey} (${newCount})`;
} else {
formattedResultKey = resultKey;
}
report.results[formattedResultKey] = results;
}

function processResults(results, reportConfig) {
const withinThreshold = reportConfig.threshold ?
results.issues.length <= reportConfig.threshold :
false;
if (results.issues.length && !withinThreshold) {
report.results[results.pageUrl] = results.issues;
saveResults(results.pageUrl, results.issues, reportConfig);
report.errors += results.issues.length;
} else {
report.results[results.pageUrl] = [];
saveResults(results.pageUrl, [], reportConfig);
report.passes += 1;
}
}

// This is the actual test runner, which the queue will
// execute on each of the URLs
// eslint-disable-next-line max-statements
async function testRunner(config) {
let url;
if (typeof config === 'string') {
@@ -129,7 +153,7 @@ function pa11yCi(urls, options) {
processResults(results, config);
} catch (error) {
await cycleReporters(reporters, 'error', error, url, config);
report.results[url] = [error];
saveResults(url, [error], config);
} finally {
if (config.useIncognitoBrowserContext) {
await config.browser.close();
18 changes: 12 additions & 6 deletions test/unit/lib/pa11y-ci.test.js
Original file line number Diff line number Diff line change
@@ -85,7 +85,8 @@ describe('lib/pa11y-ci', () => {
userUrls = [
'foo-url',
'bar-url',
'baz-url'
'baz-url',
'foo-url'
];
userOptions = {
concurrency: 4,
@@ -150,14 +151,14 @@ describe('lib/pa11y-ci', () => {
});

it('Runs the Pa11y test runner on each of the URLs', () => {
assert.callCount(pa11y, 3);
assert.callCount(pa11y, 4);
assert.calledWith(pa11y, 'foo-url');
assert.calledWith(pa11y, 'bar-url');
assert.calledWith(pa11y, 'baz-url');
});

it('logs that the tests have started running', () => {
assert.calledWithMatch(log.info, /3 URLs/i);
assert.calledWithMatch(log.info, /4 URLs/i);
});

it('logs the number of errors for each URL, or if they fail to run', () => {
@@ -167,7 +168,7 @@ describe('lib/pa11y-ci', () => {
});

it('logs the pass/fail ratio', () => {
assert.calledWithMatch(log.error, /1\/3 urls passed/i);
assert.calledWithMatch(log.error, /2\/4 urls passed/i);
});

it('logs the errors for each URL that has some', () => {
@@ -189,7 +190,7 @@ describe('lib/pa11y-ci', () => {
});

it('has a `passes` property set to the total number of URLs that passed the test', () => {
assert.strictEqual(report.passes, 1);
assert.strictEqual(report.passes, 2);
});

it('has a `results` property set to an object where keys are URLs and values are their results', () => {
@@ -207,6 +208,11 @@ describe('lib/pa11y-ci', () => {
assert.strictEqual(report.results['baz-url'][0], pa11yError);
});

it('appends a count to the `results` key for duplicate URLs', () => {
assert.isArray(report.results['foo-url (2)']);
assert.lengthEquals(report.results['foo-url (2)'], 0);
});

});

});
@@ -246,7 +252,7 @@ describe('lib/pa11y-ci', () => {
});

it('logs the pass/fail ratio', () => {
assert.calledWithMatch(log.info, /3\/3 urls passed/i);
assert.calledWithMatch(log.info, /4\/4 urls passed/i);
});

it('never logs any errors', () => {