generated from hmcts/expressjs-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patha11y.ts
113 lines (96 loc) · 3.12 KB
/
a11y.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
import * as supertest from 'supertest';
import { app } from '../../main/app';
const pa11y = require('pa11y');
const agent = supertest.agent(app);
class Pa11yResult {
documentTitle: string;
pageUrl: string;
issues: PallyIssue[];
constructor(documentTitle: string, pageUrl: string, issues: PallyIssue[]) {
this.documentTitle = documentTitle;
this.pageUrl = pageUrl;
this.issues = issues;
}
}
class PallyIssue {
code: string;
context: string;
message: string;
selector: string;
type: string;
typeCode: number;
constructor(code: string, context: string, message: string, selector: string, type: string, typeCode: number) {
this.code = code;
this.context = context;
this.message = message;
this.selector = selector;
this.type = type;
this.typeCode = typeCode;
}
}
async function ensurePageCallWillSucceed(url: string): Promise<void> {
try {
console.log(`Ensuring page call for URL: ${url}`);
const res = await agent.get(url);
console.log(`Response for URL: ${url} - Status: ${res.status} - Text: ${res.text}`);
if (res.redirect) {
throw new Error(`Call to ${url} resulted in a redirect to ${res.get('Location')}`);
}
if (res.serverError) {
throw new Error(`Call to ${url} resulted in internal server error: ${res.text}`);
}
} catch (err) {
console.error(`Error ensuring page call for URL ${url}:`, err);
throw err;
}
}
async function runPally(url: string): Promise<Pa11yResult> {
try {
console.log(`Running Pa11y on URL: ${url}`);
const result = await pa11y(url, {
hideElements: '.govuk-footer__licence-logo, .govuk-header__logotype-crown',
});
console.log(`Pa11y result for URL: ${url} - Result:`, result);
return result;
} catch (err) {
console.error(`Pa11y error on URL ${url}:`, err);
throw err;
}
}
function expectNoErrors(messages: PallyIssue[]): void {
const errors = messages.filter(m => m.type === 'error');
if (errors.length > 0) {
const errorsAsJson = `${JSON.stringify(errors, null, 2)}`;
console.error('Accessibility issues found:', errorsAsJson);
throw new Error(`There are accessibility issues:
${errorsAsJson}
`);
}
}
function testAccessibility(url: string): void {
describe(`Page ${url}`, () => {
test('should have no accessibility errors', async () => {
try {
console.log(`Starting accessibility test for URL: ${url}`);
await ensurePageCallWillSucceed(url);
const result = await runPally(agent.get(url).url);
expect(result.issues).toEqual(expect.any(Array));
expectNoErrors(result.issues);
} catch (err) {
console.error(`Accessibility test failed for URL ${url} with error:`, err);
throw err;
}
});
});
}
describe('Accessibility', () => {
process.on('unhandledRejection', (reason, promise) => {
console.error('Unhandled Rejection at:', promise, 'reason:', reason);
});
process.on('uncaughtException', err => {
console.error('Uncaught Exception thrown', err);
process.exit(1);
});
testAccessibility('/');
// TODO: include each path of your application in accessibility checks
});