Skip to content

Commit

Permalink
Integrate axe-core/playwright into storybook/test-runner
Browse files Browse the repository at this point in the history
  • Loading branch information
angelikatyborska committed Aug 19, 2024
1 parent 0b33bd1 commit aff522a
Show file tree
Hide file tree
Showing 3 changed files with 2,693 additions and 56 deletions.
35 changes: 35 additions & 0 deletions .storybook/test-runner.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// TODO: try to make axe-playwright work or uninstall
const { injectAxe, checkA11y, configureAxe } = require('axe-playwright');
const { getStoryContext } = require('@storybook/test-runner');
const { AxeBuilder } = require('@axe-core/playwright');
/*
* See https://storybook.js.org/docs/writing-tests/test-runner#test-hook-api
* to learn more about the test-runner hooks API.
*/
module.exports = {
async postVisit(page, context) {
// Get the entire context of a story, including parameters, args, argTypes, etc.
const storyContext = await getStoryContext(page, context);

// Do not run a11y tests on disabled stories.
if (storyContext.parameters?.a11y?.disable) {
return;
}

const results = await new AxeBuilder({ page }).options({
// those rules fail for every story
rules: {
"region": { "enabled": false },
"landmark-one-main": { "enabled": false },
"page-has-heading-one": { "enabled": false }
}
}).analyze();

if (results.violations.length > 0) {
const storyId = `'${storyContext.title} ${storyContext.name}' (id: ${storyContext.id})`
console.error(`Story ${storyId} did not pass axe-core checks.\n\n`, JSON.stringify(results.violations, null, 2));

throw `Story ${storyId} did not pass axe-core checks. See test error logs for details.`
}
},
};
4 changes: 4 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
"format:docs": "prettier --ignore-path .gitignore --write './**/*.{md,mdx}'",
"format": "yarn format:js && yarn format:scss && yarn format:docs",
"test": "script/test",
"test-storybook": "test-storybook",
"checks": "yarn lint && yarn css:stats && yarn test",
"watch": "yarn build && yarn build --watch",
"clean": "rimraf build/*",
Expand Down Expand Up @@ -49,6 +50,7 @@
},
"homepage": "https://bitcrowd.github.io/bitstyles",
"devDependencies": {
"@axe-core/playwright": "^4.9.1",
"@babel/core": "^7.18.10",
"@storybook/addon-a11y": "^6.5.9",
"@storybook/addon-actions": "^6.5.9",
Expand All @@ -58,10 +60,12 @@
"@storybook/builder-webpack5": "^6.5.9",
"@storybook/html": "^6.5.9",
"@storybook/manager-webpack5": "^6.5.9",
"@storybook/test-runner": "^0.16.0",
"a11y-dialog": "^7.1.0",
"alpinejs": "^2.8.2",
"analyze-css": "^2.1.50",
"autoprefixer": "^10.4.8",
"axe-playwright": "^2.0.1",
"babel-loader": "^8.2.5",
"css-loader": "^6.7.1",
"cssnano": "^5.1.13",
Expand Down
Loading

0 comments on commit aff522a

Please sign in to comment.