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

tests: assert what axe checks matches our a11y audits #12699

Merged
merged 17 commits into from
Apr 25, 2022
Merged
Show file tree
Hide file tree
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
20 changes: 16 additions & 4 deletions lighthouse-core/gather/gatherers/accessibility.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,6 @@ const axeLibSource = require('../../lib/axe.js').source;
const pageFunctions = require('../../lib/page-functions.js');

/**
* This is run in the page, not Lighthouse itself.
* axe.run returns a promise which fulfills with a results object
* containing any violations.
* @return {Promise<LH.Artifacts.Accessibility>}
*/
/* c8 ignore start */
Expand All @@ -38,8 +35,11 @@ async function runA11yChecks() {
'wcag2aa',
],
},
// resultTypes doesn't limit the output of the axeResults object. Instead, if it's defined,
// some expensive element identification is done only for the respective types. https://github.com/dequelabs/axe-core/blob/f62f0cf18f7b69b247b0b6362cf1ae71ffbf3a1b/lib/core/reporters/helpers/process-aggregate.js#L61-L97
resultTypes: ['violations', 'inapplicable'],
rules: {
// Consider http://go/prcpg for expert review of the aXe rules.
'tabindex': {enabled: true},
'accesskeys': {enabled: true},
'heading-order': {enabled: true},
Expand All @@ -60,6 +60,12 @@ async function runA11yChecks() {
// https://github.com/dequelabs/axe-core/issues/2958
'nested-interactive': {enabled: false},
'frame-focusable-content': {enabled: false},
'aria-roledescription': {enabled: false},
'scrollable-region-focusable': {enabled: false},
// TODO(paulirish): create audits and enable these 3.
'input-button-name': {enabled: false},
'role-img-alt': {enabled: false},
'select-name': {enabled: false},
Comment on lines +66 to +68
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

has this list changed since this pr?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

funnily enough, nope!

},
});

Expand All @@ -70,7 +76,8 @@ async function runA11yChecks() {
return {
violations: axeResults.violations.map(createAxeRuleResultArtifact),
incomplete: axeResults.incomplete.map(createAxeRuleResultArtifact),
notApplicable: axeResults.inapplicable.map(result => ({id: result.id})),
notApplicable: axeResults.inapplicable.map(result => ({id: result.id})), // FYI: inapplicable => notApplicable!
passes: axeResults.passes.map(result => ({id: result.id})),
version: axeResults.testEngine.version,
};
}
Expand Down Expand Up @@ -150,6 +157,11 @@ class Accessibility extends FRGatherer {
supportedModes: ['snapshot', 'navigation'],
};

static pageFns = {
runA11yChecks,
createAxeRuleResultArtifact,
};

/**
* @param {LH.Gatherer.FRTransitionalContext} passContext
* @return {Promise<LH.Artifacts.Accessibility>}
Expand Down
44 changes: 43 additions & 1 deletion lighthouse-core/test/gather/gatherers/accessibility-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,10 @@

const AccessibilityGather = require('../../../gather/gatherers/accessibility.js');
const assert = require('assert').strict;
let accessibilityGather;
const {LH_ROOT} = require('../../../../root.js');

describe('Accessibility gatherer', () => {
let accessibilityGather;
// Reset the Gatherer before each test.
beforeEach(() => {
accessibilityGather = new AccessibilityGather();
Expand All @@ -32,3 +33,44 @@ describe('Accessibility gatherer', () => {
err => assert.ok(err.message.includes(error)));
});
});

describe('a11y audits + aXe', () => {
let browser;
const axeLibSource = require('../../../lib/axe.js').source;
const pageFunctions = require('../../../lib/page-functions.js');
const fs = require('fs');

beforeAll(async () => {
browser = await require('puppeteer').launch();
});

afterAll(async () => {
await browser.close();
});

it('only runs the axe rules we have audits defined for', async () => {
const page = await browser.newPage();
page.setContent(`<!doctype html><meta charset="utf8"><title>hi</title>valid.`);
await page.evaluate(axeLibSource);
await page.evaluate(pageFunctions.getNodeDetailsString);
await page.evaluate(AccessibilityGather.pageFns.runA11yChecks.toString());
await page.evaluate(AccessibilityGather.pageFns.createAxeRuleResultArtifact.toString());

// 1. Run axe in the browser.
const a11yArtifact = await page.evaluate(`runA11yChecks()`);
// 2. Get list of the axe rules that ran.
const axeRuleIds = new Set();
for (const key of ['violations', 'incomplete', 'notApplicable', 'passes']) {
if (a11yArtifact[key]) a11yArtifact[key].forEach(result => axeRuleIds.add(result.id));
}

// 3. Get audit list we have implementations for.
// Note: audit ids match their filenames, thx to the getAuditList test in runner-test.js
const filenames = fs.readdirSync(`${LH_ROOT}/lighthouse-core/audits/accessibility/`)
.map(f => f.replace('.js', ''))
.filter(f => f !== 'axe-audit' && f !== 'manual');

// 4. Compare. (Received from aXe, Expected is LH audits)
expect(Array.from(axeRuleIds).sort()).toEqual(filenames.sort());
});
});
1 change: 1 addition & 0 deletions types/artifacts.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,7 @@ declare module Artifacts {
interface Accessibility {
violations: Array<AxeRuleResult>;
notApplicable: Array<Pick<AxeRuleResult, 'id'>>;
passes: Array<Pick<AxeRuleResult, 'id'>>;
incomplete: Array<AxeRuleResult>;
version: string;
}
Expand Down