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

@sa11y/vitest

v8.4.0

Published

Accessibility testing matcher for vitest

Readme

@sa11y/vitest

Accessibility matcher for Vitest

Overview

The toBeAccessible() API from this library can be used in Vitest unit tests to test HTML elements or DOM for accessibility.

Note: The core accessibility logic is provided by @sa11y/matcher, which can be used directly for custom or framework-agnostic testing.

Install

  • Using yarn: yarn add -D @sa11y/vitest
  • Using npm: npm install -D @sa11y/vitest

Setup

The accessibility APIs need to be registered with Vitest before they can be used in tests.

Project level

You can set up the Sa11y API once at the project level to make it available to all the Vitest tests in the project.

  • Add a Vitest setup file (e.g. vitest-setup.ts) and add the following code to register the Sa11y matcher:
import { setup } from '@sa11y/vitest';
// Register the Sa11y matcher with default options
setup();

// Register with custom options for automatic checks and DOM saving
setup({
    autoCheckOpts: { runAfterEach: true, cleanupAfterEach: true },
    renderedDOMSaveOpts: {
        /* your RenderedDOMSaveOpts here */
    },
});
  • In your vitest.config.ts, add the setup file to the setupFiles array:
import { defineConfig } from 'vitest/config';

export default defineConfig({
    test: {
        setupFiles: ['./vitest-setup.ts'],
    },
});
  • This makes the toBeAccessible API available for any test in the project.

Test module level

You can also invoke setup before using the toBeAccessible API in individual test modules:

import { setup } from '@sa11y/vitest';

beforeAll(() => {
    setup();
});
  • This makes the toBeAccessible API available for the tests only in that specific test module.

Usage

  • toBeAccessible can be invoked on the entire document (JSDOM) or on a specific HTML element to check for accessibility:
import { setup } from '@sa11y/vitest';
import { extended, full } from '@sa11y/preset-rules';

beforeAll(() => {
    setup();
});

it('should be accessible', async () => {
    // Setup DOM to be tested for accessibility
    // ...

    // Assert that DOM is accessible (using base preset-rule)
    await expect(document).toBeAccessible();

    // Can be used to test accessibility of a specific HTML element
    const elem = document.getElementById('foo');
    await expect(elem).toBeAccessible();

    // To test using an extended ruleset with best practices and experimental rules
    await expect(document).toBeAccessible(extended);

    // To test using all rules provided by axe
    await expect(document).toBeAccessible(full);
});

Caveats

  • Async: toBeAccessible must be invoked with async/await or Promise.
  • Fake Timers: When timers are mocked (e.g. with Vitest's fake timers), the accessibility API can timeout. Switch to real timers before invoking the accessibility API.
  • DOM: Accessibility checks require a rendered DOM (e.g., JSDOM or a real browser environment).
  • template: <template> elements are not rendered in DOM and hence cannot be checked directly without rendering.

Automatic checks

The Sa11y Vitest API can be set up to be automatically invoked at the end of each test as an alternative to adding the toBeAccessible API at the end of each test.

  • When automatic checks are enabled, each child element in the DOM body will be checked for accessibility and failures reported as part of the test.
import { setup } from '@sa11y/vitest';

setup({ autoCheckOpts: { runAfterEach: true } });

// To optionally clean up the body after running accessibility checks
setup({ autoCheckOpts: { runAfterEach: true, cleanupAfterEach: true } });

// To customize how/where the rendered DOM is saved during automatic checks
setup({
    autoCheckOpts: { runAfterEach: true },
    renderedDOMSaveOpts: {
        /* your RenderedDOMSaveOpts here */
    },
});

Options

  • autoCheckOpts: Options for automatic accessibility checks (see below)
  • renderedDOMSaveOpts: Options for saving the rendered DOM during automatic checks. Allows customizing how and where the DOM is saved for debugging or reporting purposes. See @sa11y/matcher RenderedDOMSaveOpts for details.

autoCheckOpts:

  • runAfterEach: Run after each test (for integration with test runners)
  • cleanupAfterEach: Clean up the DOM after checking
  • consolidateResults: Deduplicate results
  • filesFilter: Array of file path substrings to skip automatic checks for
  • enableIncompleteResults: Include incomplete results

Note: DOM mutation observer mode is not supported: the runDOMMutationObserver option and the SA11Y_ENABLE_DOM_MUTATION_OBSERVER environment variable are ignored. As @sa11y/matcher only saves the rendered DOM in that mode, renderedDOMSaveOpts has no effect currently.

DOM checked: With the SA11Y_AUTO environment variable set, the automatic checks check the final DOM rendered by each test, even when it is cleaned up by an afterEach hook running before the automatic checks (e.g. cleanup() of Testing Library registered after setup()):

  • The DOM is saved when the first node is removed from it in the afterEach hooks of a test, and checked instead of the cleaned up DOM
  • Nodes removed during a test, e.g. when a component re-renders, don't save the DOM as it isn't final yet
  • A DOM saved in a test is never checked in another test, e.g. when the automatic checks of the test are skipped
  • A test removing what it rendered at its end, e.g. with unmount() or cleanup() of Testing Library, removes it during the test. The automatic checks then check the DOM left, e.g. an empty body. Check the rendered DOM with toBeAccessible() before removing it.
  • Limitation: when a test is retried after failing in its afterEach hooks, e.g. with a11y issues, and a soft assertion (expect.soft) fails in the retry, nodes removed during the rest of the retry save the DOM. Such a test fails anyway, but can report a11y issues of a DOM that isn't final.
  • Concurrent tests (e.g. describe.concurrent) aren't supported, as they render in the same DOM

Results Processor

vitestResultsProcessor transforms accessibility errors in Vitest test results, mirroring the Jest group violation results processor. It is meant to be called by a Vitest reporter, e.g. when running automatic checks, before the reporter writes the results:

  • The message and stack of a11y errors are replaced with the a11y violations grouped by rule
  • Other errors are dropped and failed tests without a11y errors are marked as passed, so that only a11y failures are reported
  • Tests nested in suites (describe blocks) are processed as well
  • Errors of suites and test files themselves, e.g. from hooks or failing to import a test file, are processed the same way. Suites and test files without a11y errors left in them or their tests are marked as passed.
  • Test results are modified in place. Hence other reporters see the processed results only when registered after the reporter calling the processor.
  • The exit code of the test run is not affected as Vitest sets it before calling the reporters at the end of the run
import { experimental_getRunnerTask } from 'vitest/node';
import type { Reporter, TestModule } from 'vitest/node';
import { vitestResultsProcessor } from '@sa11y/vitest';

export default class A11yReporter implements Reporter {
    onTestRunEnd(testModules: ReadonlyArray<TestModule>): void {
        if (process.env.SA11Y_AUTO) {
            vitestResultsProcessor(testModules.map((testModule) => experimental_getRunnerTask(testModule)));
        }
        // Write the results e.g. as JUnit XML
    }
}

Related Packages

  • @sa11y/matcher: Provides the core accessibility checking APIs used by this package. Use it directly for custom test runners or advanced integrations.