Skip to content

Commit aff522a

Browse files
Integrate axe-core/playwright into storybook/test-runner
1 parent 0b33bd1 commit aff522a

File tree

3 files changed

+2693
-56
lines changed

3 files changed

+2693
-56
lines changed

.storybook/test-runner.js

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
// TODO: try to make axe-playwright work or uninstall
2+
const { injectAxe, checkA11y, configureAxe } = require('axe-playwright');
3+
const { getStoryContext } = require('@storybook/test-runner');
4+
const { AxeBuilder } = require('@axe-core/playwright');
5+
/*
6+
* See https://storybook.js.org/docs/writing-tests/test-runner#test-hook-api
7+
* to learn more about the test-runner hooks API.
8+
*/
9+
module.exports = {
10+
async postVisit(page, context) {
11+
// Get the entire context of a story, including parameters, args, argTypes, etc.
12+
const storyContext = await getStoryContext(page, context);
13+
14+
// Do not run a11y tests on disabled stories.
15+
if (storyContext.parameters?.a11y?.disable) {
16+
return;
17+
}
18+
19+
const results = await new AxeBuilder({ page }).options({
20+
// those rules fail for every story
21+
rules: {
22+
"region": { "enabled": false },
23+
"landmark-one-main": { "enabled": false },
24+
"page-has-heading-one": { "enabled": false }
25+
}
26+
}).analyze();
27+
28+
if (results.violations.length > 0) {
29+
const storyId = `'${storyContext.title} ${storyContext.name}' (id: ${storyContext.id})`
30+
console.error(`Story ${storyId} did not pass axe-core checks.\n\n`, JSON.stringify(results.violations, null, 2));
31+
32+
throw `Story ${storyId} did not pass axe-core checks. See test error logs for details.`
33+
}
34+
},
35+
};

package.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
"format:docs": "prettier --ignore-path .gitignore --write './**/*.{md,mdx}'",
1616
"format": "yarn format:js && yarn format:scss && yarn format:docs",
1717
"test": "script/test",
18+
"test-storybook": "test-storybook",
1819
"checks": "yarn lint && yarn css:stats && yarn test",
1920
"watch": "yarn build && yarn build --watch",
2021
"clean": "rimraf build/*",
@@ -49,6 +50,7 @@
4950
},
5051
"homepage": "https://bitcrowd.github.io/bitstyles",
5152
"devDependencies": {
53+
"@axe-core/playwright": "^4.9.1",
5254
"@babel/core": "^7.18.10",
5355
"@storybook/addon-a11y": "^6.5.9",
5456
"@storybook/addon-actions": "^6.5.9",
@@ -58,10 +60,12 @@
5860
"@storybook/builder-webpack5": "^6.5.9",
5961
"@storybook/html": "^6.5.9",
6062
"@storybook/manager-webpack5": "^6.5.9",
63+
"@storybook/test-runner": "^0.16.0",
6164
"a11y-dialog": "^7.1.0",
6265
"alpinejs": "^2.8.2",
6366
"analyze-css": "^2.1.50",
6467
"autoprefixer": "^10.4.8",
68+
"axe-playwright": "^2.0.1",
6569
"babel-loader": "^8.2.5",
6670
"css-loader": "^6.7.1",
6771
"cssnano": "^5.1.13",

0 commit comments

Comments
 (0)