Skip to content

Commit b01fe9a

Browse files
committed
Use our own more simple vitest-axe extension
Signed-off-by: MTRNord <[email protected]>
1 parent d28405b commit b01fe9a

File tree

7 files changed

+112
-23
lines changed

7 files changed

+112
-23
lines changed

example-widget-mui/package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,6 @@
5454
]
5555
},
5656
"devDependencies": {
57-
"@chialab/vitest-axe": "^0.18.1",
5857
"@matrix-widget-toolkit/testing": "2.5.0",
5958
"@testing-library/dom": "^10.4.0",
6059
"@testing-library/jest-dom": "^6.5.0",

example-widget-mui/src/setupTests.ts

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,19 +20,41 @@
2020
// learn more: https://github.com/testing-library/jest-dom
2121
import '@testing-library/jest-dom/vitest';
2222

23-
import vitestAxeMatchers from '@chialab/vitest-axe';
24-
import matchers from '@testing-library/jest-dom/matchers';
25-
import '@testing-library/jest-dom/vitest';
2623
import { TextDecoder, TextEncoder } from 'util';
2724
import { expect, vi } from 'vitest';
2825

29-
expect.extend(matchers);
30-
3126
// Make sure to initialize i18n (see mock below)
27+
import { AxeResults } from 'axe-core';
3228
import './i18n';
3329

3430
// Add support for axe
35-
expect.extend(vitestAxeMatchers);
31+
expect.extend({
32+
toHaveNoViolations(results: AxeResults) {
33+
const violations = results.violations ?? [];
34+
35+
return {
36+
pass: violations.length === 0,
37+
actual: violations,
38+
message() {
39+
if (violations.length === 0) {
40+
return '';
41+
}
42+
43+
return `Expected no accessibility violations but received some.
44+
45+
${violations
46+
.map(
47+
(violation) => `[${violation.impact}] ${violation.id}
48+
${violation.description}
49+
${violation.helpUrl}
50+
`,
51+
)
52+
.join('\n')}
53+
`;
54+
},
55+
};
56+
},
57+
});
3658

3759
// Use a different configuration for i18next during tests
3860
vi.mock('./i18n', async () => {

example-widget-mui/src/vitest.d.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/*
2+
* Copyright 2024 Nordeck IT + Consulting GmbH
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
import 'vitest';
18+
19+
interface AxeMatchers<R = unknown> {
20+
toHaveNoViolations: () => R;
21+
}
22+
23+
declare module 'vitest' {
24+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
25+
interface Assertion<T = any> extends AxeMatchers<T> {}
26+
interface AsymmetricMatchersContaining extends AxeMatchers {}
27+
}

packages/mui/package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
"module": "./src/index.ts",
99
"types": "./src/index.ts",
1010
"devDependencies": {
11-
"@chialab/vitest-axe": "^0.18.1",
1211
"@testing-library/jest-dom": "^6.5.0",
1312
"@testing-library/react": "^16.0.1",
1413
"@testing-library/user-event": "^14.5.2",

packages/mui/src/setupTests.ts

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,9 @@
2121
import '@testing-library/jest-dom/vitest';
2222

2323
// Make sure to initialize i18n (see mock below)
24-
import vitestAxeMatchers from '@chialab/vitest-axe';
2524
import i18n from 'i18next';
25+
26+
import { AxeResults } from 'axe-core';
2627
import { initReactI18next } from 'react-i18next';
2728
import { expect } from 'vitest';
2829

@@ -35,4 +36,30 @@ i18n.use(initReactI18next).init({
3536
});
3637

3738
// Add support for axe
38-
expect.extend(vitestAxeMatchers);
39+
expect.extend({
40+
toHaveNoViolations(results: AxeResults) {
41+
const violations = results.violations ?? [];
42+
43+
return {
44+
pass: violations.length === 0,
45+
actual: violations,
46+
message() {
47+
if (violations.length === 0) {
48+
return '';
49+
}
50+
51+
return `Expected no accessibility violations but received some.
52+
53+
${violations
54+
.map(
55+
(violation) => `[${violation.impact}] ${violation.id}
56+
${violation.description}
57+
${violation.helpUrl}
58+
`,
59+
)
60+
.join('\n')}
61+
`;
62+
},
63+
};
64+
},
65+
});

packages/mui/src/vitest.d.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/*
2+
* Copyright 2024 Nordeck IT + Consulting GmbH
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
import 'vitest';
18+
19+
interface AxeMatchers<R = unknown> {
20+
toHaveNoViolations: () => R;
21+
}
22+
23+
declare module 'vitest' {
24+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
25+
interface Assertion<T = any> extends AxeMatchers<T> {}
26+
interface AsymmetricMatchersContaining extends AxeMatchers {}
27+
}

yarn.lock

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -326,11 +326,6 @@
326326
human-id "^1.0.2"
327327
prettier "^2.7.1"
328328

329-
"@chialab/vitest-axe@^0.18.1":
330-
version "0.18.1"
331-
resolved "https://registry.yarnpkg.com/@chialab/vitest-axe/-/vitest-axe-0.18.1.tgz#4d6a3ae3628bc3222b4640295023445e0e530619"
332-
integrity sha512-kdEwEdb6ill2i3Vjm/txJiWzBRES04HowNIzk9KUXy4VH35BXQpfdlNrb8p5c5vztZr0CFuUUyPhQVSBLjateg==
333-
334329
"@emotion/babel-plugin@^11.12.0":
335330
version "11.12.0"
336331
resolved "https://registry.yarnpkg.com/@emotion/babel-plugin/-/babel-plugin-11.12.0.tgz#7b43debb250c313101b3f885eba634f1d723fcc2"
@@ -1567,7 +1562,7 @@
15671562
semver "^7.6.0"
15681563
ts-api-utils "^1.3.0"
15691564

1570-
"@typescript-eslint/[email protected]", "@typescript-eslint/utils@^6.0.0 || ^7.0.0 || ^8.0.0":
1565+
"@typescript-eslint/[email protected]":
15711566
version "8.4.0"
15721567
resolved "https://registry.yarnpkg.com/@typescript-eslint/utils/-/utils-8.4.0.tgz#35c552a404858c853a1f62ba6df2214f1988afc3"
15731568
integrity sha512-swULW8n1IKLjRAgciCkTCafyTHHfwVQFt8DovmaF69sKbOxTSFMmIZaSHjqO9i/RV0wIblaawhzvtva8Nmm7lQ==
@@ -2905,13 +2900,6 @@ eslint-config-prettier@^9.1.0:
29052900
resolved "https://registry.yarnpkg.com/eslint-config-prettier/-/eslint-config-prettier-9.1.0.tgz#31af3d94578645966c082fcb71a5846d3c94867f"
29062901
integrity sha512-NSWl5BFQWEPi1j4TjVNItzYV7dZXZ+wP6I6ZhrBGpChQhZRUaElihE9uRRkcbRnNb76UMKDF3r+WTmNcGPKsqw==
29072902

2908-
eslint-plugin-jest@^28.8.2:
2909-
version "28.8.3"
2910-
resolved "https://registry.yarnpkg.com/eslint-plugin-jest/-/eslint-plugin-jest-28.8.3.tgz#c5699bba0ad06090ad613535e4f1572f4c2567c0"
2911-
integrity sha512-HIQ3t9hASLKm2IhIOqnu+ifw7uLZkIlR7RYNv7fMcEi/p0CIiJmfriStQS2LDkgtY4nyLbIZAD+JL347Yc2ETQ==
2912-
dependencies:
2913-
"@typescript-eslint/utils" "^6.0.0 || ^7.0.0 || ^8.0.0"
2914-
29152903
eslint-plugin-notice@^1.0.0:
29162904
version "1.0.0"
29172905
resolved "https://registry.yarnpkg.com/eslint-plugin-notice/-/eslint-plugin-notice-1.0.0.tgz#4b98ffdf2274d10f34a4231a74efafdecad6cde4"

0 commit comments

Comments
 (0)