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 🙏

© 2026 – Pkg Stats / Ryan Hefner

jest-aria-snapshots

v0.2.0

Published

Jest snapshot serializer for Testing Library - creates clean accessibility tree snapshots from DOM elements

Readme

jest-aria-snapshots

A custom Jest snapshot serializer for testing accessibility with Testing Library. Converts DOM elements into clean, readable accessibility tree snapshots focusing on ARIA roles, names, and attributes.

Installation

yarn add -D jest-aria-snapshots

Development

# Install dependencies
yarn install

# Build the project
yarn build

# Watch mode
yarn dev

# Run tests
yarn test

# Watch tests
yarn test:watch

# Clean build artifacts
yarn clean

Usage

With Testing Library

This serializer is designed to work seamlessly with Testing Library's DOM queries:

/**
 * @jest-environment jsdom
 */
import { render, screen } from '@testing-library/react';
import { ariaSnapshotSerializer } from 'jest-aria-snapshots';

// Add the serializer
expect.addSnapshotSerializer(ariaSnapshotSerializer);

test('navigation accessibility', () => {
  render(
    <nav aria-label="Main navigation">
      <ul>
        <li><a href="/">Home</a></li>
        <li><a href="/about">About</a></li>
      </ul>
    </nav>
  );

  const nav = screen.getByRole('navigation');
  expect(nav).toMatchSnapshot();
});

Snapshot output:

{
  "aria-label": "Main navigation",
  "children": [
    {
      "children": [
        {
          "children": [
            {
              "name": "Home",
              "role": "link",
              "text": "Home"
            },
          ],
          "role": "listitem"
        },
        {
          "children": [
            {
              "name": "About",
              "role": "link",
              "text": "About"
            },
          ],
          "role": "listitem"
        },
      ],
      "role": "list"
    },
  ],
  "name": "Main navigation",
  "role": "navigation"
}

Global Configuration

Add it globally in your jest.config.js:

export default {
  testEnvironment: 'jsdom',
  setupFilesAfterEnv: ['<rootDir>/jest.setup.js'],
};

In jest.setup.js:

import { ariaSnapshotSerializer } from 'jest-aria-snapshots';
import '@testing-library/jest-dom';

expect.addSnapshotSerializer(ariaSnapshotSerializer);

Custom Matcher

Prefer an explicit matcher instead of registering a serializer? Extend Jest with toMatchAriaSnapshot and call it directly inside your tests:

import { render, screen } from '@testing-library/react';
import { toMatchAriaSnapshot } from 'jest-aria-snapshots';

expect.extend({ toMatchAriaSnapshot });

test('button accessibility', () => {
  render(<button aria-label="Close dialog">✕</button>);

  // Basic usage
  expect(screen.getByRole('button')).toMatchAriaSnapshot();

  // Per-call options
  expect(screen.getByRole('button')).toMatchAriaSnapshot({
    includeTextNodes: false,
  });
});

Create reusable matcher variants with predefined options via createAriaSnapshotMatcher:

import {
  toMatchAriaSnapshot,
  createAriaSnapshotMatcher,
} from 'jest-aria-snapshots';

const toMatchAriaSnapshotWithoutText = createAriaSnapshotMatcher({
  includeTextNodes: false,
});

expect.extend({
  toMatchAriaSnapshot,
  toMatchAriaSnapshotWithoutText,
});

expect(screen.getByRole('navigation')).toMatchAriaSnapshotWithoutText();

The matcher uses the same formatting logic as the serializer, so existing snapshots remain compatible whether you prefer expect.addSnapshotSerializer() or expect.extend().

What Gets Captured

The serializer extracts and formats:

From DOM Elements:

  • ✅ Implicit ARIA roles (button, link, navigation, etc.)
  • ✅ Explicit role attributes
  • ✅ Accessible names (from aria-label, aria-labelledby, labels, text content)
  • ✅ Accessible descriptions (from aria-describedby)
  • ✅ All aria-* attributes
  • ✅ State properties (checked, disabled)
  • ✅ Child elements with roles (nested accessibility tree)

From Plain Objects:

  • ✅ Objects with role, name, or description properties
  • ✅ Objects with aria-* attributes

Examples

Button with aria-label

const button = screen.getByRole('button', { name: /close/i });
expect(button).toMatchSnapshot();
// {
//   "aria-label": "Close dialog",
//   "name": "Close dialog",
//   "role": "button",
// }

Form with inputs

const form = screen.getByRole('form');
expect(form).toMatchSnapshot();
// {
//   "children": [
//     {
//       "name": "Email",
//       "role": "textbox",
//     },
//   ],
//   "role": "form"
// }

Checkbox state

const checkbox = screen.getByRole('checkbox');
expect(checkbox).toMatchSnapshot();
// {
//   "checked": true,
//   "name": "I agree to the terms",
//   "role": "checkbox",
// }

Why Use This?

Traditional DOM snapshots can be noisy and fragile. This serializer focuses on the accessibility tree - what actually matters for users with assistive technology. It helps you:

  • 🎯 Test the user experience, not implementation details
  • 📸 Create focused snapshots that highlight accessibility
  • 🔍 Catch accessibility regressions early
  • 📚 Document component accessibility in your tests