← Back to Angular, page 10

angular-0183 · Medium mid

How do you test the accessibility of an Angular app? What can automated tools such as axe find, and what must you check by hand?

Use several layers: template lint rules while coding, axe-core checks in component and e2e tests, Lighthouse in CI, and manual checks with keyboard only and a screen reader. Automated tools find missing labels, alt text, bad ARIA and some contrast problems, but cannot judge whether the experience makes sense.

Full answer

Accessibility (a11y) means people who use keyboards, screen readers, zoom or voice control can use the app. WCAG is the standard, and level AA is the usual target. Testing needs several layers, because no single tool finds everything.

  1. Lint while you write. angular-eslint has template accessibility rules, such as alt-text, label-has-associated-control, click-events-have-key-events, interactive-supports-focus and valid-aria. They catch mistakes in the editor and in pull requests.
  2. Automated checks in tests. axe-core scans a DOM and returns violations with rule id, impact and the failing element. Run it in unit or component tests on the rendered fixture (axe.run(fixture.nativeElement), or jest-axe and vitest-axe wrappers), and in e2e tests on whole pages (@axe-core/playwright). Fail the test when there are violations, and keep a small, documented list of exceptions.
  3. Lighthouse or Pa11y in CI for page level reports and trends.
  4. Manual checks, which matter most:
    • Keyboard only: can you reach and use every control with Tab, Shift+Tab, Enter, Space and arrow keys? Is the focus order logical, and is the focus always visible? After opening a dialog, does focus move into it and return afterwards?
    • Screen reader: test with NVDA or VoiceOver. Are names, roles, states and error messages announced? Do route changes announce the new page (focus the heading or use LiveAnnouncer)?
    • Zoom to 200 percent and a narrow window: nothing is cut off. Check colour contrast and do not use colour as the only signal.
    • Reduced motion and text spacing settings.

What automated tests find well: missing form labels, missing alt text, duplicate ids, invalid ARIA attributes, empty buttons and links, document language, and many contrast failures. What they cannot judge: whether a label is meaningful, whether the reading order makes sense, whether a custom widget really works with a keyboard, and whether the page is understandable. Treat a green axe run as "no known basic faults", not as "accessible".

Angular helps with @angular/cdk/a11y (FocusTrap, FocusMonitor, LiveAnnouncer, ListKeyManager) and Angular Material, which follows ARIA patterns. Still test your own composition.

Process: define a checklist in the definition of done, test new components with axe in the same pull request, and run a manual audit before large releases.

typescript
import { Component } from '@angular/core';
import * as axe from 'axe-core';

@Component({
  selector: 'app-bad-form',
  template: `
    <img src="data:image/gif;base64,R0lGODlhAQABAAAAACw=" />
    <input type="text" />
    <button type="button"></button>
  `,
})
export class BadFormComponent {}

@Component({
  selector: 'app-good-form',
  template: `
    <img src="data:image/gif;base64,R0lGODlhAQABAAAAACw=" alt="School logo" />
    <label for="name">Name</label>
    <input id="name" type="text" />
    <button type="button" aria-label="Save"></button>
  `,
})
export class GoodFormComponent {}

export async function violationsFor(element: HTMLElement): Promise<string[]> {
  const results = await axe.run(element, { runOnly: ['image-alt', 'label', 'button-name'] });
  return results.violations.map((v) => v.id).sort();
}

Angular 20.3 with axe-core. A Karma test runs axe on a form with missing labels and a missing image alt, and on a fixed version. The violations found were button-name, image-alt and label for the bad form, and none for the fixed one. Screen reader and keyboard checks were not run.

Key points

  • Layers: lint rules, axe in tests, Lighthouse in CI, and manual keyboard and screen reader checks.
  • Automated tools find mechanical problems (labels, alt text, ARIA validity, some contrast). They cannot judge meaning or flow.
  • Check focus order, visible focus, dialog focus handling, and route change announcements by hand.
  • CDK a11y and Material help, but your own composition still needs testing.

Common mistakes

  • Treating a clean axe report as proof that the app is accessible.
  • Testing only with a mouse.
  • Adding ARIA attributes that contradict native semantics instead of using the right element.

Follow-ups

  • How do you announce a route change to a screen reader?
  • Why is a native `button` better than a `div` with a click handler?
  • How would you add axe checks to the existing test suite?

Tips

  • List the four layers.
  • Give examples of what each layer cannot find.
  • Mention the keyboard test first. It shows real experience.

Evaluation rubric

  • Names the layers and tools accurately.
  • Knows the limits of automation.
  • Includes concrete manual checks.