Skip to content

Custom Vitest matcher for testing accessibility with aXe. Forked from jest-axe.

License

Notifications You must be signed in to change notification settings

koralle/vitest-axe

 
 

Repository files navigation

vitest-axe

Custom Vitest matcher for testing accessibility with aXe


version MIT License

Watch on GitHub

This library is a fork of jest-axe. It shares that library's implementation and API. It is intended to make it easier to include its matchers without clashes between Vitest and Jest's environment or types.

See the README for the original package for usage details.

Important

There is currently a bug in Happy DOM related to its implementation of Node.prototype.isConnected. This causes compatibility issues with axe, which means that this library will not work if your Vitest environment is set to happy-dom.

Installation

This module should be installed as one of your project's devDependencies:

# with npm
npm install --save-dev @koralle/vitest-axe
# yarn
yarn add --dev @koralle/vitest-axe
# pnpm
pnpm add -D @koralle/vitest-axe

Setup

Extend in test setup file

You can import the matcher from @koralle/vitest-axe then pass them to Vitest's expect.extend method yourself:

// vitest-setup.js
import { toHaveNoViolations } from "@koralle/vitest-axe"
import { expect } from "vitest"

expect.extend({ toHaveNoViolations })

Extend in individual tests

import { axe, toHaveNoViolations } from "@koralle/vitest-axe"

expect.extend({ toHaveNoViolations })

it("should have no axe violations", async () => {
  const html = "<html><!-- accessible markup! --></html>"
  expect(await axe(html)).toHaveNoViolations()
})

With TypeScript

You will need to augment Vitest's Assertion and AsymmetricMatchersContaining interfaces.

import "vitest"
import type { AxeMatchers } from "@koralle/vitest-axe"

declare module "vitest" {
  export interface Assertion extends AxeMatchers {}
  export interface AsymmetricMatchersContaining extends AxeMatchers {}
}

Further reading:

Usage

import { axe } from "@koralle/vitest-axe"

it("should demonstrate this matcher's usage", async () => {
  const render = () => '<img src="#"/>'
  // pass anything that outputs html to axe
  const html = render()
  expect(await axe(html)).toHaveNoViolations()
})

About

Custom Vitest matcher for testing accessibility with aXe. Forked from jest-axe.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • TypeScript 85.0%
  • JavaScript 15.0%