<link rel="stylesheet" href="assets/fonts/inter.css"> Angular Design System Tests with Jest and A11y Skip to main content

Angular Design System Testing with Jest and A11y

Nov 30, 2026By Viral Patel

Use Jest to test Angular design-system behavior, keyboard interaction, accessibility semantics, and resilient component contracts.

Angular Design System Testing with Jest and Accessibility Checks

A design-system component is a promise to product teams: given a documented input and context, it will behave consistently, expose understandable states, and work for people using different input methods. A test that only checks whether Angular created the component verifies very little of that promise.

This workflow focuses on behavior that consumers depend on: rendered semantics, events, disabled and loading states, keyboard interaction, and accessible names. Examples use Angular testing concepts and Jest-style assertions. This repository uses Jest, not Karma or Vitest; Angular's current testing guide describes Vitest as the default for new CLI projects, so keep that distinction clear when applying its general testing guidance.

Start with the component contract

Before writing a spec, write down the public contract in plain language. For a button component, that might include:

  • It renders a native button and accepts the intended label and visual variant.
  • It emits an action when activated, unless disabled.
  • It communicates its disabled state to assistive technology and browser interaction.
  • It supports standard keyboard activation through the native element.
  • It does not silently replace the consumer's accessible name with decorative text.

For a dialog trigger, the contract may include the relationship to the dialog, focus movement, escape behavior, and focus restoration. Be precise about which layer owns each behavior. A component should not claim a keyboard or focus behavior that it does not implement.

Testing the contract reduces brittle tests. A consumer should be able to change an internal wrapper or class without breaking tests if the public semantics remain correct. Conversely, an apparent visual change should fail a test if it removes a label, stops an event, or makes an essential state unreachable.

Use the repository's test setup, not an assumed default

Check package.json, the Jest configuration, test setup files, and nearby component specs before adding tools or configuration. This project uses Jest with jest-preset-angular; its existing commands and mocks are the source of truth for how to run tests here. Keep new specs consistent with that setup and use the smallest relevant test command.

Angular's official testing guide currently documents Vitest as the default setup for new CLI projects and describes testing concepts and alternatives. That is useful context, not a reason to migrate an established codebase while adding a component test. Jest, Karma, and Vitest are distinct runners with different configuration and APIs; avoid copying runner-specific setup or fake-timer snippets without checking whether they apply.

For an Angular component, use the test environment already configured in the project. TestBed can declare or import the component and provide the dependencies it needs. Keep setup local unless a repeated, stable fixture genuinely belongs in a shared helper. If a component imports a large module only to gain one dependency, consider whether a focused test double is appropriate—but do not mock away the behavior that the component is meant to provide.

Query the interface the way a person encounters it

Prefer selectors that express meaning: a role and accessible name, a label, or visible text. For example, a button test should find “Save changes” as a button rather than query an implementation class such as .primary-action. This catches regressions where the element remains in the DOM but loses its user-facing role or name.

Illustrative Jest-style test:

it('emits save when the named button is activated', () => {
  const saved = jest.fn();
  fixture.componentInstance.saved.subscribe(saved);
  fixture.detectChanges();

  const button = fixture.nativeElement.querySelector(
    'button[aria-label="Save changes"]'
  ) as HTMLButtonElement;
  button.click();

  expect(saved).toHaveBeenCalledTimes(1);
});

In a real test, a role-and-name query from an established testing utility can make the intent even clearer, provided that utility is already part of the project. Use selectors that match the actual component contract; do not add aria-label just to make a test pass when visible text can provide the name naturally.

Then test state changes through supported inputs. If a button is disabled, assert that the native button is disabled and that activation does not emit. If a toggle is on, assert the appropriate accessible state as well as any visible indicator. The state should not be conveyed by color alone or by a class that assistive technology cannot interpret.

Avoid asserting every CSS class or the complete generated HTML string. Class-level assertions can be suitable for a documented styling hook, but they should not replace behavior assertions. Large snapshots often turn a semantic regression into a wall of diff that reviewers may accept mechanically.

Test keyboard behavior and focus deliberately

Native HTML controls already provide expected keyboard behavior. Use a <button> for an action and an <a> for navigation instead of a clickable <div> that requires reimplementing focus and activation. A unit test can verify that the expected semantic element exists, that disabled controls cannot activate, and that custom interaction code responds to the documented keys.

For a composite widget—such as a menu, listbox, tab set, or dialog—document its interaction model before adding key handlers. Test the actual supported keys, focus destination, and state updates. For example, if arrow keys move between items, assert the focused item after the key event and verify that the active item is exposed correctly. Do not test a key simply because a pattern elsewhere uses it; match the widget's intended behavior.

Focus is observable in the DOM environment, but simulated DOMs do not reproduce every browser and assistive-technology detail. A focused unit test is useful for regressions in component logic; it is not a substitute for checking the interaction in a real browser. For dialogs, overlays, and menus, include an integration or browser-level check when focus trapping, stacking, or restoration is central to the experience.

A practical keyboard checklist:

  • Can the control receive focus through the expected route?
  • Is the visible focus indicator preserved rather than removed by component styles?
  • Does Enter or Space activate the correct native control?
  • Do custom key interactions match the documented widget pattern?
  • After an overlay closes, does focus return to a sensible invoking control?
  • Can a disabled or unavailable action be understood without relying on color?

Cover accessible names, relationships, and status

Tests can catch common semantic gaps early. Check that icon-only buttons have an accessible name; form fields have a programmatically associated label; errors identify the affected field and describe how to resolve the problem; and expanded or selected controls expose their state. For dynamic feedback, verify that a status message is present in the expected live region when the product relies on it.

Do not treat ARIA attributes as decoration. A role or state should describe behavior that the component actually supports. Prefer native semantics when they fit. If a component uses aria-describedby, test that the referenced ID exists and that the relationship points to the intended hint or error. If it uses aria-controls, confirm that the target is present when the relationship is meaningful.

For form controls, test the user-visible sequence: label, input, invalid value, error text, and correction. A component that sets aria-invalid="true" but leaves the person guessing which value is wrong has not delivered a complete error experience. Likewise, an error summary should link to the fields it identifies when that is part of the design.

Automated accessibility matchers can complement direct assertions, but they do not prove that labels are clear, reading order is logical, or a workflow makes sense with a screen reader. Keep tests focused on known requirements and schedule manual evaluation for high-impact interaction patterns.

Build a useful layer of test coverage

Use a mix of tests rather than trying to make every test do everything:

  1. Component contract tests verify public inputs, outputs, semantics, and state.
  2. Composition tests verify a real component inside a representative form, menu, or page.
  3. Browser checks cover behavior that depends on layout, native focus, or platform integration.
  4. Accessibility evaluation includes automated inspection and sessions with people who use assistive technologies.

For a design-system button, a compact suite may test the default accessible name, a disabled case, and the emitted event. It does not need a test for every color token, every internal element, or every possible combination of inputs. For a dialog, add tests around focus and dismissal because those are essential interaction responsibilities.

Keep asynchronous behavior understandable. Prefer the Angular testing utilities already used in the codebase and wait for the state change the component promises. Use fake timers only when the component truly depends on time, such as a documented debounce. Jest's timers are runner-specific; mixing timer controls with Angular's asynchronous testing tools can create confusing tests if the suite is not designed for both.

Make failures actionable and maintainable

Give each test a name that describes the user-observable behavior, such as “does not submit when the required field is empty.” Arrange the fixture, perform an interaction, then assert the result. Avoid tests that inspect private fields or call private methods: those tests lock down implementation details and may pass even when the rendered experience is broken.

If a test fails, ask whether the public behavior regressed or the test depends on an incidental detail. Update a brittle query only when the behavior remains correct; do not weaken an accessibility assertion just to restore a green build. When a component's responsibilities are unclear, clarify its contract before expanding the spec.

Before publishing a component, review these questions:

  • Do tests verify rendered semantics and accessible names, not only creation?
  • Are enabled, disabled, selected, expanded, error, and loading states covered where relevant?
  • Do keyboard checks reflect the chosen native or custom interaction model?
  • Are focus changes tested for overlays and composite widgets?
  • Can the tests be run using the repository's current Jest scripts and setup?
  • Are a few essential flows checked in a browser or with assistive-technology users?

Design-system tests work best as executable documentation: they describe the promise a component makes to its consumers while catching meaningful regressions. Use Jest to test that promise in this repository, and complement unit tests with real interaction evaluation rather than treating a passing test report as proof of accessibility.

References