vi-axe 0.0.1-pre-alpha → 0.0.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -1,329 +1,133 @@
1
- # vi-axe [A modern Fork / Rewrite of jest-axe]
1
+ # vi-axe
2
2
 
3
- [![npm version](https://img.shields.io/npm/v/vi-axe.svg)](http://npm.im/vi-axe)
4
- ![node](https://img.shields.io/node/v/vi-axe)
5
-
6
- # DO NOT USE THIS AS IT IS NOW.
7
-
8
- Custom [Vi][Vi] matcher for [axe](https://github.com/dequelabs/axe-core) for testing accessibility
9
-
10
- ## ⚠️✋ This project does not guarantee that what you build is accessible.
11
-
12
- The GDS Accessibility team found that around [~30% of access barriers are missed by automated testing](https://accessibility.blog.gov.uk/2017/02/24/what-we-found-when-we-tested-tools-on-the-worlds-least-accessible-webpage).
3
+ ### A modern fork / rewrite of jest-axe for vitest.
13
4
 
14
- Tools like axe are similar to [code linters](https://en.wikipedia.org/wiki/Lint_%28software%29) such as [eslint](https://eslint.org/) or [stylelint](https://stylelint.io/): they can find common issues but cannot guarantee that what you build works for users.
5
+ I'm aware that vitest-axe also exists, however it seems to be unmaintained.
15
6
 
16
- You'll also need to:
17
-
18
- - test your interface with the [assistive technologies that real users use](https://www.gov.uk/service-manual/technology/testing-with-assistive-technologies#when-to-test) (see also [WebAIM's survey results](https://webaim.org/projects/screenreadersurvey8/#primary)).
19
- - include disabled people in user research.
20
-
21
- ### Checks that do not work in jest-axe
7
+ [![npm version](https://img.shields.io/npm/v/vi-axe.svg)](http://npm.im/vi-axe)
8
+ ![node](https://img.shields.io/node/v/vi-axe)
22
9
 
23
- Color contrast checks do not work in JSDOM so are turned off in jest-axe.
10
+ Custom Vitest matcher for [aXe](https://github.com/dequelabs/axe-core) to test accessibility in your components and pages.
24
11
 
25
- ## Installation:
12
+ ## Installation
26
13
 
27
14
  ```bash
28
- npm install --save-dev jest jest-axe jest-environment-jsdom
29
- ```
30
-
31
- [TypeScript](https://www.typescriptlang.org/) users can install the community maintained types package:
32
-
33
- ```bash
34
- npm install --save-dev @types/jest-axe
35
- ```
36
-
37
- ## Usage:
38
-
39
- ```javascript
40
- /**
41
- * @jest-environment jsdom
42
- */
43
- const { axe, toHaveNoViolations } = require("jest-axe");
44
-
45
- expect.extend(toHaveNoViolations);
46
-
47
- it("should demonstrate this matcher`s usage", async () => {
48
- const render = () => '<img src="#"/>';
49
-
50
- // pass anything that outputs html to axe
51
- const html = render();
52
-
53
- expect(await axe(html)).toHaveNoViolations();
54
- });
55
- ```
56
-
57
- ![Screenshot of the resulting output from the usage example](example-cli.png)
58
-
59
- > Note, you can also require `'jest-axe/extend-expect'` which will call `expect.extend` for you.
60
- > This is especially helpful when using the jest `setupFilesAfterEnv` configuration.
61
-
62
- ### Testing React
63
-
64
- ```javascript
65
- const React = require("react");
66
- const { render } = require("react-dom");
67
- const App = require("./app");
68
-
69
- const { axe, toHaveNoViolations } = require("jest-axe");
70
- expect.extend(toHaveNoViolations);
71
-
72
- it("should demonstrate this matcher`s usage with react", async () => {
73
- render(<App />, document.body);
74
- const results = await axe(document.body);
75
- expect(results).toHaveNoViolations();
76
- });
15
+ pnpm add -D vi-axe
16
+ # or
17
+ npm install -D vi-axe
18
+ # or
19
+ yarn add -D vi-axe
77
20
  ```
78
21
 
79
- ### Testing React with [React Testing Library](https://testing-library.com/docs/react-testing-library/intro)
80
-
81
- ```javascript
82
- const React = require("react");
83
- const App = require("./app");
22
+ ## Setup
84
23
 
85
- const { render } = require("@testing-library/react");
86
- const { axe, toHaveNoViolations } = require("jest-axe");
87
- expect.extend(toHaveNoViolations);
24
+ Configure Vitest so the `toHaveNoViolations` matcher is available in all tests. In your Vitest config (e.g. `vitest.config.ts`):
88
25
 
89
- it("should demonstrate this matcher`s usage with react testing library", async () => {
90
- const { container } = render(<App />);
91
- const results = await axe(container);
26
+ ```ts
27
+ import { defineConfig } from "vitest/config";
92
28
 
93
- expect(results).toHaveNoViolations();
29
+ export default defineConfig({
30
+ test: {
31
+ environment: "jsdom", // required for axe
32
+ setupFiles: ["vi-axe/extend-expect"],
33
+ },
94
34
  });
95
35
  ```
96
36
 
97
- > Note: If you're using `react testing library` <9.0.0 you should be using the
98
- > [`cleanup`](https://testing-library.com/docs/react-testing-library/api#cleanup) method. This method removes the rendered application from the DOM and ensures a clean HTML Document for further testing.
37
+ Alternatively, in a single test file:
99
38
 
100
- If you're using [React Portals](https://reactjs.org/docs/portals.html), use the [`baseElement`](https://testing-library.com/docs/react-testing-library/api#baseelement) instead of `container`:
101
-
102
- ```js
103
- it("should work with React Portals as well", async () => {
104
- const { baseElement } = render(<App />);
105
- const results = await axe(baseElement);
106
-
107
- expect(results).toHaveNoViolations();
108
- });
39
+ ```ts
40
+ import "vi-axe/extend-expect";
109
41
  ```
110
42
 
111
- ### Testing Vue with [Vue Test Utils](https://vue-test-utils.vuejs.org/)
43
+ ## Usage
112
44
 
113
- ```javascript
114
- const App = require("./App.vue");
45
+ ### Basic
115
46
 
116
- const { mount } = require("@vue/test-utils");
117
- const { axe, toHaveNoViolations } = require("jest-axe");
118
- expect.extend(toHaveNoViolations);
47
+ Run axe on an HTML string or DOM element, then assert there are no violations:
119
48
 
120
- it("should demonstrate this matcher`s usage with vue test utils", async () => {
121
- const wrapper = mount(Image);
122
- const results = await axe(wrapper.element);
49
+ ```ts
50
+ import { axe } from "vi-axe";
123
51
 
52
+ test("has no a11y violations", async () => {
53
+ const html = `<main><a href="https://example.com">Example</a></main>`;
54
+ const results = await axe(html);
124
55
  expect(results).toHaveNoViolations();
125
56
  });
126
57
  ```
127
58
 
128
- ### Testing Vue with [Vue Testing Library](https://testing-library.com/docs/vue-testing-library/intro)
59
+ ### With React Testing Library
129
60
 
130
- ```javascript
131
- const App = require("./app");
61
+ Pass the rendered container (or any element) to `axe`:
132
62
 
133
- const { render } = require("@testing-library/vue");
134
- const { axe, toHaveNoViolations } = require("jest-axe");
135
- expect.extend(toHaveNoViolations);
63
+ ```ts
64
+ import { render } from "@testing-library/react";
65
+ import { axe } from "vi-axe";
136
66
 
137
- it("should demonstrate this matcher`s usage with react testing library", async () => {
138
- const { container } = render(<App />);
67
+ test("component has no a11y violations", async () => {
68
+ const { container } = render(<MyComponent />);
139
69
  const results = await axe(container);
140
-
141
70
  expect(results).toHaveNoViolations();
142
71
  });
143
72
  ```
144
73
 
145
- > Note: If you're using `vue testing library` <3.0.0 you should be using the
146
- > [`cleanup`](https://testing-library.com/docs/vue-testing-library/api#cleanup) method. This method removes the rendered application from the DOM and ensures a clean HTML Document for further testing.
74
+ ### With Vue Test Utils
147
75
 
148
- ### Testing Angular with [Nx](https://nx.dev/)
76
+ Pass the wrapper element:
149
77
 
150
- ```typescript
151
- import { ComponentFixture, TestBed } from "@angular/core/testing";
152
- import { axe } from "jest-axe";
153
-
154
- import { SomeComponent } from "./some.component";
155
-
156
- describe("SomeComponent", () => {
157
- let fixture: ComponentFixture<SomeComponent>;
158
-
159
- beforeEach(() => {
160
- TestBed.configureTestingModule({
161
- declarations: [SomeComponent],
162
- });
163
-
164
- fixture = TestBed.createComponent(SomeComponent);
165
- });
166
-
167
- it("should create", async () => {
168
- const results = await axe(fixture.nativeElement);
169
- expect(results).toHaveNoViolations();
170
- });
171
- });
172
- ```
173
-
174
- > Note: You may need to extend jest by importing `jest-axe/extend-expect` at `test-setup.ts`
175
-
176
- ### Usage with jest.useFakeTimers() or mocking setTimeout
177
-
178
- > thrown: "Exceeded timeout of 5000 ms for a test.
179
- > Use jest.setTimeout(newTimeout) to increase the timeout value, if this is a long-running test."
180
-
181
- aXe core does not work when timers (setTimeout) are mocked. When using `jest.useFakeTimers()` aXe core will timeout often causing failing tests.
182
-
183
- We recommend renabling the timers temporarily for aXe:
184
-
185
- ```javascript
186
- jest.useRealTimers();
187
- const results = await axe(wrapper.element);
188
- jest.useFakeTimers();
189
- ```
190
-
191
- ### Axe configuration
192
-
193
- The `axe` function allows options to be set with the [same options as documented in axe-core](https://github.com/dequelabs/axe-core/blob/master/doc/API.md#options-parameter):
194
-
195
- ```javascript
196
- const { axe, toHaveNoViolations } = require("jest-axe");
197
-
198
- expect.extend(toHaveNoViolations);
199
-
200
- it("should demonstrate this matcher`s usage with a custom config", async () => {
201
- const render = () => `
202
- <div>
203
- <img src="#"/>
204
- </div>
205
- `;
206
-
207
- // pass anything that outputs html to axe
208
- const html = render();
209
-
210
- const results = await axe(html, {
211
- rules: {
212
- // for demonstration only, don't disable rules that need fixing.
213
- "image-alt": { enabled: false },
214
- },
215
- });
78
+ ```ts
79
+ import { mount } from "@vue/test-utils";
80
+ import { axe } from "vi-axe";
216
81
 
82
+ test("component has no a11y violations", async () => {
83
+ const wrapper = mount(MyComponent);
84
+ const results = await axe(wrapper.element);
217
85
  expect(results).toHaveNoViolations();
218
86
  });
219
87
  ```
220
88
 
221
- ### Testing isolated components
222
-
223
- > All page content must be contained by landmarks (region)
89
+ ### Custom configuration
224
90
 
225
- When testing with aXe sometimes it assumes you are testing a page. This then results in unexpected violations for landmarks for testing isolation components.
91
+ Use `configureAxe` to create an axe runner with default options. You can pass [axe-core run options](https://github.com/dequelabs/axe-core/blob/develop/doc/API.md) and vi-axe-specific options:
226
92
 
227
- You can disable this behaviour with the `region` rule:
228
-
229
- ```javascript
230
- const { configureAxe } = require("jest-axe");
93
+ ```ts
94
+ import { configureAxe } from "vi-axe";
231
95
 
232
96
  const axe = configureAxe({
233
- rules: {
234
- // disable landmark rules when testing isolated components.
235
- region: { enabled: false },
236
- },
237
- });
238
- ```
239
-
240
- ## Setting global configuration
241
-
242
- If you find yourself repeating the same options multiple times, you can export a version of the `axe` function with defaults set.
243
-
244
- Note: You can still pass additional options to this new instance; they will be merged with the defaults.
245
-
246
- This could be done in [Jest's setup step](https://jestjs.io/docs/en/setup-teardown)
247
-
248
- ```javascript
249
- // Global helper file (axe-helper.js)
250
- const { configureAxe } = require("jest-axe");
251
-
252
- const axe = configureAxe({
253
- rules: {
254
- // for demonstration only, don't disable rules that need fixing.
255
- "image-alt": { enabled: false },
97
+ globalOptions: {
98
+ rules: [{ id: "link-name", enabled: false }],
256
99
  },
257
100
  });
258
101
 
259
- module.exports = axe;
260
- ```
261
-
262
- ```javascript
263
- // Individual test file (test.js)
264
- const { toHaveNoViolations } = require("jest-axe");
265
- const axe = require("./axe-helper.js");
266
-
267
- expect.extend(toHaveNoViolations);
268
-
269
- it("should demonstrate this matcher`s usage with a default config", async () => {
270
- const render = () => `
271
- <div>
272
- <img src="#"/>
273
- </div>
274
- `;
275
-
276
- // pass anything that outputs html to axe
277
- const html = render();
278
-
279
- expect(await axe(html)).toHaveNoViolations();
102
+ test("custom axe run", async () => {
103
+ const results = await axe("<a href='#'></a>");
104
+ expect(results).toHaveNoViolations();
280
105
  });
281
106
  ```
282
107
 
283
- ### Setting custom rules and checks.
108
+ Per-run options can be passed as the second argument:
284
109
 
285
- The configuration object passed to `configureAxe`, accepts a `globalOptions` property to configure the format of the data used by axe and to add custom checks and rules. The property value is the same as the parameter passed to [axe.configure](https://github.com/dequelabs/axe-core/blob/master/doc/API.md#parameters-1).
286
-
287
- ```javascript
288
- // Global helper file (axe-helper.js)
289
- const { configureAxe } = require("jest-axe");
290
-
291
- const axe = configureAxe({
292
- globalOptions: {
293
- checks: [
294
- /* custom checks definitions */
295
- ],
296
- },
297
- // ...
110
+ ```ts
111
+ const results = await axe(html, {
112
+ rules: { "link-name": { enabled: false } },
298
113
  });
299
-
300
- module.exports = axe;
301
114
  ```
302
115
 
303
- ### Setting the level of user impact.
116
+ ### Filtering by impact level
304
117
 
305
- An array which defines which [impact](https://github.com/dequelabs/axe-core/blob/develop/doc/rule-descriptions.md) level should be considered. This ensures that only violations with a specific impact on the user are considered. The level of impact can be "minor", "moderate", "serious", or "critical".
118
+ To fail only for certain impact levels (e.g. critical/serious), use `impactLevels` in `configureAxe`. The matcher will then consider only violations with those impacts:
306
119
 
307
- ```javascript
308
- // Global helper file (axe-helper.js)
309
- const { configureAxe } = require("jest-axe");
120
+ ```ts
121
+ import { configureAxe } from "vi-axe";
310
122
 
311
123
  const axe = configureAxe({
312
- impactLevels: ["critical"],
313
- // ...
124
+ impactLevels: ["critical", "serious"],
314
125
  });
315
-
316
- module.exports = axe;
317
126
  ```
318
127
 
319
- Refer to [Developing Axe-core Rules](https://github.com/dequelabs/axe-core/blob/master/doc/rule-development.md) for instructions on how to develop custom rules and checks.
320
-
321
- ## Thanks
128
+ ## Requirements
322
129
 
323
- - [Jest][Jest] for the great test runner that allows extending matchers.
324
- - [axe](https://www.deque.com/axe/) for the wonderful axe-core that makes it so easy to do this.
325
- - Government Digital Service for making coding in the open the default.
326
- - GOV.UK Publishing Frontend team who published the [basis of the aXe reporter](https://github.com/alphagov/govuk_publishing_components/blob/581c22c9d35d85d5d985571d007f6397a4399f4c/spec/javascripts/govuk_publishing_components/AccessibilityTestSpec.js)
327
- - [jest-image-snapshot](https://github.com/americanexpress/jest-image-snapshot) for inspiration on README and repo setup
130
+ - **Node**: >= 20
131
+ - **Vitest** with **jsdom** (or another DOM environment); axe needs a DOM to run.
328
132
 
329
- [Jest]: https://jestjs.io/
133
+ Color contrast rules are disabled by default in vi-axe because they do not work reliably in jsdom.
@@ -1,24 +1,23 @@
1
1
  import { AxeResults, ImpactValue, RunOptions } from "axe-core";
2
2
  /** Global options passed to axe.configure (vi-axe uses rules array) */
3
3
  interface GlobalAxeOptions {
4
- rules?: Array<{
5
- id: string;
6
- enabled: boolean;
7
- }>;
8
- [key: string]: unknown;
4
+ rules?: Array<{
5
+ id: string;
6
+ enabled: boolean;
7
+ }>;
8
+ [key: string]: unknown;
9
9
  }
10
10
  /** Options for configureAxe: globalOptions plus runner options (impactLevels is vi-axe specific) */
11
11
  interface ConfigureAxeOptions {
12
- globalOptions?: GlobalAxeOptions;
13
- impactLevels?: ImpactValue[];
12
+ globalOptions?: GlobalAxeOptions;
13
+ impactLevels?: ImpactValue[];
14
14
  }
15
15
  type HtmlInput = Element | string;
16
16
  /**
17
- * Small wrapper for axe-core#run that enables promises (required for Jest),
18
- * default options and injects html to be tested
19
- * @param options default options to use in all instances
20
- * @returns returns instance of axe
21
- */
17
+ * Small wrapper for axe-core#run that enables promises (required for Jest),
18
+ * default options and injects html to be tested
19
+ * @param options default options to use in all instances
20
+ * @returns returns instance of axe
21
+ */
22
22
  export declare function configureAxe(options?: ConfigureAxeOptions & RunOptions): (html: HtmlInput, additionalOptions?: RunOptions) => Promise<AxeResults>;
23
23
  export {};
24
- //# sourceMappingURL=axe-utils.d.ts.map
@@ -0,0 +1,7 @@
1
+ interface AxeMatchers<TReturn = unknown> {
2
+ toHaveNoViolations: () => TReturn;
3
+ }
4
+ declare module "vitest" {
5
+ interface Matchers<T = any> extends AxeMatchers<T> {}
6
+ }
7
+ export {};
@@ -0,0 +1,2 @@
1
+ import { toHaveNoViolations as o } from "./index.js";
2
+ expect.extend(o);
@@ -0,0 +1,28 @@
1
+ import { ImpactValue, Result, RunOptions } from "axe-core";
2
+ import { configureAxe } from "./axe-utils.js";
3
+ /** Minimal axe results shape accepted by the matcher (vi-axe allows toolOptions.impactLevels) */
4
+ interface AxeResultsLike {
5
+ violations?: Result[];
6
+ toolOptions?: RunOptions & {
7
+ impactLevels?: ImpactValue[];
8
+ };
9
+ }
10
+ /** Matcher result returned by toHaveNoViolations */
11
+ interface MatcherResult {
12
+ actual: Result[];
13
+ message: () => string | undefined;
14
+ pass: boolean;
15
+ }
16
+ /** Vitest/Jest matcher object for toHaveNoViolations */
17
+ interface ToHaveNoViolationsMatcher {
18
+ toHaveNoViolations(results: AxeResultsLike): MatcherResult;
19
+ }
20
+ /**
21
+ * Custom Jest expect matcher, that can check aXe results for violations.
22
+ * @param results requires an instance of aXe's results object
23
+ * @returns returns Jest matcher object
24
+ */
25
+ declare const toHaveNoViolations: ToHaveNoViolationsMatcher;
26
+ export type { AxeResultsLike };
27
+ export { configureAxe, toHaveNoViolations };
28
+ export declare const axe: ReturnType<typeof configureAxe>;