@chanzuckerberg/axe-storybook-testing

中文文档
10.0.0发布于 2025/11/26MITES Module近 30 天下载 1.9万

Package Status Tests [!

原始使用文档
文档目录19 节
  1. Table of contents
  2. Code of conduct
  3. Minimum requirements
  4. Installation
  5. Usage
  6. Options
  7. Story parameters
  8. disabledRules
  9. mode
  10. runOptions
  11. context
  12. config
  13. skip
  14. timeout
  15. waitForSelector
  16. TypeScript
  17. Developing
  18. Security
  19. Inspiration

@chanzuckerberg/axe-storybook-testing

Package Status Tests Release

Command line interface for running axe-core accessibility tests on your Storybook stories.

If there are any violations, information about them will be printed, and the command will exit with a non-zero exit code. That way, you can use this as automated accessibility tests on CI.

Table of contents

Code of conduct

This project adheres to the Contributor Covenant code of conduct. By participating, you are expected to uphold this code. Please report unacceptable behavior to [email protected].

Minimum requirements

  • Node 18
  • Storybook 10
    • For Storybook 9, use axe-storybook-testing v9
    • For Storybook 7 or 8, use axe-storybook-testing v8.2.3
    • For Storybook 6, use axe-storybook-testing v6.3.1
  • axe-core 4.0

Installation

# via npm
npm install --save-dev @chanzuckerberg/axe-storybook-testing

# or with Yarn
yarn add --dev @chanzuckerberg/axe-storybook-testing

Usage

To use:

  1. Add a script that creates a storybook build and then executes the axe-storybook command

    // In package.json
    "scripts": {
      "test:axe": "storybook build && axe-storybook"
    },
    
  2. Run the tests by calling the script from the previous step

    npm run test:axe
    
  3. (Optional) Install more browsers. By default Chromium is installed. If you want to also use Firefox and/or Safari, install them with:

    npx playwright install
    

You may also configure your script to run storybook build --test instead, and configure storybook to disable irrelevant addons/features for testing.

Options

The command-line interface has the following options:

OptionDefaultValuesDescription
--browserchromiumchromium, firefox, webkitWhich browser to run the tests
--build-dirstorybook-staticpathStorybook static build directory
--failing-impactallall, minor, moderate, serious, criticalThe lowest impact level that should be considered a failure
--headlesstruebooleanWhether to run headlessly or not
--pattern.*regex patternOnly run tests that match a component name pattern
--portnumberPort to run Storybook on while testing. If missing, an empty port will automatically be selected. Ignored if storybook-url is provided
--reporterspecspec, dot, nyan, tap, landing, list, progress, json, json-stream, min, doc, markdown, xunitHow to display the test run. Can be any built-in Mocha reporter.
--reporter-optionsstringOptions to pass to the mocha reporter. Especially useful with the xunit reporter - e.g. --reporter-options output=./filename.xml
--storybook-addressurlDeprecated! Use --storybook-url instead.
--storybook-urlurlUrl to a running Storybook to test against. Alternative to --build-dir, which will be ignored if this is set.
--timeout2000numberDeprecated! Use the timeout story parameter instead.

For example, to run non-headlessly in Firefox, you would run

# If using npm
npm run storybook:axe -- --headless false --browser firefox

# or, if using Yarn
yarn storybook:axe --headless false --browser firefox

Story parameters

Stories can use parameters to configure how axe-storybook-testing handles them.

You can provide these wherever Storybook accepts parameters (story, component, or global).

disabledRules

Prevent axe-storybook-testing from running specific Axe rules on a story by using the disabledRules parameter.

// SomeComponent.stories.jsx

export const SomeStory = {
  parameters: {
    axe: {
      disabledRules: ['select-name'],
    },
  }.
};

Rules can also be disabled globally by setting this parameter for all stories in .storybook/preview.js.

// .storybook/preview.js

export const parameters = {
  axe: {
    disabledRules: ['select-name'],
  },
};

mode

Set whether errors for a story will fail the test suite or not.

Valid options are:

  • off - the story will be skipped and axe will not run on it. This is the same as setting skip: true.
  • warn - axe errors will be printed, but won't fail the test suite. Stories with this set will show up as pending.
  • error (default) - axe errors will fail the test suite for a story.
// .storybook/preview.js

export const parameters = {
  axe: {
    mode: 'warn',
  },
};

runOptions

Allows use of any of the available axe.run options. See the link for more details. When using runOptions.rules in combination with disabledRules, disabledRules will always take precedent.

export const SomeStory = {
  parameters: {
    axe: {
      runOptions: {
        preload: true,
        selectors: true,
        ...
      }
    }
  }
}

context

Axe context, which is passed to axe.run. Useful for including or excluding elements from the tests.

export const SomeStory = {
  parameters: {
    axe: {
      context: {
        exclude: '.foo',
      },
    }
  }
}

config

Axe configuration, which is passed to axe.configure.

export const SomeStory = {
  parameters: {
    axe: {
      config: {
        checks: [...],
        ...
      }
    }
  }
}

skip

Prevent axe-storybook-testing from running a story by using the skip parameter. This is shorthand for setting mode: 'off'.

// SomeComponent.stories.jsx

export const SomeStory = {
  parameters: {
    axe: {
      skip: true,
    },
  },
};

timeout

Overrides global --timeout for this specific test

// SomeComponent.stories.jsx

export const SomeStory = {
  parameters: {
    axe: {
      timeout: 5000,
    },
  },
};

waitForSelector

Deprecated!

Legacy way of waiting for a selector before running Axe.

Now we recommend using a Storybook play function to do the same thing.

// SomeComponent.stories.jsx

// Old, deprecated way.
export const SomeStory = {
  parameters: {
    axe: {
      waitForSelector: '#some-component-selector',
    },
  },
};

// New, better way using a play function - https://storybook.js.org/docs/react/writing-stories/play-function
SomeStory.play = async () => {
  await screen.findByText('some string');
};

TypeScript

axe-storybook-testing provides TypeScript types for the story parameters listed above. Story parameters can be type checked by augmenting Storybook's Parameter type:

// overrides.d.ts

import type { AxeParams } from '@chanzuckerberg/axe-storybook-testing';

declare module '@storybook/react' {
  // Augment Storybook's definition of Parameters so it contains valid options for axe-storybook-testing
  interface Parameters {
    axe?: AxeParams;
  }
}

Annotate your stories with the StoryObj type, and your parameters will be type-checked!

// SomeComponent.stories.ts

export const SomeStory: StoryObj<Args> = {
  parameters: {
    axe: {
      timeout: 5000,
    },
  },
};

Developing

If you want to work on this project or contribute back to it, see our wiki entry on Development setup.

Security

To report security issues, see ./SECURITY.md.

Inspiration

This project was originally based on @percy/storybook.

版本与发布记录

@chanzuckerberg/axe-storybook-testing 共发布 47 个版本,首次发布于 2020/09/24;过去 12 个月发布了 1 个版本。

版本发布日期距今
10.0.0latest2025/11/2611 个月前
9.0.02025/06/101.4 年前
8.2.32025/05/021.5 年前
8.2.22025/01/271.8 年前
8.2.12024/10/222 年前
8.2.02024/07/252 年前

其他发布通道:alpha → 8.0.1-alpha.0。

关于 @chanzuckerberg/axe-storybook-testing 的常见问题

@chanzuckerberg/axe-storybook-testing 是什么?

[!

如何安装 @chanzuckerberg/axe-storybook-testing?

在项目目录运行 npm install @chanzuckerberg/axe-storybook-testing,或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖:axe-core(^4.0.0)、storybook(^10.0.1)。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖,Yarn 需要手动安装。它还提供命令行工具 axe-storybook,可以用 npx axe-storybook 直接运行。

@chanzuckerberg/axe-storybook-testing 支持 TypeScript 吗?

该包的 package.json 没有声明类型入口。需要类型提示时,可以在 npm 上查找社区维护的 @types/chanzuckerberg__axe-storybook-testing 是否存在。

@chanzuckerberg/axe-storybook-testing 支持 ESM 还是 CommonJS?

以 ES Module 形式发布,推荐使用 import 引入。在 CommonJS 项目中,Node.js 20.19+ / 22.12+ 可以直接 require() 不含顶层 await 的 ES Module,更早的版本需要改用动态 import()。

@chanzuckerberg/axe-storybook-testing 可以商用吗?

@chanzuckerberg/axe-storybook-testing 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@chanzuckerberg/axe-storybook-testing 的下载量有多少?

根据 npm 官方下载统计,@chanzuckerberg/axe-storybook-testing 在 2026/10/10 – 2026/11/08 这 30 天内被下载 19,170 次,日均约 639 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@chanzuckerberg/axe-storybook-testing 还在维护吗?

最新版本 10.0.0 发布于 2025/11/26(11 个月前);至今共发布 47 个版本,其中过去 12 个月发布了 1 个;首次发布于 2020/09/24。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。