-
Notifications
You must be signed in to change notification settings - Fork 806
/
Copy pathnode.js
93 lines (85 loc) · 2.44 KB
/
node.js
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
// this file is purposefully written without mocha and in es5 syntax in order
// to be compatible with node 4+
var axe = require('../../');
var assert = require('assert');
var spawn = require('child_process').spawn;
var fs = require('fs');
var path = require('path');
initJsdom(function (err, window) {
assert.equal(err, null);
console.log('running axe');
axe.run(
window.document.documentElement,
{
preload: false,
rules: { 'color-contrast': { enabled: false } }
},
function (axeError, results) {
assert.equal(axeError, null);
assert.notEqual(results.violations.length, 0);
console.log('axe ran successfully');
}
);
});
/**
* Install a version of jsdom that is compatible with the currently running node
* version and return the jsdom window object.
* @param {Function} callback - callback function when jsdom is installed.
* Is passed any error object and the jsdom window object.
*/
function initJsdom(callback) {
try {
var nodeToJsdomMatrix = {
4: '9.12.0', // last jsdom version that supported this node version
6: '11.12.0',
8: '15.2.1',
10: '16.7.0',
12: '19.0.0',
14: '21.1.2',
16: '22.1.0'
};
var majorNodeVersion = process.versions.node.split('.')[0];
var jsdomVersion = nodeToJsdomMatrix[majorNodeVersion] || 'latest';
console.log('node version detected as: v' + majorNodeVersion);
console.log('installing jsdom@' + jsdomVersion);
var child = spawn(
'npm',
['install', 'jsdom@' + jsdomVersion, '--no-save'],
{
cwd: __dirname
}
);
child.stdout.setEncoding('utf8');
child.stderr.setEncoding('utf8');
child.stdout.on('data', function (data) {
console.log(data);
});
child.stderr.on('data', function (data) {
console.error(data);
});
child.on('close', function () {
console.log('installed');
var jsdom = require('jsdom');
var domStr = fs.readFileSync(
path.join('test', 'integration', 'full', 'all-rules', 'all-rules.html'),
'utf8'
);
// jsdom 9
if (jsdom.env) {
jsdom.env(domStr, function (jsdomError, window) {
if (jsdomError) {
callback(jsdomError);
}
callback(null, window);
});
}
// jsdom 11+
else {
var dom = new jsdom.JSDOM(domStr);
callback(null, dom.window);
}
});
} catch (err) {
callback(err);
}
}