-
Notifications
You must be signed in to change notification settings - Fork 13
/
test.js
73 lines (65 loc) · 2.56 KB
/
test.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
import puppeteer from 'puppeteer';
(async () => {
try {
// Open our browser.
const browser = await puppeteer.launch({
timeout: 10000,
// opt-in to the new Chrome headless implementation
// ref: https://developer.chrome.com/articles/new-headless/
headless: 'new',
args: [
// Disables interactive prompt: Do you want to the application Chromium.app to accept incoming network connections?
// ref: https://github.com/puppeteer/puppeteer/issues/4752#issuecomment-586599843
'--disable-features=DialMediaRouteProvider',
],
});
const page = await browser.newPage();
// Starts to gather coverage information for JS and CSS files
await page.coverage.startJSCoverage();
// Before navigation, start mapping browser logs to stdout.
page.on('console', message => console.log(message.text())); // eslint-disable-line no-console
// Visit our test page.
await page.goto('http://127.0.0.1:8080/test/?x-test-run-coverage');
// Wait to be signaled about the end of the test. Because the test may have
// not started, already started, or already ended, ping for status.
await page.evaluate(async () => {
await new Promise(resolve => {
const onMessage = evt => {
const { type, data } = evt.data;
if (
type === 'x-test-root-coverage-request' ||
type === 'x-test-root-end' ||
(type === 'x-test-root-pong' && (data.waiting || data.ended))
) {
top.removeEventListener('message', onMessage);
resolve();
}
};
top.addEventListener('message', onMessage);
top.postMessage({ type: 'x-test-client-ping' }, '*');
});
});
// Gather / send coverage information.
const js = await page.coverage.stopJSCoverage();
// Send coverage information to x-test and await test completion.
await page.evaluate(async data => {
await new Promise(resolve => {
const onMessage = evt => {
const { type } = evt.data;
if (type === 'x-test-root-end') {
top.removeEventListener('message', onMessage);
resolve();
}
};
top.addEventListener('message', onMessage);
top.postMessage({ type: 'x-test-client-coverage-result', data }, '*');
});
}, { js });
// Close our browser.
await browser.close();
} catch (err) {
// Ensure we exit with a non-zero code if anything fails (e.g., timeout).
console.error(err); // eslint-disable-line no-console
process.exit(1);
}
})();