Skip to content
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

feat(jest): add jest accessibility matcher #9

Merged
merged 61 commits into from May 13, 2020

Conversation

mohanraj-r
Copy link
Contributor

@mohanraj-r mohanraj-r commented Apr 28, 2020

Primary addition starts at the jest package

jest

Accessibility matcher for Jest

Setup

The accessibility matcher helper APIs need to be registered with Jest before they can be used in tests.

Project level

You can set up the a11y matchers once at the project level to make it available to all the Jest tests in the project.
For an example look at the Integration tests.

  • Add a Jest setup file (e.g. jest-setup.js) and add the following code that registers the a11y matchers
const { registerA11yMatchers } = require('@sa11y/jest');
registerA11yMatchers();
  • Add/Modify Jest config at project root to invoke the Jest setup file as setup above.
    In the jest.config.js at the root of your project, add:
module.exports = {
    setupFilesAfterEnv: ['<rootDir>/jest-setup.js'],
};

Test module level

Invoke registerA11yMatchers before using the accessibility matchers in the tests e.g.

import { registerA11yMatchers } from '@sa11y/jest';

beforeAll(() => {
    registerA11yMatchers();
});

Usage

import { recommended } from '@sa11y/preset-rules';
import { registerA11yMatchers } from '@sa11y/jest';

beforeAll(() => {
    registerA11yMatchers();
});

it('should be accessible', async () => {
    // Setup DOM to be tested for accessibility
    //...

    // assert that DOM is accessible (using extended preset-rule)
    await expect(document).toBeAccessible();

    // use recommended preset-rule
    await expect(document).toBeAccessible(recommended);
});

Mohan Raj Rajamanickam added 18 commits April 8, 2020 17:29
as --fix option wasn't being passed to the eslint cmd
modify root jest dep version to match
and add links to existing docs in root readme
to prevent failed commit process due to failed checks in staged files
@github-actions
Copy link

Coverage after merging feat/add_jest_integration into master

100.00%

Coverage Report
FileBranchesFuncsLinesUncovered Lines
packages/assert/src
   assert.ts100%100%100%
packages/format/src
   format.ts100%100%100%
packages/jest/src
   jest.ts100%100%100%
packages/preset-rules/src
   a11yConfig.ts100%100%100%
   extended.ts100%100%100%
   index.ts100%100%100%
   recommended.ts100%100%100%
packages/test-utils/src
   index.ts100%100%100%
   jest-setup.ts100%100%100%
   test-data.ts100%100%100%
   utils.ts100%100%100%

Mohan Raj Rajamanickam added 5 commits April 28, 2020 17:30
Fixes Error when using "await" with "toBeAccessible": "Unexpected await of a non-Promise
(non-"Thenable") value"
that supports typescript among other things
to enable markdown eslint plugin to check them
@github-actions
Copy link

Coverage after merging feat/add_jest_integration into master

100.00%

Coverage Report
FileBranchesFuncsLinesUncovered Lines
packages/assert/src
   assert.ts100%100%100%
packages/format/src
   format.ts100%100%100%
packages/jest/src
   jest.ts100%100%100%
packages/preset-rules/src
   a11yConfig.ts100%100%100%
   extended.ts100%100%100%
   index.ts100%100%100%
   recommended.ts100%100%100%
packages/test-utils/src
   index.ts100%100%100%
   jest-setup.ts100%100%100%
   test-data.ts100%100%100%
   utils.ts100%100%100%

@github-actions
Copy link

Coverage after merging feat/add_jest_integration into master

100.00%

Coverage Report
FileBranchesFuncsLinesUncovered Lines
packages/assert/src
   assert.ts100%100%100%
packages/format/src
   format.ts100%100%100%
packages/jest/src
   jest.ts100%100%100%
packages/preset-rules/src
   a11yConfig.ts100%100%100%
   extended.ts100%100%100%
   index.ts100%100%100%
   recommended.ts100%100%100%
packages/test-utils/src
   index.ts100%100%100%
   jest-setup.ts100%100%100%
   test-data.ts100%100%100%
   utils.ts100%100%100%

Add integration test package to test setup of a11y matcher
Copy link
Contributor Author

@mohanraj-r mohanraj-r left a comment

Choose a reason for hiding this comment

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

Thanks for the review @trevor-bliss
I have taken care of your comments. Can you review please.

* @param config - A11yConfig to be used to test for accessibility. Defaults to extended.
*/
export async function toBeAccessible(
receivedDom: Document = document,
Copy link
Contributor Author

Choose a reason for hiding this comment

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

axe supports element references, node lists and css selectors - defaulting to the entire DOM.
Which of those would make sense? Or could we just make the first parameter a passthrough?
https://github.com/dequelabs/axe-core/blob/develop/doc/API.md#context-parameter

receivedDom: Document = document,
config: A11yConfig
): Promise<jest.CustomMatcherResult> {
return toBeAccessible(receivedDom, config);
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah was wondering the same after adding it. The marginal readability achieved with the With(Config) matcher is probably not worth the overhead. Removed.

import { A11yConfig } from '@sa11y/preset-rules';
import { Formatter, a11yResultsFormatter } from '@sa11y/format';
import * as axe from 'axe-core';
import { ElementContext, RunOptions } from 'axe-core';
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Agree - felt weird for me too, fixed.

*/

export { toBeAccessible, toBeAccessibleWith } from './matcher';
export { adaptA11yConfig, registerA11yMatchers } from './setup';
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Right now the tests are using it - so it needs to be exported I guess?
But I have been thinking about its usefulness to consumers as well.. Right now with its limited scope, its probably not useful. But if we allowed consumers to register their own adapters that could be useful. But there isn't a clear use case for such a workflow right now.

In the `jest.config.js` at the root of your project, add

```javascript
const { jestConfig } = require('@sa11y/jest');
Copy link
Contributor Author

Choose a reason for hiding this comment

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

@trevor-bliss In 2b18c4b I am trying to automate setup of the a11y matchers so that they can be setup once in a project instead of every test module. But I am not able to get it to work. Can you please take a look at see what I am missing.

Choose a reason for hiding this comment

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

I would simplify the jest config in the integration test package and work backwards from a working solution.

In packages/test-integ/jest.config.js, can you have the setupFilesAfterEnv option directly that points to a local file that calls expect.extend to add a trivial custom matcher?

Once you have that then start using the imports/exports until you get back to where you wanted to be.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good suggestion, let me try that out - thanks.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@trevor-bliss Ran into some issues trying the config jest
I have opened an issue with info #11
If there are no other issues with this PR can we merge this while I continue working on the jest config? I can revert 2b18c4b from this PR and add it to another branch?

Choose a reason for hiding this comment

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

Yeah works for me. Approved the PR.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks @trevor-bliss
Modified jest setup as per your suggestions in #11
Can you please review

@github-actions
Copy link

github-actions bot commented May 4, 2020

Coverage Report 🎉

Totals Coverage
Statements: 100% ( 44 / 44 )
Methods: 100% ( 15 / 15 )

@spelak-salesforce
Copy link

@mohanraj-r , I'm working on testing the work in this Pull Request in existing jest tests from another repo, but I'm having a difficult time referencing @sa11y/jest and the other modules locally. I'll try again and let you know if I can test this Pull Request out.

@mohanraj-r
Copy link
Contributor Author

@mohanraj-r , I'm working on testing the work in this Pull Request in existing jest tests from another repo, but I'm having a difficult time referencing @sa11y/jest and the other modules locally. I'll try again and let you know if I can test this Pull Request out.

Thanks a lot @spelak-salesforce
I was able to install it from my local git dir using the following commands

cd /tmp
git clone --single-branch --branch feat/add_jest_integration git@github.com:salesforce/sa11y.git
cd /tmp/sa11y && yarn install && yarn build
cd <back to project dir where we want to install sa11y jest>
yarn add /tmp/sa11y/packages/jest/

Please let me know if that works for you.

trevor-bliss
trevor-bliss previously approved these changes May 7, 2020
Copy link

@trevor-bliss trevor-bliss left a comment

Choose a reason for hiding this comment

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

:shipit:

@mohanraj-r
Copy link
Contributor Author

@cordeliadillon I refactored the docs since your last review. Can you please look a quick look at them to make sure they make sense.

Fix incorrect integration test, add doc about async usage
Copy link

@trevor-bliss trevor-bliss left a comment

Choose a reason for hiding this comment

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

🚢

@mohanraj-r mohanraj-r merged commit fd586d1 into master May 13, 2020
@mohanraj-r mohanraj-r deleted the feat/add_jest_integration branch May 13, 2020 02:08
@mohanraj-r
Copy link
Contributor Author

Fixes #11 #8

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.

None yet

4 participants