vi-axe 0.0.1-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/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (C) 2017 Crown Copyright (Government Digital Service)
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy of
6
+ this software and associated documentation files (the "Software"), to deal in
7
+ the Software without restriction, including without limitation the rights to
8
+ use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
9
+ of the Software, and to permit persons to whom the Software is furnished to do
10
+ so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,329 @@
1
+ # vi-axe [A modern Fork / Rewrite of jest-axe]
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).
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/
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=axe-utils.test.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"axe-utils.test.d.ts","sourceRoot":"","sources":["../../../src/__tests__/axe-utils.test.ts"],"names":[],"mappings":""}
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=reactjs.test.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"reactjs.test.d.ts","sourceRoot":"","sources":["../../../src/__tests__/reactjs.test.ts"],"names":[],"mappings":""}
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=vuejs.test.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"vuejs.test.d.ts","sourceRoot":"","sources":["../../../src/__tests__/vuejs.test.ts"],"names":[],"mappings":""}
@@ -0,0 +1,24 @@
1
+ import { AxeResults, ImpactValue, RunOptions } from "axe-core";
2
+ /** Global options passed to axe.configure (vi-axe uses rules array) */
3
+ interface GlobalAxeOptions {
4
+ rules?: Array<{
5
+ id: string;
6
+ enabled: boolean;
7
+ }>;
8
+ [key: string]: unknown;
9
+ }
10
+ /** Options for configureAxe: globalOptions plus runner options (impactLevels is vi-axe specific) */
11
+ interface ConfigureAxeOptions {
12
+ globalOptions?: GlobalAxeOptions;
13
+ impactLevels?: ImpactValue[];
14
+ }
15
+ type HtmlInput = Element | string;
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
+ */
22
+ export declare function configureAxe(options?: ConfigureAxeOptions & RunOptions): (html: HtmlInput, additionalOptions?: RunOptions) => Promise<AxeResults>;
23
+ export {};
24
+ //# sourceMappingURL=axe-utils.d.ts.map
@@ -0,0 +1 @@
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"}
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=extend-expect.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"extend-expect.d.ts","sourceRoot":"","sources":["../../src/extend-expect.ts"],"names":[],"mappings":""}
@@ -0,0 +1,28 @@
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
@@ -0,0 +1 @@
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"}