Skip to content

Instantly share code, notes, and snippets.

@LeeCheneler
Created December 2, 2020 12:23
Show Gist options
  • Select an option

  • Save LeeCheneler/6eb7eae2a8233b51c3970e225604ff24 to your computer and use it in GitHub Desktop.

Select an option

Save LeeCheneler/6eb7eae2a8233b51c3970e225604ff24 to your computer and use it in GitHub Desktop.
import "@testing-library/jest-dom/extend-expect";
import { cleanup } from "@testing-library/react";
import { axe, toHaveNoViolations } from "jest-axe";
// stop RTL from cleaning up after itself so we can assert on DOM after tests with aXe
process.env.RTL_SKIP_AUTO_CLEANUP = "true";
// configure aXe
expect.extend(toHaveNoViolations);
afterEach(async () => {
try {
// if something has been rendered then we should assert it is accessible
if (document.body.childNodes.length > 0) {
expect(await axe(document.body)).toHaveNoViolations();
}
} finally {
// always cleanup the DOM
cleanup();
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment