Skip to content

Conversation

@calebeby
Copy link
Member

@calebeby calebeby commented Jun 2, 2022

This assertion, based on jest-puppeteer-axe, allows you to check a page using the axe accessibility linter.

The output is a little nicer than jest-puppeteer-axe's because it shows the markup of the elements which are breaking axe rules (as opposed to a non-unique CSS selector), so it is easier to figure out which elements need to be fixed. In the browser, you can click on the markup to see the relevant elements in the devtools elements panel.

Documentation

test(
  'Axe tests',
  withBrowser(async ({ utils, page }) => {
    await utils.injectHTML(`
      <h1>Some html</h1>
    `);
    await expect(page).toPassAxeTests();
  }),
);

Here's what the output looks like in the CLI:

Screen Shot 2022-06-02 at 8 45 29 AM

Here's what it looks like in the browser:

Screen Shot 2022-06-02 at 8 45 53 AM

Testing

Run the tests in watch mode:

git checkout to-pass-axe-tests
npm i
npm run build
npm run test:watch topassaxe

Note: the HTML syntax highlighting in the CLI output isn't shown when Pleasantest runs in its own test suite. This is so that the test snapshots are still readable and don't get covered with escape codes for coloring.

Test the browser output by changing tests/accessibility/toPassAxeTests.test.ts to use withBrowser.headed instead of withBrowser. Then it should open a browser for the test, and close it once it passes. To make the test fail so you can see the failure message in the browser, under the utils.injectHTML(...) line add a new line:

await expect(page).toPassAxeTests()

@calebeby calebeby marked this pull request as ready for review June 3, 2022 19:59
Copy link
Contributor

@Paul-Hebert Paul-Hebert left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is awesome! Code looks good to me besides one nit.

@calebeby calebeby merged commit 730300e into main Jun 6, 2022
@calebeby calebeby deleted the to-pass-axe-tests branch June 6, 2022 16:59
@github-actions github-actions bot mentioned this pull request Jun 6, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants