Skip to content

Commit 188b702

Browse files
committed
Add script to run QUnit tests headless with PhantomJS
Colorization is done by wrapper `script/test` script that detects whether output is a tty. I haven't figured how to do this from PhantomJS runtime.
1 parent c267ed2 commit 188b702

File tree

2 files changed

+69
-0
lines changed

2 files changed

+69
-0
lines changed

script/test

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#!/bin/bash
2+
set -e
3+
4+
if [ -t 1 ]; then
5+
red="$(printf "\033[31m")"
6+
brightred="$(printf "\033[31;1m")"
7+
green="$(printf "\033[32m")"
8+
reset="$(printf "\033[m")"
9+
else
10+
red=
11+
brightred=
12+
green=
13+
reset=
14+
fi
15+
16+
phantomjs tests/runner.coffee tests/qunit.html | sed -E "
17+
# failure line:
18+
s/^(✘.+)/${red}\\1${reset}/
19+
# failure details:
20+
s/^( .+)/${brightred}\\1${reset}/
21+
# success marker:
22+
s/(✔︎)/${green}\\1${reset}/
23+
"

tests/runner.coffee

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
urls = require('system').args.slice(1)
2+
page = require('webpage').create()
3+
timeout = 3000
4+
5+
qunitHooks = ->
6+
window.document.addEventListener 'DOMContentLoaded', ->
7+
for callback in ['log', 'testDone', 'done']
8+
do (callback) ->
9+
QUnit[callback] (result) ->
10+
window.callPhantom
11+
name: "QUnit.#{callback}"
12+
data: result
13+
14+
page.onInitialized = -> page.evaluate qunitHooks
15+
16+
page.onConsoleMessage = (msg) -> console.log msg
17+
18+
page.onCallback = (event) ->
19+
if event.name is 'QUnit.log'
20+
details = event.data
21+
if details.result is false
22+
console.log "#{details.module}: #{details.name}"
23+
if details.message and details.message isnt "failed"
24+
console.log " #{details.message}"
25+
if "actual" of details
26+
console.log " expected: #{details.expected}"
27+
console.log " actual: #{details.actual}"
28+
else if event.name is 'QUnit.testDone'
29+
result = event.data
30+
unless result.failed
31+
console.log "✔︎ #{result.module}: #{result.name}"
32+
else if event.name is 'QUnit.done'
33+
res = event.data
34+
console.log "#{res.total} tests, #{res.failed} failed. Done in #{res.runtime} ms"
35+
phantom.exit if !res.total or res.failed then 1 else 0
36+
37+
for url in urls
38+
page.open url, (status) ->
39+
if status isnt 'success'
40+
console.error "failed opening #{url}: #{status}"
41+
phantom.exit 1
42+
else
43+
setTimeout ->
44+
console.error "ERROR: Test execution has timed out"
45+
phantom.exit 1
46+
, timeout

0 commit comments

Comments
 (0)