-
-
Notifications
You must be signed in to change notification settings - Fork 75
/
Copy path_browser.mjs
69 lines (58 loc) · 1.59 KB
/
_browser.mjs
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
import puppeteer from 'puppeteer';
import http from 'http';
import { promises as fsp } from 'fs';
import test from 'node:test';
import process from 'node:process';
const requestListener = async function (req, res) {
const parsedUrl = new URL(req.url, 'http://localhost:8080');
const pathname = parsedUrl.pathname;
switch (pathname) {
case '':
case '/':
res.setHeader('Content-type', 'text/html');
res.writeHead(200);
res.end(await fsp.readFile('test/_browser.html', 'utf8'));
break;
case '/test/browser.expect.css':
res.writeHead(200);
res.end(await fsp.readFile('test/browser.expect.css', 'utf8'));
break;
default:
res.setHeader('Content-type', 'text/plain');
res.writeHead(404);
res.end('Not found');
break;
}
};
function startServers() {
const server = http.createServer(requestListener);
server.listen(8080);
return () => {
server.close();
};
}
if (!process.env.DEBUG) {
test('browser', { skip: process.env.GITHUB_ACTIONS && !process.env.BROWSER_TESTS }, async () => {
const cleanup = startServers();
const browser = await puppeteer.launch({
headless: 'new',
});
const page = await browser.newPage();
page.on('pageerror', (msg) => {
throw msg;
});
await page.goto('http://localhost:8080');
const result = await page.evaluate(async () => {
// eslint-disable-next-line no-undef
return await window.runTest();
});
if (!result) {
throw new Error('Test failed, expected "window.runTest()" to return true');
}
await browser.close();
await cleanup();
});
} else {
startServers();
console.log('visit : http://localhost:8080');
}