-
Notifications
You must be signed in to change notification settings - Fork 22
/
jest.ts
61 lines (55 loc) · 2.1 KB
/
jest.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
/*
* Copyright (c) 2020, salesforce.com, inc.
* All rights reserved.
* SPDX-License-Identifier: BSD-3-Clause
* For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/BSD-3-Clause
*/
import { assertAccessible } from '@sa11y/assert';
import { A11yConfig, extended } from '@sa11y/preset-rules';
import { matcherHint } from 'jest-matcher-utils';
export const matcherHintMsg = `expected document to have no accessibility violations but found following issues: `;
// Type def for custom jest a11y matcher toBeAccessible
// Ref: https://jestjs.io/docs/en/expect.html#expectextendmatchers
declare global {
// eslint-disable-next-line @typescript-eslint/no-namespace
namespace jest {
interface Matchers<R> {
toBeAccessible(config?: A11yConfig): Promise<CustomMatcherResult>;
toBeAccessibleWith(config: A11yConfig): Promise<CustomMatcherResult>;
}
}
}
/**
* Jest expect matcher to check DOM for accessibility issues
* @param receivedDom - DOM to be tested for accessibility
* @param config - A11yConfig to be used to test for accessibility. Defaults to extended.
*/
export async function toBeAccessible(
receivedDom: Document = document,
config: A11yConfig = extended
): Promise<jest.CustomMatcherResult> {
let isAccessible = true;
let a11yViolations = '';
try {
await assertAccessible(receivedDom, config);
} catch (e) {
isAccessible = false;
a11yViolations = e;
}
return {
pass: isAccessible,
// Display assertion for the report when a test fails
message: (): string => matcherHint(`${toBeAccessible.name}: ${matcherHintMsg} \n\n ${a11yViolations}`),
};
}
/**
* Jest expect matcher to check DOM for accessibility issues with a `@sa11y/preset-rule`
* @param receivedDom - DOM to be tested for accessibility
* @param config - A11yConfig to be used to test for accessibility.
*/
export function toBeAccessibleWith(
receivedDom: Document = document,
config: A11yConfig
): Promise<jest.CustomMatcherResult> {
return toBeAccessible(receivedDom, config);
}