Skip to content

Commit 730300e

Browse files
authored
Add toPassAxeTests feature (#494)
1 parent 6a05002 commit 730300e

12 files changed

Lines changed: 536 additions & 15 deletions

File tree

.changeset/early-pigs-beg.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
---
2+
'pleasantest': minor
3+
---
4+
5+
New assertion: `expect(page).toPassAxeTests()`
6+
7+
This assertion is based on the [`jest-puppeteer-axe`](https://github.com/WordPress/gutenberg/tree/3b2eccc289cfc90bd99252b12fc4c6e470ce4c04/packages/jest-puppeteer-axe) package. (That package already works with Pleasantest, our new feature just formats error messages a little differently)
8+
9+
It allows you to pass a page to be checked with the [axe accessibility linter](https://github.com/dequelabs/axe-core).
10+
11+
```js
12+
test(
13+
'Axe tests',
14+
withBrowser(async ({ utils, page }) => {
15+
await utils.injectHTML(`
16+
<h1>Some html</h1>
17+
`);
18+
19+
await expect(page).toPassAxeTests();
20+
}),
21+
);
22+
```

README.md

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ Pleasantest is a library that allows you test web applications using real browse
3535
- [Utilities API: `PleasantestUtils`](#utilities-api-pleasantestutils)
3636
- [`jest-dom` Matchers](#jest-dom-matchers)
3737
- [`getAccessibilityTree`](#getaccessibilitytreeelement-elementhandle--page-options-accessibilitytreeoptions--promiseaccessibilitytreesnapshot)
38+
- [`toPassAxeTests`](#expectpagetopassaxetestsopts-topassaxetestsopts)
3839
- [Puppeteer Tips](#puppeteer-tips)
3940
- [Comparisons with other testing tools](#comparisons-with-other-testing-tools)
4041
- [Limitations](#limitationsarchitectural-decisions)
@@ -830,6 +831,33 @@ Disabling these options can be used to reduce the output or to exclude text that
830831

831832
The returned `Promise` wraps an `AccessibilityTreeSnapshot`, which can be passed directly as the `expect` first parameter in `expect(___).toMatchInlineSnapshot()`. The returned object can also be converted to a string using `String(accessibilityTreeSnapshot)`.
832833

834+
### `expect(page).toPassAxeTests(opts?: ToPassAxeTestsOpts)`
835+
836+
This assertion, based on [`jest-puppeteer-axe`](https://github.com/WordPress/gutenberg/tree/3b2eccc289cfc90bd99252b12fc4c6e470ce4c04/packages/jest-puppeteer-axe), allows you to check a page using the [axe accessibility linter](https://github.com/dequelabs/axe-core).
837+
838+
To use this assertion, you **must install `@axe-core/puppeteer` and `axe-core`**. They are optional peer dependencies for Pleasantest, but are needed for the `toPassAxeTests` assertion.
839+
840+
```js
841+
test(
842+
'Axe tests',
843+
withBrowser(async ({ utils, page }) => {
844+
await utils.injectHTML(`
845+
<h1>Some html</h1>
846+
`);
847+
848+
await expect(page).toPassAxeTests();
849+
}),
850+
);
851+
```
852+
853+
`ToPassAxeTestsOpts` (all properties are optional):
854+
855+
- `include`: `string | string[]`: CSS selector(s) to add to the list of elements to include in analysis.
856+
- `exclude`: `string | string[]`: CSS selector(s) to add to the list of elements to exclude from analysis.
857+
- `disabledRules`: `string | string[]`: The list of [Axe rules](https://github.com/dequelabs/axe-core/tree/v4.4.2/lib/rules) to skip from verification.
858+
- `options`: [`axe.RunOptions`](https://github.com/dequelabs/axe-core/blob/v4.4.2/axe.d.ts#L89): A flexible way to [configure how Axe run operates](https://github.com/dequelabs/axe-core/blob/HEAD/doc/API.md#options-parameter).
859+
- `config`: [`axe.Spec`](https://github.com/dequelabs/axe-core/blob/v4.4.2/axe.d.ts#L195): [Axe configuration object](https://github.com/dequelabs/axe-core/blob/HEAD/doc/API.md#api-name-axeconfigure).
860+
833861
## Puppeteer Tips
834862

835863
Pleasantest uses [Puppeteer](https://github.com/puppeteer/puppeteer) under the hood. You don't need to know how to use Puppeteer in order to use Pleasantest, but a little bit of Puppeteer knowledge might come in handy. Here are the parts of Puppeteer that are most helpful and relevant for Pleasantest:

package-lock.json

Lines changed: 53 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,21 @@
88
"dist"
99
],
1010
"license": "MIT",
11+
"peerDependencies": {
12+
"@axe-core/puppeteer": "^4.4.2",
13+
"axe-core": "^4.4.2"
14+
},
15+
"peerDependenciesMeta": {
16+
"@axe-core/puppeteer": {
17+
"optional": true
18+
},
19+
"axe-core": {
20+
"optional": true
21+
}
22+
},
1123
"devDependencies": {
1224
"@ampproject/remapping": "2.2.0",
25+
"@axe-core/puppeteer": "4.4.2",
1326
"@babel/core": "7.16.7",
1427
"@babel/preset-env": "7.16.8",
1528
"@babel/preset-typescript": "7.16.7",
@@ -28,6 +41,7 @@
2841
"@vue/compiler-sfc": "3.2.36",
2942
"ansi-regex": "6.0.1",
3043
"aria-query": "*",
44+
"axe-core": "4.4.2",
3145
"babel-plugin-un-cjs": "2.5.0",
3246
"dom-accessibility-api": "0.5.14",
3347
"errorstacks": "2.4.0",

rollup.config.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ const external = [
1919
'esbuild',
2020
/postcss/,
2121
/mime/,
22+
'@axe-core/puppeteer',
2223
];
2324

2425
/** @type {import('rollup').RollupOptions} */
@@ -55,7 +56,7 @@ const mainConfig = {
5556
const typesConfig = {
5657
input: 'src/index.ts',
5758
output: [{ file: 'dist/index.d.ts', format: 'es' }],
58-
external: [...external, 'polka'],
59+
external: [...external, 'polka', 'axe-core'],
5960
plugins: [dts({ respectExternal: true })],
6061
};
6162

src/accessibility/browser.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,16 @@ import {
66
// @ts-expect-error This is a fake file that triggers a rollup plugin
77
import requiredOwnedElementsMap from 'generated:requiredOwnedElements';
88
import type { AccessibilityTreeOptions } from '.';
9+
import * as colors from 'kolorist';
10+
11+
export * as colors from 'kolorist';
12+
export { printElement } from '../serialize';
13+
14+
// We haver to tell kolorist to print the colors
15+
// because by default it won't since we are in the browser
16+
// (the colored message gets sent to node to be printed)
17+
colors.options.enabled = true;
18+
colors.options.supportLevel = 1;
919

1020
const indent = (text: string, indenter = ' ') =>
1121
indenter + text.split('\n').join(`\n${indenter}`);

0 commit comments

Comments
 (0)