-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
60 lines (51 loc) · 1.28 KB
/
index.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
'use strict';
const jsdom = require('jsdom');
/**
* @typedef {import('@mochify/mochify').MochifyDriver} MochifyDriver
*/
exports.mochifyDriver = mochifyDriver;
/**
* @param {Object} [options]
* @returns {Promise<MochifyDriver>}
*/
function mochifyDriver(options = {}) {
const {
stderr = process.stderr,
url = 'http://localhost',
pretendToBeVisual = true,
strictSSL = false,
...jsdom_options
} = options;
const virtual_console = new jsdom.VirtualConsole();
const { window } = new jsdom.JSDOM(
'<!DOCTYPE html>\n<html><body></body></html>',
{
url,
pretendToBeVisual,
strictSSL,
...jsdom_options,
virtualConsole: virtual_console,
runScripts: 'dangerously'
}
);
function end() {
return Promise.resolve();
}
virtual_console.on('jsdomError', (error) => {
process.exitCode = 1;
if (error && error.type === 'unhandled exception') {
// These errors will be logged by the global onerror handler
// in the client script, so there's no need to duplicate them here.
return;
}
stderr.write(error.stack || String(error));
stderr.write('\n');
});
function evaluate(script) {
return Promise.resolve(window.eval(script));
}
return Promise.resolve({
evaluate,
end
});
}