npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

extend-to-be-announced

v2.0.0

Published

Utility for asserting ARIA Live Regions

Downloads

1,804

Readme

extend-to-be-announced

version

Motivation | Installation | Usage | Support

Utility for asserting ARIA live regions.

extend-to-be-announced is a matcher extender for Jest and Vitest. It makes validating ARIA live regions extremely easy. Internally it is utilizing aria-live-capture for announcement detection.

For Storybook integration see storybook-addon-aria-live.

Motivation

Read more about inspiration from Building testing tools for ARIA live regions.

Validating ARIA live regions with @testing-library and jest-dom requires developers to consider implementation details. Current solutions are prone to false positives.

In test below it is not clearly visible that Loading... is not actually announced. Assistive technologies are only expected to announce updates of ARIA live regions - not the initial content.

render(<div role="status">Loading...</div>);

const liveRegion = screen.getByRole('status');

// Loading is not be announced by assistive technologies ❌
// Content of live region has not updated. This is it's initial text content.
expect(liveRegion).toHaveTextContent('Loading...');

Instead developers should check that messages are rendered into existing ARIA Live regions.

const { rerender } = render(<div role="status"></div>);

// Live region should be present
const liveRegion = screen.getByRole('status');

// Live region should initially be empty
expect(liveRegion).toBeEmptyDOMElement();

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

// Loading is announced by assistive technologies ✅
expect(liveRegion).toHaveTextContent('Loading...');

toBeAnnounced can be used to hide such implementation detail from tests.

const { rerender } = render(<div role="status"></div>);

rerender(<div role="status">Loading...</div>);

expect('Loading...').toBeAnnounced('polite');

Installation

extend-to-be-announced should be included in development dependencies.

npm install --save-dev extend-to-be-announced

Usage

Test setup

There are out-of-the-box setups for Vitest and Jest.

Vitest

Import registration entrypoint in your test setup.

import 'extend-to-be-announced/vitest';

For setting up registration options use register(options) method instead.

import { register } from 'extend-to-be-announced/vitest/register';

register({
    /** Indicates whether live regions inside `ShadowRoot`s should be tracked. Defaults to false. */
    includeShadowDom: true,
});

Jest

Import registration entrypoint in your test setup.

import 'extend-to-be-announced/jest';

For setting up registration options use register(options) method instead.

import { register } from 'extend-to-be-announced/jest/register';

register({
    /** Indicates whether live regions inside `ShadowRoot`s should be tracked. Defaults to false. */
    includeShadowDom: true,
});

Note that you'll need to add ESM dependencies of this package to your Jest config's transformIgnorePatterns. For example with pnpm:

transformIgnorePatterns: ['/node_modules/.pnpm/(?!(aria-live-capture)@)'],

Typescript

This package utilizes Typescript's exports support for type declarations. You'll need to set "moduleResolution": "node16" or "moduleResolution": "nodenext" in your tsconfig.json in order to have typings picked properly. For legacy setups where certain fields of tsconfig.json cannot be modified, such as create-react-app, there is a work-around entrypoint for jest.

{
    "compilerOptions": {
        "moduleResolution": "node16" // Or nodenext
    }
}

Assertions

toBeAnnounced

Assert whether given message was announced by assistive technologies. Accepts string or regexp as matcher value.

expect('Loading...').toBeAnnounced();
expect(/loading/i).toBeAnnounced();
expect('Error occured...').toBeAnnounced();
expect(/error occured/i).toBeAnnounced();

Politeness setting of announcement can be optionally asserted.

expect('Loading...').toBeAnnounced('polite');
expect('Error occured...').toBeAnnounced('assertive');
Examples
Render#1 | <div role="status"></div>
Render#2 | <div role="status">Loading</div>
PASS ✅  | expect('Loading').toBeAnnounced('polite');
Render#1 | <div role="alert">Error</div>
PASS ✅  | expect('Error').toBeAnnounced('assertive');
Render#1 | <div></div>
Render#2 | <div role="alert">Error</div>
PASS ✅  | expect('Error').toBeAnnounced();
Render#1 | <div role="status">Loading</div>
FAIL ❌  | expect('Loading').toBeAnnounced();
Render#1 | <div></div>
Render#2 | <div role="status">Loading</div>
FAIL ❌  | expect('Loading').toBeAnnounced();

With register({ includeShadowDom: true }):

Render#1 | <div role="status">
         |     #shadow-root
         |     <div></div>
         | </div>
         |
Render#2 | <div role="status">
         |     #shadow-root
         |     <div>Loading</div>
         | </div>
         |
PASS ✅  | expect('Loading').toBeAnnounced()

Utilities

getAnnouncements

Get all announcements as Map<string, PolitenessSetting>.

import { getAnnouncements } from 'extend-to-be-announced';
getAnnouncements();

> Map {
>   "Status message" => "polite",
>   "Alert message" => "assertive",
> }

clearAnnouncements

Clear all captured announcements.

import { clearAnnouncements } from 'extend-to-be-announced';
clearAnnouncements();

Support

| Feature | Status | | :-------------: | :----: | | role | ✅ | | aria-live | ✅ | | aria-atomic | ❌ 👷 | | aria-busy | ❌ | | aria-relevant | ❌ |