Accessibility 测试
Automate a11y 检查使用 axe, 测试 keyboard flow, and 验证 ARIA 语义.
你将学到
- 为什么 accessibility 测试是 首先-类测试 concern
- 运行 axe automated 检查使用 jest-axe and @vitest/浏览器
- 测试 keyboard 导航 and focus management
- 验证 ARIA roles, labels, and 颜色 contrast
概念讲解
Accessibility Is Testing
An inaccessible UI is a broken UI — for screen reader users, keyboard users, and anyone on a slow or small device. Accessibility (a11y) testing automates the checks that catch the most common violations: missing labels, insufficient contrast, duplicate ids, invalid ARIA. Like any other bug, an a11y bug is cheapest to fix when caught immediately in development.
Automated Checks with axe
axe-core is a rules engine that scans the DOM for a11y violations. jest-axe and @axe-core/playwright wrap it for tests. After rendering a component, run expect(await axe(container)).toHaveNoViolations() and the test fails if axe finds any issue. Automated checks catch roughly 30–40% of WCAG issues — they are necessary but not sufficient, so combine them with manual and keyboard tests.
Keyboard Testing
Many users navigate by keyboard alone. Test that every interactive element is reachable with Tab, that focus order is logical, that Enter and Space activate buttons, and that modals trap focus and return it on close. Use Testing Library's userEvent.tab() and userEvent.keyboard to drive these flows. A component that works only with the mouse is broken for keyboard users.
ARIA and Contrast
Assert that interactive elements have accessible names (getByRole enforces this), that images have alt text (or are marked decorative), and that form fields have associated labels. Check color contrast with axe or a tool like @axe-核心/playwright's contrast rule. These checks are fast to automate and catch the violations that most often lead to real-world exclusion.
In Practice
Integrate axe into every component test so a11y violations fail the build immediately, not in a separate audit weeks later. Run a broader axe scan against rendered pages in E2E tests to catch issues that appear only when components compose. Beyond automation, periodically navigate your app with a screen reader (VoiceOver, NVDA) and with the keyboard alone — automation cannot detect confusing reading order, unlabeled icon buttons that a sighted user infers, or focus traps that break flow. Treat accessibility bugs with the same urgency as functional bugs: file them, fix them, and add a regression test. Building accessible components from the start (semantic HTML, labeled inputs, sufficient contrast) is far cheaper than retrofitting, and testing keeps them accessible as the UI evolves.
示例代码
import { describe, it, expect } from "vitest";
import { render } from "@testing-library/react";
import { axe } from "vitest-axe";
import { LoginForm } from "./LoginForm";
describe("LoginForm accessibility", () => {
it("has no a11y violations", async () => {
const { container } = render(<LoginForm />);
const results = await axe(container);
expect(results.violations).toEqual([]);
});
it("submits when pressing Enter in the password field", async () => {
const user = userEvent.setup();
render(<LoginForm />);
await user.type(screen.getByLabelText(/password/i), "secret{Enter}");
expect(screen.getByText("Welcome")).toBeInTheDocument();
});
});
axe scans rendered DOM for violations 如 missing labels and 低 contrast. keyboard 测试验证真实用户能 提交不使用 mouse.
动手试一试
常见错误
错误写法
Treating axe as 完整 a11y audit
正确做法
axe catches 仅 automated-checkable issues. 总是添加 keyboard 测试 and screen-reader spot 检查; 许多 WCAG failures 是不 automatable.
错误写法
添加 aria-label 到元素那个 already 有可见文本
正确做法
Prefer 可见 labels. Redundant aria-labels 能 override accessible name and confuse screen readers. Use getByRole 到确认 name.
查阅不认识的术语
新手友好的术语表用大白话解释编程术语——变量、函数、DOM、Promise 等等。
动手做项目
通过引导式项目应用所学,含起始代码与解决方案。
解读错误信息
用大白话解释常见错误——什么意思、为什么发生、如何修复。