Skip to content

Set up jest-axe for a11y testing #2262

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Jun 28, 2024
Merged

Set up jest-axe for a11y testing #2262

merged 6 commits into from
Jun 28, 2024

Conversation

beaesguerra
Copy link
Member

@beaesguerra beaesguerra commented Jun 27, 2024

Summary:

  • Configure jest-axe
  • Adds toHaveNoA11yViolations matcher for jest tests
  • Configure jest await async lint rule for toHaveNoA11yViolations matcher. This is so we don't forget to await the a11y checks. Also fixing some linting issues that were caught when this rule was enabled
  • Adds an example a11y test for Title component

Note: this set up is similar to how jest-axe is set up in webapp (I referenced this jest-axe webapp PR!)

Issue: WB-466

Test plan:

  • Run unit tests and linting to make sure there are no errors
  • Manually tested jest-axe is working by making sure that it catches expected a11y errors locally. Used this example with headings that are out of order:
test("expect a11y test to fail", async () => {
  // Arrange
  // Act
  const {container} = render(
      <div>
        <h1>title1</h1>
        <h3>title2</h3>
      </div>,
  );

  // Assert
  await expect(container).toHaveNoA11yViolations();
});

This is so we don't forget to await the a11y checks. Also fixing some linting issues that were caught when this rule was enabled
@beaesguerra beaesguerra self-assigned this Jun 27, 2024
Copy link

changeset-bot bot commented Jun 27, 2024

🦋 Changeset detected

Latest commit: c969e7a

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 0 packages

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

Copy link
Contributor

github-actions bot commented Jun 27, 2024

Size Change: 0 B

Total Size: 97.1 kB

ℹ️ View Unchanged
Filename Size
packages/wonder-blocks-accordion/dist/es/index.js 3.78 kB
packages/wonder-blocks-banner/dist/es/index.js 1.53 kB
packages/wonder-blocks-birthday-picker/dist/es/index.js 1.72 kB
packages/wonder-blocks-breadcrumbs/dist/es/index.js 1.13 kB
packages/wonder-blocks-button/dist/es/index.js 4.28 kB
packages/wonder-blocks-cell/dist/es/index.js 2.25 kB
packages/wonder-blocks-clickable/dist/es/index.js 3.3 kB
packages/wonder-blocks-core/dist/es/index.js 3.66 kB
packages/wonder-blocks-data/dist/es/index.js 6.34 kB
packages/wonder-blocks-dropdown/dist/es/index.js 14.4 kB
packages/wonder-blocks-form/dist/es/index.js 5.34 kB
packages/wonder-blocks-grid/dist/es/index.js 1.36 kB
packages/wonder-blocks-i18n/dist/es/index.js 4.6 kB
packages/wonder-blocks-icon-button/dist/es/index.js 3.23 kB
packages/wonder-blocks-icon/dist/es/index.js 1.06 kB
packages/wonder-blocks-labeled-field/dist/es/index.js 72 B
packages/wonder-blocks-layout/dist/es/index.js 1.94 kB
packages/wonder-blocks-link/dist/es/index.js 2.53 kB
packages/wonder-blocks-modal/dist/es/index.js 5.51 kB
packages/wonder-blocks-pill/dist/es/index.js 1.64 kB
packages/wonder-blocks-popover/dist/es/index.js 4.85 kB
packages/wonder-blocks-progress-spinner/dist/es/index.js 1.52 kB
packages/wonder-blocks-search-field/dist/es/index.js 1.54 kB
packages/wonder-blocks-switch/dist/es/index.js 2.09 kB
packages/wonder-blocks-testing/dist/es/index.js 3.94 kB
packages/wonder-blocks-theming/dist/es/index.js 693 B
packages/wonder-blocks-timing/dist/es/index.js 1.8 kB
packages/wonder-blocks-tokens/dist/es/index.js 1.74 kB
packages/wonder-blocks-toolbar/dist/es/index.js 855 B
packages/wonder-blocks-tooltip/dist/es/index.js 6.91 kB
packages/wonder-blocks-typography/dist/es/index.js 1.48 kB

compressed-size-action

Copy link

codecov bot commented Jun 27, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 96.36%. Comparing base (dcafa86) to head (c969e7a).

Additional details and impacted files

Impacted file tree graph

@@            Coverage Diff             @@
##             main    #2262      +/-   ##
==========================================
+ Coverage   93.91%   96.36%   +2.45%     
==========================================
  Files         248      248              
  Lines       29514    29514              
  Branches     2407     2538     +131     
==========================================
+ Hits        27717    28441     +724     
+ Misses       1793     1056     -737     
- Partials        4       17      +13     

see 54 files with indirect coverage changes


Continue to review full report in Codecov by Sentry.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update dcafa86...c969e7a. Read the comment docs.

Copy link
Contributor

github-actions bot commented Jun 27, 2024

A new build was pushed to Chromatic! 🚀

undefined

Chromatic results:

Metric Total
Captured snapshots undefined
Tests with visual changes undefined
Total stories undefined
Inherited (not captured) snapshots [TurboSnap] undefined
Tests on the build undefined

@@ -47,7 +47,7 @@ describe("Test utils", () => {
await promise;

// Assert
expect(promise).resolves.toHaveProperty("type", "resize");
Copy link
Member Author

Choose a reason for hiding this comment

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

Fixing this after the jest-await-async-matchers rule caught this

@@ -259,7 +259,7 @@ describe("RespondWith", () => {
const act = Promise.race([settleableResponse, otherResponse]);

// Assert
await expect(act).resolves;
Copy link
Member Author

Choose a reason for hiding this comment

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

The jest-await-async-matchers linting rule was complaining about this too. The suggested fix was to add await before it(...) where we define the test case, but that had errors too. Updating it to use toResolve instead seemed to fix it and the tests still pass. Let me know if you have any other suggestions for this!

@@ -53,7 +53,7 @@ jobs:
with:
changed-files: ${{ steps.changed.outputs.files }}
files: packages/ # Only look for changes in packages
globs: "!(**/__tests__/**), !(**/dist/*)" # Ignore test files
globs: "!(**/__tests__/**), !(**/dist/*), !(**/*.test.ts)" # Ignore test files
Copy link
Member Author

Choose a reason for hiding this comment

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

Excluding .test.ts files from the changeset check

It was failing previously because there was no changeset for the wonder-blocks-layout package. A changeset isn't needed since this package only has an updated test!

Error: Changeset entry required for @khanacademy/wonder-blocks-layout because there have been changes since the last release.

@beaesguerra beaesguerra marked this pull request as ready for review June 27, 2024 21:07
@khan-actions-bot
Copy link
Contributor

Gerald

Required Reviewers
  • @Khan/wonder-blocks for changes to .eslintrc.js, package.json, yarn.lock, .changeset/rotten-bats-count.md, types/matchers.d.ts, .github/workflows/node-ci-pr.yml, config/jest/test.config.js, config/jest/matchers/to-have-no-a11y-violations.ts, packages/wonder-blocks-layout/src/util/test-util.test.ts, packages/wonder-blocks-testing/src/__tests__/respond-with.test.ts, packages/wonder-blocks-typography/src/components/__tests__/title.test.tsx

Don't want to be involved in this pull request? Comment #removeme and we won't notify you of further changes.

@khan-actions-bot khan-actions-bot requested a review from a team June 27, 2024 21:07
Copy link
Contributor

npm Snapshot: NOT Published

🤕 Oh noes!! We couldn't find any changesets in this PR (7a516f9). As a result, we did not publish an npm snapshot for you.

Copy link
Member

@jeresig jeresig left a comment

Choose a reason for hiding this comment

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

These changes all look reasonable to me, thank you Bea!

@beaesguerra beaesguerra merged commit 69c6b37 into main Jun 28, 2024
41 of 43 checks passed
@beaesguerra beaesguerra deleted the setup-jest-axe branch June 28, 2024 15:41
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