Skip to content

Commit d240c17

Browse files
committed
Add shouldIgnoreViolation optional prop to Scenario for a11y tests
1 parent be08955 commit d240c17

File tree

1 file changed

+17
-3
lines changed

1 file changed

+17
-3
lines changed

src/accessibility.ts

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { run } from 'axe-core';
2+
import type { Result } from 'axe-core';
23
import { ReactWrapper, mount } from 'enzyme';
34
import { isValidElement } from 'preact';
45
import type { VNode } from 'preact';
@@ -18,6 +19,14 @@ export type Scenario = {
1819
* component with a `<div />` in that case.
1920
*/
2021
content: () => VNode | ReactWrapper;
22+
23+
/**
24+
* This callback will be applied to all found accessibility violations,
25+
* allowing you to ignore those that are not relevant for your test.
26+
* A use case for this is any combination that axe-core considers invalid
27+
* because certain screen readers won't play well, but their use is marginal.
28+
*/
29+
shouldIgnoreViolation?: (violation: Result) => boolean;
2130
};
2231

2332
async function testScenario(elementOrWrapper: VNode | ReactWrapper) {
@@ -66,7 +75,9 @@ export function checkAccessibility(
6675
scenarios: Scenario | Scenario[],
6776
): () => Promise<void> {
6877
return async () => {
69-
for (const { name = 'default', content } of asArray(scenarios)) {
78+
for (const { name = 'default', content, shouldIgnoreViolation } of asArray(
79+
scenarios,
80+
)) {
7081
if (typeof content !== 'function') {
7182
throw new Error(
7283
`"content" key for accessibility scenario "${name}" should be a function but is a ${typeof content}`,
@@ -85,10 +96,13 @@ export function checkAccessibility(
8596
}
8697

8798
const violations = await testScenario(elementOrWrapper);
88-
for (const violation of violations) {
99+
const filteredViolations = shouldIgnoreViolation
100+
? violations.filter(v => !shouldIgnoreViolation(v))
101+
: violations;
102+
for (const violation of filteredViolations) {
89103
console.error('axe-core violation', JSON.stringify(violation, null, 2));
90104
}
91-
if (violations.length > 0) {
105+
if (filteredViolations.length > 0) {
92106
throw new Error(`Scenario "${name}" has accessibility violations`);
93107
}
94108
}

0 commit comments

Comments
 (0)