-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
92 lines (78 loc) · 1.98 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
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
'use strict';
const driver = require('puppeteer');
/**
* @import { Writable } from 'node:stream'
* @import { PuppeteerLaunchOptions } from 'puppeteer'
* @import { MochifyDriver } from '@mochify/mochify'
*/
/**
* @typedef {Object} PuppeteerDriverOptions
* @property {string} [url]
* @property {Writable} [stderr]
*/
exports.mochifyDriver = mochifyDriver;
/**
* @param {PuppeteerDriverOptions & PuppeteerLaunchOptions} [options]
* @returns {Promise<MochifyDriver>}
*/
async function mochifyDriver(options = {}) {
const {
url = `file:${__dirname}/index.html`,
stderr = /** @type {Writable} */ (process.stderr),
...launch_options
} = options;
// In case this arrives through CLI flags, yargs will pass a string
// when a single arg is given and an Array of strings when multiple
// args are given.
const extra_args = launch_options.args || [];
launch_options.args = [
'--allow-insecure-localhost',
'--disable-dev-shm-usage',
...extra_args
];
const browser = await driver.launch({
headless: true,
acceptInsecureCerts: true,
...launch_options
});
const page = await browser.newPage();
page.on('console', (msg) => {
const type = msg.type();
const text = msg.text();
if (type === 'log') {
return;
}
if (type === 'warn' && text.includes('window.webkitStorageInfo')) {
// Swallow deprecation warning.
return;
}
stderr.write(text);
stderr.write('\n');
});
/**
* @param {Error} err
*/
function handlePuppeteerError(err) {
stderr.write(err.stack || String(err));
stderr.write('\n');
process.exitCode = 1;
end();
}
page.on('pageerror', handlePuppeteerError).on('error', handlePuppeteerError);
async function end() {
await page.close();
await browser.close();
}
await page.goto(url);
/**
* @param {string} script
* @returns {Promise<Object>}
*/
function evaluate(script) {
return page.evaluate(script);
}
return {
evaluate,
end
};
}