Skip to content

Commit e0aeaf2

Browse files
jethrolarsonclaude
authored andcommitted
feat(matcher): add per-test filtering via testsFilter and SA11Y_AUTO_FILTER_TESTS
Implements the long-standing TODO for per-test exclusion from automatic accessibility checks. Adds: - `testsFilter` field to `AutoCheckOpts` — array of test name substrings - `skipTestByName()` exported utility, mirroring `skipTest()` for files - `SA11Y_AUTO_FILTER_TESTS` env var for pipeline-level test name exclusions - `updateAutoCheckOpts` reads the env var and merges into `testsFilter` Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
1 parent a2533f2 commit e0aeaf2

4 files changed

Lines changed: 60 additions & 4 deletions

File tree

packages/matcher/__tests__/automaticMatcher.test.ts

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
* For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/BSD-3-Clause
66
*/
77

8-
import { runAutomaticCheck, skipTest, registerCustomSa11yRules } from '../src';
8+
import { runAutomaticCheck, skipTest, skipTestByName, registerCustomSa11yRules } from '../src';
99

1010
import {
1111
beforeEachSetup,
@@ -106,6 +106,39 @@ describe('automatic checks call', () => {
106106
}
107107
);
108108

109+
110+
test.each([
111+
['my test name', undefined, false],
112+
['my test name', [], false],
113+
['my test name', ['my test'], true],
114+
['my test name', ['MY TEST'], true],
115+
['my test name', ['my test', 'other'], true],
116+
['my test name', ['other'], false],
117+
['my test name', ['my test name extra'], false],
118+
])(
119+
'should filter test by name as expected with args # %#',
120+
(testName: string, testsFilter: string[] | undefined, expectedResult: boolean) => {
121+
expect(skipTestByName(testName, testsFilter)).toBe(expectedResult);
122+
}
123+
);
124+
125+
it('should skip auto checks when test name is excluded using testsFilter', async () => {
126+
document.body.innerHTML = domWithA11yIssues;
127+
await expect(
128+
runAutomaticCheck(
129+
{ testsFilter: ['non-matching-name', testName] },
130+
{ renderedDOMDumpDirPath: '' },
131+
testPath,
132+
testName
133+
)
134+
).resolves.toBeUndefined();
135+
});
136+
137+
it('should run auto checks when test name is not excluded using testsFilter', async () => {
138+
document.body.innerHTML = domWithA11yIssues;
139+
await expect(runAutomaticCheck({ testsFilter: ['non-matching-name'] }, {}, testPath, testName)).rejects.toThrow();
140+
});
141+
109142
it('should skip auto checks when file is excluded using filter', async () => {
110143
document.body.innerHTML = domWithA11yIssues;
111144
await expect(
@@ -199,6 +232,7 @@ describe('automatic.ts exports', () => {
199232
cleanupAfterEach: true,
200233
consolidateResults: true,
201234
filesFilter: [],
235+
testsFilter: [],
202236
runDOMMutationObserver: false,
203237
enableIncompleteResults: false,
204238
});

packages/matcher/src/automatic.ts

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,10 @@ export type AutoCheckOpts = {
2121
runAfterEach?: boolean;
2222
cleanupAfterEach?: boolean;
2323
consolidateResults?: boolean;
24-
// TODO (feat): add support for optional exclusion of selected tests
25-
// excludeTests?: string[];
26-
// List of test file paths (as regex) to filter for automatic checks
24+
// List of test file path substrings to filter for automatic checks
2725
filesFilter?: string[];
26+
// List of test name substrings to filter for automatic checks
27+
testsFilter?: string[];
2828
runDOMMutationObserver?: boolean;
2929
enableIncompleteResults?: boolean;
3030
};
@@ -45,6 +45,7 @@ export const defaultAutoCheckOpts: AutoCheckOpts = {
4545
cleanupAfterEach: true,
4646
consolidateResults: true,
4747
filesFilter: [],
48+
testsFilter: [],
4849
runDOMMutationObserver: false,
4950
enableIncompleteResults: false,
5051
};
@@ -75,6 +76,20 @@ export function skipTest(testPath: string | undefined, filesFilter?: string[]):
7576
return skip;
7677
}
7778

79+
/**
80+
* Check if current test needs to be skipped based on test name filter
81+
*/
82+
export function skipTestByName(testName: string | undefined, testsFilter?: string[]): boolean {
83+
if (!testName || !testsFilter || testsFilter.length === 0) return false;
84+
const skip = testsFilter.some((filter) => testName.toLowerCase().includes(filter.toLowerCase()));
85+
if (skip) {
86+
log(
87+
`Skipping automatic accessibility check for test "${testName}" as it matches given tests filter: ${testsFilter.toString()}`
88+
);
89+
}
90+
return skip;
91+
}
92+
7893
/**
7994
* Run accessibility check on each element node in the body using {@link toBeAccessible}
8095
* @param opts - Options for automatic checks {@link AutoCheckOpts}
@@ -87,6 +102,7 @@ export async function runAutomaticCheck(
87102
isFakeTimerUsed: () => boolean = () => false
88103
): Promise<void> {
89104
if (skipTest(testPath, opts.filesFilter)) return;
105+
if (skipTestByName(testName, opts.testsFilter)) return;
90106

91107
// Skip automatic check if test is using fake timer as it would result in timeout
92108
if (isFakeTimerUsed()) {

packages/matcher/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ export {
1111
mutationObserverCallback,
1212
observerOptions,
1313
skipTest,
14+
skipTestByName,
1415
runAutomaticCheck,
1516
AutoCheckOpts,
1617
RenderedDOMSaveOpts,

packages/matcher/src/setup.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,11 @@ export function updateAutoCheckOpts(autoCheckOpts: AutoCheckOpts): void {
7979
'ui-help-components/modules/forceHelp/linkToKnownIssue/__tests__/linkToKnownIssue.spec.js',
8080
]);
8181

82+
if (process.env.SA11Y_AUTO_FILTER_TESTS?.trim().length) {
83+
autoCheckOpts.testsFilter = (autoCheckOpts.testsFilter ?? []).concat(
84+
process.env.SA11Y_AUTO_FILTER_TESTS.split(',')
85+
);
86+
}
8287
autoCheckOpts.runDOMMutationObserver ||= !!process.env.SA11Y_ENABLE_DOM_MUTATION_OBSERVER;
8388
autoCheckOpts.enableIncompleteResults ||= !!process.env.SA11Y_ENABLE_INCOMPLETE_RESULTS;
8489
}

0 commit comments

Comments
 (0)