-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathWebConsole.js
118 lines (102 loc) · 3.53 KB
/
WebConsole.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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
;
(function(root, factory) {
if (typeof define === 'function' && define.amd) {
define([''], factory)
} else {
root.WebConsole = factory()
}
})(this, function() {
function WebConsole(runner) {
//TODO needs to be removed to options somehow
var reporterQueryParameter = 'test=console'
var stats = { suites: 0, tests: 0, passes: 0, pending: 0, failures: 0 }
var failures = this.failures = []
var total = runner.total
var title = document.title
var calls = []
runner.stats = stats
runner.on('pass', function(test) {
stats.passes = stats.passes || 0
var medium = test.slow() / 2
test.speed = test.duration > test.slow() ? 'slow' : test.duration > medium ? 'medium' : 'fast'
stats.passes++
})
runner.on('pending', function() {
stats.pending++
})
runner.on('start', function() {
stats.start = (new Date)
})
runner.on('test', function(test) {
console.log('Running test:', test.title)
})
runner.on('fail', function(test, err) {
stats.failures = stats.failures || 0
stats.failures++
test.err = err
failures.push(test)
calls.push(['info', null, test.title])
calls.push(['error', null, test.err.stack])
calls.push(['log', null, {Expected: err.expected, Actual: err.actual }])
flagFailures(test.parent)
})
function parentSuiteTitle(suite) {
if (!suite.parent) return suite.title
return parentSuiteTitle(suite.parent) + ' ' + suite.title
}
function flagFailures(node) {
node.hasFailures = true
if (node.parent) flagFailures(node.parent)
}
runner.on('suite', function(suite) {
stats.suites = stats.suites || 0
suite.root || stats.suites++
var parameter = '?grep=' + encodeURIComponent(suite.fullTitle()) + '&' + reporterQueryParameter
var location = document.location
var url = location.origin + location.pathname + parameter
if (!suite.root) {
calls.push(['group', suite, suite.title])
calls.push(['groupCollapsed', suite , 'url'])
calls.push(['log', suite, url])
calls.push(['groupEnd', suite])
}
})
runner.on('suite end', function(suite) {
calls.push(['groupEnd', suite])
logNewCalls()
})
runner.on('test end', function(test) {
stats.tests = stats.tests || 0
stats.tests++
var percent = stats.tests / total * 100 | 0
document.title = percent + '% ' + (stats.failures ? stats.failures + ' failures ' : '' ) + title
})
runner.on('end', function() {
stats.end = (new Date)
stats.duration = (new Date) - stats.start
logNewCalls()
if (stats.errors) console.warn(stats.errors, ' errors')
if (stats.failures) console.warn(stats.failures, ' failures')
var skipped = stats.tests - stats.failures - stats.passes
if (skipped) console.warn(skipped, ' skipped')
console.log(stats.passes, ' tests passed')
console.log(stats.duration / 1000, ' seconds')
console.log((new Date).toUTCString())
console.log('Run all tests ' + location.origin + location.pathname + '?' + reporterQueryParameter)
})
function logNewCalls() {
while (calls.length > 0) {
logCall(calls.shift())
}
}
function logCall(call) {
var command = call.shift()
var suite = call.shift()
var failures = !suite || suite.hasFailures
if (failures || command === 'info' || command === 'error') {
console[command].apply(console, call)
}
}
}
return WebConsole
})