-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathpa11y.js
More file actions
82 lines (69 loc) · 1.75 KB
/
Copy pathpa11y.js
File metadata and controls
82 lines (69 loc) · 1.75 KB
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
import path from 'node:path';
import { styleText } from 'node:util';
import pa11y from 'pa11y';
import puppeteer from 'puppeteer';
/**
* @typedef {'light'|'dark'} ColorScheme
* @typedef {object} TestCase
* @property {string} name
* @property {ColorScheme} colorScheme
*/
const PAGE_URL = path.join(import.meta.dirname, 'resume.html');
/** @type {TestCase[]} */
const TEST_CASES = [
{
name: 'Light Mode (default)',
colorScheme: 'light'
},
{
name: 'Dark Mode',
colorScheme: 'dark'
},
];
/**
* @param {import('puppeteer').Browser} browser
* @param {TestCase} testCase
* @returns {Promise<any>}
*/
const pa11yRunner = async (browser, testCase) => {
const page = await browser.newPage();
await page.emulateMediaFeatures([
{ name: 'prefers-color-scheme', value: testCase.colorScheme }
]);
const results = await pa11y(PAGE_URL, {
standard: 'WCAG2AAA',
browser,
page,
});
await page.close();
return results;
};
const browser = await puppeteer.launch();
const results = await Promise.all(TEST_CASES.map(async (testCase) => ({
name: testCase.name,
result: await pa11yRunner(browser, testCase),
})));
await browser.close();
let failed = false;
for (const { name, result } of results) {
if (result.issues.length === 0) {
continue;
}
failed = true;
for (const issue of result.issues) {
console.error(
'%s > %s\n%s %s\n%s %s\n%s %s\n',
styleText(['blue', 'bold'], name),
styleText(['red', 'bold'], issue.code),
styleText('yellow', 'Message:'),
issue.message,
styleText('yellow', 'Context:'),
styleText(['gray', 'italic'], issue.context),
styleText('yellow', 'Selector:'),
styleText('gray', issue.selector),
);
}
}
if (failed) {
process.exitCode = 1;
}