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

jest-pa11y

v1.3.0

Published

Custom Jest matcher for Pa11y for testing accessibility

Downloads

59

Readme

jest-pa11y

Commitizen friendly

jest-pa11y is a custom Jest matcher for Pa11y, useful for testing the accessibility of html, React components or Vue components.

Pa11y's api is typically geared towards testing the accessibility of a url. jest-pa11y allows for the testing for accessibility issues at the component level, rather than at the page level, providing more clarity as to which parts of pages are having issues, and allowing you to easily test the accessibility of different states of your components.

How it works

jest-pa11y starts a simple http server serving a webpage. Html or Node Elements provided to runPa11y() are rendered to a string and then rendered to the webpage via Puppeteer. Then Pa11y is used to analyze the content of the webpage, returning an array of voilations if any are found. The results can then be used in assertions via the expect extension .toHaveNoPa11yViolations().

Installation

npm install --save-dev jest-pa11y

or

yarn add --save-dev jest-pa11y

Usage

// jest.config.js
{
  "preset": "jest-pa11y"
}
const { runPa11y } = require('jest-pa11y');

it('should demonstrate this matcher`s usage', async () => {
  const render = () => '<img src="#"/>';
  const html = render();
  expect(await runPa11y(html)).toHaveNoPa11yViolations();
});

Screenshot of the resulting output from the usage example

Pa11y configuration

The runPa11y function allows options to be set with the same options as documented in Pa11y:

const { runPa11y } = require('jest-pa11y');

it('image has no Pa11y violations', async () => {
  const results = await runPa11y('<img src="#"/>', {
    standard: 'WCAG2AA',
  });

  expect(results).toHaveNoPa11yViolations();
});

it('button has no Pa11y violations', async () => {
  const results = await runPa11y('<img src="#"/>', {
    standard: 'WCAG2AA',
  });

  expect(results).toHaveNoPa11yViolations();
});

Additionally, the configurePa11y function returns a preconfigured runPa11y function. This is useful if you want to globally configure Pa11y.

const { configurePa11y } = require('jest-pa11y');

const runPa11y = configurePa11y({
  standard: 'WCAG2AA',
});

it('image has no Pa11y violations', async () => {
  const results = await runPa11y('<img src="#"/>');
  expect(results).toHaveNoPa11yViolations();
});

it('button has no Pa11y violations', async () => {
  const results = await runPa11y('<img src="#"/>');
  expect(results).toHaveNoPa11yViolations();
});

Inspiration

This project was originally inspired by and forked from jest-axe. jest-pa11y differs in that it uses Pa11y to implement both aXe and HTML_CodeSniffer. Pa11y also normalizes the output between both runners to help provide consistent a feedback format for both runners.