Skip to content

Commit 0d8802a

Browse files
committed
Add ui-test command
1 parent 4a150e8 commit 0d8802a

4 files changed

Lines changed: 71 additions & 6 deletions

File tree

.github/workflows/test.yml

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,7 @@ jobs:
1010
- run: npm audit --audit-level=low
1111
- run: npx c8 -r lcovonly -r text npm test
1212
- run: npx lj lint
13-
- name: Browser tests
14-
run: |
15-
npx lj serve &
16-
google-chrome --headless --virtual-time-budget=5000 --enable-logging=stderr http://127.0.0.1:8080/test/index.html 2>&1 | tee /tmp/browser-test.txt
17-
grep -q "# pass" /tmp/browser-test.txt && ! grep -q "FAIL" /tmp/browser-test.txt
13+
- run: npx lj ui-test test/index.html
1814
- uses: coverallsapp/github-action@v2
1915
name: Upload to coveralls.io
2016
with:

cli.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,11 @@ var fs = require("fs")
6767
script: true,
6868
out: "_site/",
6969
},
70+
"ui-test": {
71+
port: 8091,
72+
budget: 5000,
73+
_: ["test/index.html"]
74+
},
7075
test_t: {
7176
status: true,
7277
tz: "",

lib/serve.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ module.exports = function(opts) {
3434
zip: "application/zip"
3535
}
3636

37-
http.createServer(function(req, res) {
37+
return http.createServer(function(req, res) {
3838
console.log(new Date(), req.method, req.url)
3939
if (req.method === "GET") {
4040
sendFile(res, req.url)

lib/ui-test.js

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
//-
2+
//- Usage
3+
//- lj ui-test [URL_PATH]
4+
//-
5+
//- Options
6+
//- --port Server port (default: 8091)
7+
//- --budget Virtual time budget in ms (default: 5000)
8+
9+
var { command } = require("..")
10+
, consoleRe = /^[\s\S]*INFO:CONSOLE[^"]*"([\s\S]*)/
11+
12+
module.exports = function(opts) {
13+
var output = ""
14+
, buf = ""
15+
, urlPath = opts._[0] || "test/index.html"
16+
, browser = ["google-chrome-stable", "google-chrome", "chromium-browser", "chromium"].find(command)
17+
, retries = 10
18+
opts._ = []
19+
20+
if (!browser) {
21+
console.error("No browser found")
22+
process.exitCode = 1
23+
return
24+
}
25+
26+
var server = require("./serve")(opts)
27+
.on("error", function(e) {
28+
if (e.code === "EADDRINUSE" && retries--) return server.listen(++opts.port, "127.0.0.1")
29+
console.error(e.message)
30+
process.exitCode = 1
31+
})
32+
.on("listening", function() {
33+
var url = "http://127.0.0.1:" + opts.port + "/" + urlPath
34+
, proc = require("child_process").spawn(browser, [
35+
"--headless",
36+
"--virtual-time-budget=" + opts.budget,
37+
"--enable-logging=stderr",
38+
"--no-sandbox",
39+
url
40+
], { stdio: ["ignore", "ignore", "pipe"] })
41+
42+
proc.stderr.on("data", function(chunk) {
43+
buf += chunk
44+
var lines = buf.split(/", source: .*\n/m)
45+
buf = lines.pop()
46+
lines.forEach(parseLine)
47+
})
48+
49+
proc.on("close", function() {
50+
parseLine(buf)
51+
server.close()
52+
process.exitCode = /\u2714/.test(output) && !/FAIL|\u2718/.test(output) ? 0 : 1
53+
})
54+
})
55+
56+
function parseLine(line) {
57+
var match = consoleRe.exec(line)
58+
if (match) {
59+
console.log(match[1])
60+
output += match[1]
61+
}
62+
}
63+
}
64+

0 commit comments

Comments
 (0)