vi-axe 0.0.1-pre-alpha → 0.0.2-pre-alpha

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,8 @@
1
- # vi-axe [A modern Fork / Rewrite of jest-axe]
1
+ # vi-axe
2
+
3
+ ### A modern Fork / Rewrite of jest-axe for vitest
2
4
 
3
5
  [![npm version](https://img.shields.io/npm/v/vi-axe.svg)](http://npm.im/vi-axe)
4
6
  ![node](https://img.shields.io/node/v/vi-axe)
5
7
 
6
8
  # 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).
13
-
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.
15
-
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
22
-
23
- Color contrast checks do not work in JSDOM so are turned off in jest-axe.
24
-
25
- ## Installation:
26
-
27
- ```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
- });
77
- ```
78
-
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");
84
-
85
- const { render } = require("@testing-library/react");
86
- const { axe, toHaveNoViolations } = require("jest-axe");
87
- expect.extend(toHaveNoViolations);
88
-
89
- it("should demonstrate this matcher`s usage with react testing library", async () => {
90
- const { container } = render(<App />);
91
- const results = await axe(container);
92
-
93
- expect(results).toHaveNoViolations();
94
- });
95
- ```
96
-
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.
99
-
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
- });
109
- ```
110
-
111
- ### Testing Vue with [Vue Test Utils](https://vue-test-utils.vuejs.org/)
112
-
113
- ```javascript
114
- const App = require("./App.vue");
115
-
116
- const { mount } = require("@vue/test-utils");
117
- const { axe, toHaveNoViolations } = require("jest-axe");
118
- expect.extend(toHaveNoViolations);
119
-
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);
123
-
124
- expect(results).toHaveNoViolations();
125
- });
126
- ```
127
-
128
- ### Testing Vue with [Vue Testing Library](https://testing-library.com/docs/vue-testing-library/intro)
129
-
130
- ```javascript
131
- const App = require("./app");
132
-
133
- const { render } = require("@testing-library/vue");
134
- const { axe, toHaveNoViolations } = require("jest-axe");
135
- expect.extend(toHaveNoViolations);
136
-
137
- it("should demonstrate this matcher`s usage with react testing library", async () => {
138
- const { container } = render(<App />);
139
- const results = await axe(container);
140
-
141
- expect(results).toHaveNoViolations();
142
- });
143
- ```
144
-
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.
147
-
148
- ### Testing Angular with [Nx](https://nx.dev/)
149
-
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
- });
216
-
217
- expect(results).toHaveNoViolations();
218
- });
219
- ```
220
-
221
- ### Testing isolated components
222
-
223
- > All page content must be contained by landmarks (region)
224
-
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.
226
-
227
- You can disable this behaviour with the `region` rule:
228
-
229
- ```javascript
230
- const { configureAxe } = require("jest-axe");
231
-
232
- 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 },
256
- },
257
- });
258
-
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();
280
- });
281
- ```
282
-
283
- ### Setting custom rules and checks.
284
-
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
- // ...
298
- });
299
-
300
- module.exports = axe;
301
- ```
302
-
303
- ### Setting the level of user impact.
304
-
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".
306
-
307
- ```javascript
308
- // Global helper file (axe-helper.js)
309
- const { configureAxe } = require("jest-axe");
310
-
311
- const axe = configureAxe({
312
- impactLevels: ["critical"],
313
- // ...
314
- });
315
-
316
- module.exports = axe;
317
- ```
318
-
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
322
-
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
328
-
329
- [Jest]: https://jestjs.io/
@@ -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 @@
1
+ export {};
@@ -0,0 +1,2 @@
1
+ import { toHaveNoViolations as o } from "./index.js";
2
+ expect.extend({ toHaveNoViolations: 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>;
@@ -28669,7 +28669,9 @@ function $O(I) {
28669
28669
  };
28670
28670
  return document.body.innerHTML = I, [document.body, b];
28671
28671
  }
28672
- throw typeof I == "string" ? new TypeError(`html parameter ("${I}") has no elements`) : new TypeError("html parameter should be an HTML string or an HTML element");
28672
+ throw typeof I == "string" ? new TypeError(`html parameter ("${I}") has no elements`) : new TypeError(
28673
+ "html parameter should be an HTML string or an HTML element"
28674
+ );
28673
28675
  }
28674
28676
  function BO(I = {}) {
28675
28677
  const { globalOptions: B = {}, ...b } = I, { rules: Q = [], ...w } = B, d = PO.map(({ ruleId: v }) => ({
@@ -28682,9 +28684,13 @@ function BO(I = {}) {
28682
28684
  }), function(A, p = {}) {
28683
28685
  const [g, y] = $O(A), m = IO({}, b, p);
28684
28686
  return new Promise((D, x) => {
28685
- gf.run(g, m, (S, C) => {
28686
- y(), S && x(S), D(C);
28687
- });
28687
+ gf.run(
28688
+ g,
28689
+ m,
28690
+ (S, C) => {
28691
+ y(), S && x(S), D(C);
28692
+ }
28693
+ );
28688
28694
  });
28689
28695
  };
28690
28696
  }
@@ -28702,7 +28708,10 @@ const VO = {
28702
28708
  const { toolOptions: B } = I;
28703
28709
  let b = [];
28704
28710
  B && "impactLevels" in B && B.impactLevels && ({ impactLevels: b } = B);
28705
- const Q = qO(I.violations, b), d = ((p) => {
28711
+ const Q = qO(
28712
+ I.violations,
28713
+ b
28714
+ ), d = ((p) => {
28706
28715
  if (p.length === 0)
28707
28716
  return "";
28708
28717
  const g = `
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "vi-axe",
3
- "version": "0.0.1-pre-alpha",
3
+ "version": "0.0.2-pre-alpha",
4
4
  "description": "Custom Vitest matcher for aXe for testing accessibility",
5
5
  "keywords": [
6
6
  "a11y",
@@ -17,10 +17,15 @@
17
17
  ],
18
18
  "type": "module",
19
19
  "main": "dist/vi-axe.js",
20
+ "types": "dist/vi-axe.d.ts",
20
21
  "exports": {
21
22
  ".": {
22
- "import": "./dist/vi-axe.js",
23
- "require": "./dist/vi-axe.cjs"
23
+ "types": "./dist/vi-axe.d.ts",
24
+ "import": "./dist/vi-axe.js"
25
+ },
26
+ "./extend-expect": {
27
+ "types": "./dist/extend-expect.d.ts",
28
+ "import": "./dist/extend-expect.js"
24
29
  }
25
30
  },
26
31
  "scripts": {
@@ -1,2 +0,0 @@
1
- export {};
2
- //# sourceMappingURL=axe-utils.test.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"axe-utils.test.d.ts","sourceRoot":"","sources":["../../../src/__tests__/axe-utils.test.ts"],"names":[],"mappings":""}
@@ -1,2 +0,0 @@
1
- export {};
2
- //# sourceMappingURL=reactjs.test.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"reactjs.test.d.ts","sourceRoot":"","sources":["../../../src/__tests__/reactjs.test.ts"],"names":[],"mappings":""}
@@ -1,2 +0,0 @@
1
- export {};
2
- //# sourceMappingURL=vuejs.test.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"vuejs.test.d.ts","sourceRoot":"","sources":["../../../src/__tests__/vuejs.test.ts"],"names":[],"mappings":""}
@@ -1 +0,0 @@
1
- {"version":3,"file":"axe-utils.d.ts","sourceRoot":"","sources":["../../src/axe-utils.ts"],"names":[],"mappings":"AAAA,OAAgB,EAAE,UAAU,EAAE,WAAW,EAAE,UAAU,EAAE,MAAM,UAAU,CAAC;AAGxE,uEAAuE;AACvE,UAAU,gBAAgB;IACxB,KAAK,CAAC,EAAE,KAAK,CAAC;QAAE,EAAE,EAAE,MAAM,CAAC;QAAC,OAAO,EAAE,OAAO,CAAA;KAAE,CAAC,CAAC;IAChD,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAAC;CACxB;AAED,oGAAoG;AACpG,UAAU,mBAAmB;IAC3B,aAAa,CAAC,EAAE,gBAAgB,CAAC;IACjC,YAAY,CAAC,EAAE,WAAW,EAAE,CAAC;CAC9B;AAED,KAAK,SAAS,GAAG,OAAO,GAAG,MAAM,CAAC;AAsDlC;;;;;GAKG;AACH,wBAAgB,YAAY,CAAC,OAAO,GAAE,mBAAmB,GAAG,UAAe,IA0BrD,MAAM,SAAS,EAAE,oBAAmB,UAAe,KAAG,OAAO,CAAC,UAAU,CAAC,CAc9F"}
@@ -1,2 +0,0 @@
1
- export {};
2
- //# sourceMappingURL=extend-expect.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"extend-expect.d.ts","sourceRoot":"","sources":["../../src/extend-expect.ts"],"names":[],"mappings":""}
@@ -1,28 +0,0 @@
1
- import "./types/global.d.ts";
2
- import { ImpactValue, Result, RunOptions } from "axe-core";
3
- import { configureAxe } from "./axe-utils";
4
- /** Minimal axe results shape accepted by the matcher (vi-axe allows toolOptions.impactLevels) */
5
- interface AxeResultsLike {
6
- violations?: Result[];
7
- toolOptions?: RunOptions & {
8
- impactLevels?: ImpactValue[];
9
- };
10
- }
11
- /** Matcher result returned by toHaveNoViolations */
12
- interface MatcherResult {
13
- actual: Result[];
14
- message: () => string | undefined;
15
- pass: boolean;
16
- }
17
- /**
18
- * Custom Jest expect matcher, that can check aXe results for violations.
19
- * @param results requires an instance of aXe's results object
20
- * @returns returns Jest matcher object
21
- */
22
- declare const toHaveNoViolations: {
23
- toHaveNoViolations(results: AxeResultsLike): MatcherResult;
24
- };
25
- export type { AxeResultsLike };
26
- export { configureAxe, toHaveNoViolations };
27
- export declare const axe: (html: string | Element, additionalOptions?: RunOptions) => Promise<import("axe-core").AxeResults>;
28
- //# sourceMappingURL=index.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,qBAAqB,CAAC;AAE7B,OAAO,EAAE,WAAW,EAAc,MAAM,EAAE,UAAU,EAAE,MAAM,UAAU,CAAC;AAGvE,OAAO,EAAE,YAAY,EAAE,MAAM,aAAa,CAAC;AAE3C,iGAAiG;AACjG,UAAU,cAAc;IACtB,UAAU,CAAC,EAAE,MAAM,EAAE,CAAC;IACtB,WAAW,CAAC,EAAE,UAAU,GAAG;QAAE,YAAY,CAAC,EAAE,WAAW,EAAE,CAAA;KAAE,CAAC;CAC7D;AAoBD,oDAAoD;AACpD,UAAU,aAAa;IACrB,MAAM,EAAE,MAAM,EAAE,CAAC;IACjB,OAAO,EAAE,MAAM,MAAM,GAAG,SAAS,CAAC;IAClC,IAAI,EAAE,OAAO,CAAC;CACf;AAED;;;;GAIG;AACH,QAAA,MAAM,kBAAkB;gCACM,cAAc,GAAG,aAAa;CA+D3D,CAAC;AAEF,YAAY,EAAE,cAAc,EAAE,CAAC;AAC/B,OAAO,EAAE,YAAY,EAAE,kBAAkB,EAAE,CAAC;AAC5C,eAAO,MAAM,GAAG,oGAAiB,CAAC"}