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 custom results processor for a11y failures #62

Merged
merged 78 commits into from
Jun 22, 2021
Merged
Changes from 1 commit
Commits
Show all changes
78 commits
Select commit Hold shift + click to select a range
77b7277
refactor: use map to consolidate results; add a util to convert axe r…
mohanraj-r May 12, 2021
5d17571
feat: add minimal custom jest reporter
mohanraj-r May 13, 2021
76aa29f
refactor: move custom jest reporter to typescript inside jest pkg
mohanraj-r May 14, 2021
a6d04be
build: move jest reporter pkg dependency from root to jest pkg
mohanraj-r May 14, 2021
443b4e1
feat: access and store a11y violations from reporter
mohanraj-r May 17, 2021
228b384
refactor: revert to using default test runner with on run complete hook
mohanraj-r May 18, 2021
483cdf7
build: remove obsolete jest circus pkg, add jest test-result pkg
mohanraj-r May 19, 2021
aed493c
refactor: revert conditional gates to reduce indented blocks
mohanraj-r May 21, 2021
43f1f5c
refactor: serialize a11y error into failure message and parse it back
mohanraj-r May 21, 2021
ca71b30
refactor: revert serialize a11y error into failure msg
mohanraj-r May 25, 2021
84c8546
test(eslint): add ignore pattern for unused vars rule
mohanraj-r May 25, 2021
e183191
refactor: consolidate a11y results from reporter keyed on test path
mohanraj-r May 26, 2021
be9661f
refactor: replace hardcoded output file path in reporter with config
mohanraj-r May 26, 2021
8cf387f
build: add xml pkg dependency
mohanraj-r May 27, 2021
7f0bbda
feat: add xml output support for reporter
mohanraj-r May 27, 2021
dba9a81
feat: add a WIP test results processor that currently filters failed …
mohanraj-r May 28, 2021
fc68b02
feat: create a new test suite for a11y errors duplicated from existin…
mohanraj-r May 28, 2021
b0f480d
refactor: remove err msg from original suite for now
mohanraj-r May 28, 2021
0695605
refactor: extract test result processing into its own function
mohanraj-r May 29, 2021
57fa2be
feat: transform a11y error details into suite, test names
mohanraj-r May 29, 2021
097b2a1
test: add data and first set of basic tests for test results processor
mohanraj-r Jun 1, 2021
0457cd2
refactor: split up results processing logic into smaller functions
mohanraj-r Jun 1, 2021
97b8cf0
refactor: update failure msgs format
mohanraj-r Jun 1, 2021
6f19075
feat: add wcag metadata to results output
mohanraj-r Jun 1, 2021
54bf098
refactor: move wcag metadata processing into format pkg, add tests
mohanraj-r Jun 1, 2021
dfd4cfb
refactor: remove sc prefix for best practice
mohanraj-r Jun 1, 2021
5179445
build: add jest test result pkg dep to sa11y pkgs
mohanraj-r Jun 1, 2021
dd0a65f
refactor: remove redundant err msg header in a11y error class
mohanraj-r Jun 3, 2021
fcaaa3b
refactor: convert axe violations into a11y results for reuse
mohanraj-r Jun 3, 2021
1e07f4d
refactor: move sorting violations from a11y error into a11y results
mohanraj-r Jun 3, 2021
878e9d5
test: rename result processor test file name to align with module tested
mohanraj-r Jun 3, 2021
6728614
build: update ES lib/target to ES 2019 based on TS recommendation
mohanraj-r Jun 4, 2021
bcbb2db
test: add tests to generate data for testing results processor
mohanraj-r Jun 4, 2021
4e0ed0b
test: update result processor snapshots
mohanraj-r Jun 4, 2021
8be300c
refactor: reuse a11y results in results processor
mohanraj-r Jun 4, 2021
6686a0c
refactor: reorganize result processor code into smaller functions
mohanraj-r Jun 5, 2021
b9ba221
refactor: replace wcag obj ref with formatted string in result
mohanraj-r Jun 7, 2021
15b0d4b
test: replace hard-coded test results data with generated data
mohanraj-r Jun 8, 2021
067a4d6
refactor: add a11 failure summary, rearrange output order, update tes…
mohanraj-r Jun 8, 2021
817985b
refactor: change formatting of wcag tags, remove redundant format method
mohanraj-r Jun 9, 2021
f224b55
refactor: move suite name to class name and rule name to test name
mohanraj-r Jun 9, 2021
ce51ed0
refactor: narrow scope of function to convert a11y failure to a test …
mohanraj-r Jun 9, 2021
be1de3d
refactor: consolidate a11y results within a suite
mohanraj-r Jun 9, 2021
52b7e05
test: add more data for result processor tests
mohanraj-r Jun 10, 2021
0e05588
refactor: remove obsolete jest custom reporter
mohanraj-r Jun 10, 2021
0a60d6f
refactor: fix code cov for format opts
mohanraj-r Jun 10, 2021
fe47b54
test: add tests for a11y results consolidation
mohanraj-r Jun 10, 2021
c6ef300
docs: update dependency graph
mohanraj-r Jun 10, 2021
8838979
refactor: add format opts to de-duplicate
mohanraj-r Jun 10, 2021
b2482e0
refactor(reporter): rearrange order of output info, sort wcag tags
mohanraj-r Jun 14, 2021
71eea8a
build: add results processor to pkg export, add console log for resul…
mohanraj-r Jun 15, 2021
cce6621
refactor: capitalize a11y error msg header
mohanraj-r Jun 15, 2021
5a4fc3f
refactor: add wrapper for console log with common prefix
mohanraj-r Jun 15, 2021
124256f
docs: revise automatic check docs and add info about custom reporter
mohanraj-r Jun 15, 2021
73004ac
chore: update dev deps
mohanraj-r Jun 15, 2021
a8f2d0d
fix(format): revert default value of formatter to fix codecov
mohanraj-r Jun 15, 2021
a24ed27
chore(release): bump pkg versions, update changelog
mohanraj-r Jun 15, 2021
174c47f
docs: add section about updating deps
mohanraj-r Jun 15, 2021
467cb5b
ci: update circle ci orb versions
mohanraj-r Jun 15, 2021
b4bfda6
build: update jest to v27
mohanraj-r Jun 15, 2021
fc355d5
revert "build: update jest to v27" due to incorrect codecov data
mohanraj-r Jun 15, 2021
b29acf5
ci: add run in band option to jest cli for stable codecov output with…
mohanraj-r Jun 16, 2021
9ef8ac5
test: update utils test to increase code cov
mohanraj-r Jun 16, 2021
131eb9f
test(utils): expand tests to fix missing code cov
mohanraj-r Jun 16, 2021
34dd7e4
test(utils): fix missing code cov on conditional branch
mohanraj-r Jun 16, 2021
7470dab
test: exclude statement from code cov that is causing issue in CI
mohanraj-r Jun 17, 2021
aedf3c1
test: add code cov exceptions to edge cases
mohanraj-r Jun 17, 2021
15decaa
test(jest): add script cmd to invoke automatic checks and output json…
mohanraj-r Jun 17, 2021
fcc2646
refactor(resultsProcessor): rename functions and improve docs for rea…
mohanraj-r Jun 17, 2021
88c30a6
refactor: add a debug flag to enable console log msg and silence by d…
mohanraj-r Jun 21, 2021
acb0ab1
docs(jest): add limitations of automatic checks and json result trans…
mohanraj-r Jun 21, 2021
7717c3d
refactor: remove duplicated consolidation logic based on axe results
mohanraj-r Jun 22, 2021
6582930
refactor: unify consolidation based on a11y results
mohanraj-r Jun 22, 2021
39c80ef
refactor: rename a11y results consolidate function to add for brevity
mohanraj-r Jun 22, 2021
b424e86
test: fix visual a11y issues count for wdio test
mohanraj-r Jun 22, 2021
6e0fc7e
refactor: create a convenience A11yResults class and remove Consolida…
mohanraj-r Jun 22, 2021
3052554
docs(jest): reword limitations of automatic checks for readability
mohanraj-r Jun 22, 2021
d24cea3
docs(jest): fix indentation in json results example
mohanraj-r Jun 22, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
93 changes: 87 additions & 6 deletions packages/jest/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ Accessibility matcher for [Jest](https://jestjs.io)
- [Test module level](#test-module-level)
- [Automatic checks](#automatic-checks)
- [Using environment variables](#using-environment-variables)
- [Workflow](#workflow)
- [Sa11y results processor](#sa11y-results-processor)
- [JSON result transformation](#json-result-transformation)
- [Limitations](#limitations)
- [Caution](#caution)
- [Usage](#usage)

Expand Down Expand Up @@ -92,6 +94,8 @@ beforeAll(() => {

The sa11y API can be setup to be automatically invoked at the end of each test as an alternative to adding the `toBeAccessible` API at the end of each test.

- When automatic checks are enabled each child element in the DOM body will be checked for a11y and failures reported as part of the test.

```javascript
setup({ autoCheckOpts: { runAfterEach: true } });

Expand All @@ -110,18 +114,95 @@ SA11Y_AUTO=1 SA11Y_CLEANUP=1 jest
- Invoking `jest` with environment variables as above will enable automatic checks with no changes required to `setup()`
- The environment variables can be used to set up parallel builds e.g., in a CI environment without code changes to `setup()` to opt-in to automatic checks

#### Workflow
#### Sa11y results processor

When automatic checks are enabled
The sa11y custom test results processor can be enabled using e.g., - `jest --json --outputFile results.json --testResultsProcessor node_modules/@sa11y/jest/dist/resultsProcessor.js`

- Each child element in the DOM body will be checked for a11y, results consolidated and failures reported as part of the test
- sa11y results processor affects only the JSON result output
- It does not affect the default console reporter or output of any other reporter (e.g., HTML reporter)
- a11y errors within a single test file will be de-duped by rule ID and CSS selectors
- a11y errors can be transformed into their own test failures using the sa11y custom test result processor
- `jest --json --outputFile results.json --testResultsProcessor node_modules/@sa11y/jest/dist/resultsProcessor.js`
- a11y errors will be transformed into their own test failures
- This would extract the a11y errors from the original tests and create additional test failures with the WCAG version, level, rule ID, CSS selectors as key
- bringing a11y metadata to forefront instead of being part of stacktrace
- The JSON output can be transformed into JUnit XML format e.g., using [jest-junit](https://github.com/jest-community/jest-junit)

##### JSON result transformation

With default results processor - a11y error is embedded within the test failure:

```json
"assertionResults": [
{
"ancestorTitles": [
"integration test @sa11y/jest"
],
"failureMessages": [
"A11yError: 1 Accessibility issues found\n * (link-name) Links must have discernible text: a\n\t- Help URL: https://dequeuniversity.com/rules/axe/4.1/link-name\n at Function.checkAndThrow (packages/format/src/format.ts:67:19)\n at automaticCheck (packages/jest/src/automatic.ts:54:19)\n at Object.<anonymous> (packages/jest/src/automatic.ts:69:13)"
],
"fullName": "integration test @sa11y/jest should throw error for inaccessible dom",
"location": null,
"status": "failed",
"title": "should throw error for inaccessible dom"
}
]
```

With sa11y results processor:

- Original JSON test result (failure with embedded a11y error) is disabled

```json
"assertionResults": [
{
"ancestorTitles": [
"integration test @sa11y/jest"
],
"failureMessages": [
"A11yError: 1 Accessibility issues found\n * (link-name) Links must have discernible text: a\n\t- Help URL: https://dequeuniversity.com/rules/axe/4.1/link-name\n at Function.checkAndThrow (packages/format/src/format.ts:67:19)\n at automaticCheck (packages/jest/src/automatic.ts:54:19)\n at Object.<anonymous> (packages/jest/src/automatic.ts:69:13)"
],
"fullName": "integration test @sa11y/jest should throw error for inaccessible dom",
"location": null,
"status": "disabled",
"title": "should throw error for inaccessible dom"
},
]
```

- Each unique a11y failure in a test module is extracted as a new test failure and added to a new test suite using a11y metadata as key. This could result in increase of total test count and suite count in the results JSON.

```json
"assertionResults": [
{
"ancestorTitles": [
"integration test @sa11y/jest",
"integration test @sa11y/jest should throw error for inaccessible dom"
],
"failureMessages": [
"Accessibility issues found: Links must have discernible text\nCSS Selectors: a\nHTML element: <a href=\"#\"></a>\nHelp: https://dequeuniversity.com/rules/axe/4.1/link-name\nTests: \"integration test @sa11y/jest should throw error for inaccessible dom\"\nSummary: Fix all of the following:\n Element is in tab order and does not have accessible text\n\nFix any of the following:\n Element does not have text that is visible to screen readers\n aria-label attribute does not exist or is empty\n aria-labelledby attribute does not exist, references elements that do not exist or references elements that are empty\n Element has no title attribute"
],
"fullName": "Links must have discernible text: a",
"location": null,
"status": "failed",
"title": "should throw error for inaccessible dom"
}
],
```

#### Limitations

Automatic checks currently has the following limitations.

- Automatic check is triggered regardless of the test status which would result in the original test failure if any getting overwritten by a11y failures if any from automatic checks ([#66](https://github.com/salesforce/sa11y/issues/66))
- Tests using the sa11y jest api would get tested twice with automatic checks - once as part of the sa11y API in the test and again as part of the automatic check
- a11y issues from automatic checks would overwrite the a11y issues found by the API
- If the sa11y API has been added to the test to check specific intermediate states of the DOM, enabling automatic checks could result in missed a11y issues

Choose a reason for hiding this comment

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

I think this point is also covered by the next one below.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes it is related, but there is also a fine difference - Enabling automatic checks without using the API vs while also using the API. Please check if the rewording now makes sense.

Choose a reason for hiding this comment

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

I guess to me since you say the automatic check is only at the end of the test it was clear to me that checking intermediate states of the DOM, whether you called the API or not, would not be caught. I'm fine to keep it worded like this though if you think it's more clear.

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. I will try to revisit and reword/remove it in a subsequent PR.

- Automatic checks check the DOM state as it is at the end of the test. DOM states before the end of the test are not checked which could result in missed a11y issues.
- If the test cleans up the DOM after execution, as part of teardown e.g., the sa11y automatic check executed at the end of the test would not be able to check the DOM
- Workaround: Remove the DOM cleanup code from the test and opt-in to using sa11y to clean-up the DOM using the options as described above (`cleanupAfterEach: true` or `SA11Y_CLEANUP=1`)
- With the sa11y results processor
- Though the originating test from which the a11y failures are extracted is disabled, and test counts adjusted accordingly - the original test suite failure message still contains the a11y failures.
- The test suite failure message is typically not displayed or used in testing workflows. But if your testing workflow uses the test suite failure message, this might cause confusion.

Choose a reason for hiding this comment

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

The double indentation here feels a little confusing to me. Can we combine these 3 bullets into a single bullet and a couple sentences maybe?

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, modified - please take a look.


## Caution

- **async**: `toBeAccessible` **must** be invoked with `async/wait` or `Promise` or the equivalent supported asynchronous method in your environment
Expand Down