Skip to content

Commit 368a73a

Browse files
committed
retry logic to WCAG
1 parent 9847a35 commit 368a73a

1 file changed

Lines changed: 44 additions & 22 deletions

File tree

.storybook/wcag/test-runner.ts

Lines changed: 44 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,49 @@ import { Page } from 'playwright-core';
33
import { runOnly } from '../wcagConfig';
44
import { TestContext, TestRunnerConfig } from '@storybook/test-runner';
55

6+
const AXE_ALREADY_RUNNING_ERROR = 'Axe is already running';
7+
const MAX_AXE_RETRIES = 3;
8+
const AXE_RETRY_DELAY_MS = 250;
9+
10+
const sleep = (ms: number) => new Promise((resolve) => setTimeout(resolve, ms));
11+
12+
// Retry if errors from Axe already running to decrease flakiness
13+
const runA11yCheck = async (page: Page, context: TestContext) => {
14+
for (let attempt = 1; attempt <= MAX_AXE_RETRIES; attempt++) {
15+
try {
16+
await checkA11y(
17+
page,
18+
{
19+
exclude: [
20+
'#root .mapboxgl-canvas-container',
21+
'.mapboxgl-marker',
22+
'.mapboxgl-popup-close-button'
23+
],
24+
},
25+
{
26+
axeOptions: {
27+
runOnly,
28+
rules: {
29+
'color-contrast': { enabled: context.name !== 'Loading' },
30+
},
31+
},
32+
detailedReport: true,
33+
detailedReportOptions: {
34+
html: true,
35+
},
36+
}
37+
);
38+
return;
39+
} catch (error) {
40+
const message = error instanceof Error ? error.message : String(error);
41+
if (!message.includes(AXE_ALREADY_RUNNING_ERROR) || attempt === MAX_AXE_RETRIES) {
42+
throw error;
43+
}
44+
await sleep(AXE_RETRY_DELAY_MS * attempt);
45+
}
46+
}
47+
};
48+
649
/**
750
* See https://storybook.js.org/docs/react/writing-tests/test-runner#test-hook-api-experimental
851
* to learn more about the test-runner hooks API.
@@ -12,28 +55,7 @@ const renderFunctions: TestRunnerConfig = {
1255
await injectAxe(page);
1356
},
1457
async postVisit(page: Page, context: TestContext) {
15-
await checkA11y(
16-
page,
17-
{
18-
exclude: [
19-
'#root .mapboxgl-canvas-container',
20-
'.mapboxgl-marker',
21-
'.mapboxgl-popup-close-button'
22-
],
23-
},
24-
{
25-
axeOptions: {
26-
runOnly,
27-
rules: {
28-
'color-contrast': { enabled: context.name !== 'Loading' },
29-
},
30-
},
31-
detailedReport: true,
32-
detailedReportOptions: {
33-
html: true,
34-
},
35-
}
36-
);
58+
await runA11yCheck(page, context);
3759
},
3860
};
3961

0 commit comments

Comments
 (0)