1
1
import { run } from 'axe-core' ;
2
+ import type { Result } from 'axe-core' ;
2
3
import { ReactWrapper , mount } from 'enzyme' ;
3
4
import { isValidElement } from 'preact' ;
4
5
import type { VNode } from 'preact' ;
@@ -18,6 +19,14 @@ export type Scenario = {
18
19
* component with a `<div />` in that case.
19
20
*/
20
21
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 ;
21
30
} ;
22
31
23
32
async function testScenario ( elementOrWrapper : VNode | ReactWrapper ) {
@@ -66,7 +75,9 @@ export function checkAccessibility(
66
75
scenarios : Scenario | Scenario [ ] ,
67
76
) : ( ) => Promise < void > {
68
77
return async ( ) => {
69
- for ( const { name = 'default' , content } of asArray ( scenarios ) ) {
78
+ for ( const { name = 'default' , content, shouldIgnoreViolation } of asArray (
79
+ scenarios ,
80
+ ) ) {
70
81
if ( typeof content !== 'function' ) {
71
82
throw new Error (
72
83
`"content" key for accessibility scenario "${ name } " should be a function but is a ${ typeof content } ` ,
@@ -85,10 +96,13 @@ export function checkAccessibility(
85
96
}
86
97
87
98
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 ) {
89
103
console . error ( 'axe-core violation' , JSON . stringify ( violation , null , 2 ) ) ;
90
104
}
91
- if ( violations . length > 0 ) {
105
+ if ( filteredViolations . length > 0 ) {
92
106
throw new Error ( `Scenario "${ name } " has accessibility violations` ) ;
93
107
}
94
108
}
0 commit comments