Skip to content

Commit

Permalink
Update axe-playwright config.
Browse files Browse the repository at this point in the history
  • Loading branch information
mkrause committed Dec 24, 2024
1 parent 320d5e2 commit 73e0c69
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 19 deletions.
1 change: 1 addition & 0 deletions .github/workflows/storybook_test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,4 @@ jobs:
- run: npm ci
#- run: npm run build --if-present
- run: npm run test:storybook-ci
continue-on-error: true # Not enforced for now
15 changes: 15 additions & 0 deletions .storybook/preview.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,21 @@ const preview = {
classTarget: 'html',
stylePreview: true,
},

// https://storybook.js.org/docs/8.5/writing-tests/accessibility-testing
// https://github.com/dequelabs/axe-core/blob/develop/doc/rule-descriptions.md
a11y: {
config: {
// Format: { id: <rule-name>, enabled: <boolean>, selector: <css-selector> }
rules: [
// Known accessibility issues (need to fix these)
//{ id: 'color-contrast', selector: '*:not(button[class*=primary])' },
{ id: 'color-contrast', enabled: false },
],
},
// Axe's options parameter
options: {},
},
},
} satisfies Preview;

Expand Down
36 changes: 17 additions & 19 deletions .storybook/test-runner.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { TestRunnerConfig } from '@storybook/test-runner';
import { injectAxe, checkA11y } from 'axe-playwright';
import { type TestRunnerConfig, getStoryContext } from '@storybook/test-runner';
import { injectAxe, configureAxe, checkA11y } from 'axe-playwright';

/*
* See https://storybook.js.org/docs/writing-tests/test-runner#test-hook-api
Expand All @@ -9,24 +9,22 @@ const config: TestRunnerConfig = {
async preVisit(page) {
await injectAxe(page);
},
async postVisit(page) {
await checkA11y(
page,
'#storybook-root',
{
detailedReport: true,
detailedReportOptions: {
html: true,
},
async postVisit(page, context) {
// Get the entire context of a story, including parameters, args, argTypes, etc.
const storyContext = await getStoryContext(page, context);

// Apply story-level a11y rules
await configureAxe(page, {
rules: storyContext.parameters?.a11y?.config?.rules,
});

const element = storyContext.parameters?.a11y?.element ?? '#storybook-root';
await checkA11y(page, element, {
detailedReport: true,
detailedReportOptions: {
html: true,
},
// true,
// 'html',
// {
// outputDirPath: 'results',
// outputDir: 'accessibility',
// reportFileName: 'accessibility-audit.html',
// },
);
});
},
};

Expand Down

0 comments on commit 73e0c69

Please sign in to comment.