Jest/Vitest Matcher for ARIA Live Regions

2.0.0 · active · verified Wed Apr 22

`extend-to-be-announced` is a utility package providing custom Jest and Vitest matchers, specifically `toBeAnnounced`, for robustly testing ARIA live regions. It aims to simplify accessibility testing by abstracting away the complexities of how assistive technologies handle live region updates, preventing common false positives associated with simply checking DOM text content. The package is currently at version 2.0.0 and receives updates as needed, particularly when underlying dependencies or testing frameworks evolve. It differentiates itself by leveraging `aria-live-capture` internally to precisely detect actual announcements, ensuring tests reflect true user experience rather than just DOM state. This approach helps developers write more reliable and accurate accessibility tests, especially for dynamic content updates.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates the basic usage of `toBeAnnounced` matcher for both Jest and Vitest, showing how to test for announced updates and avoid false positives with initial content.

import { render, screen, cleanup } from '@testing-library/react';
import { register } from 'extend-to-be-announced/jest/register';

// Configure Jest setup if not done globally via setupFilesAfterEnv
register();

describe('ARIA live region announcements', () => {
  afterEach(cleanup);

  it('announces content updates to polite live regions', () => {
    const { rerender } = render(<div role="status"></div>);
    // Initially empty, nothing to announce
    expect('').not.toBeAnnounced();

    // Update the content of the live region
    rerender(<div role="status">Loading data...</div>);

    // Expect 'Loading data...' to be announced politely
    expect('Loading data...').toBeAnnounced('polite');
  });

  it('does not announce initial content of a live region', () => {
    render(<div role="status">Initial Message</div>);
    // The initial content 'Initial Message' should not be announced
    expect('Initial Message').not.toBeAnnounced();
  });
});

view raw JSON →