forked from ciena-blueplanet/ember-test-utils
-
Notifications
You must be signed in to change notification settings - Fork 0
/
reporter.js
180 lines (148 loc) · 4.09 KB
/
reporter.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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
/**
* @typedef {Object} TestResult
* @property {Boolean} failed - whether or not test failed
* @property {String} name - name of test
* @property {Boolean} passed - whether or not test passed
* @property {Number} runDuration - run duration in milliseconds
* @property {Boolean} skipped - whether or not test was skipped
*/
/**
* Get duration in human friendly format
* @param {Number} value - duration in milliseconds
* @returns {String} human friendly duration
*/
function getHumanReadableDuration (value) {
if (value < 1000) {
return value + ' ms'
}
// Convert from milliseconds to seconds
value = value / 1000
if (value < 60) {
return value + ' s'
}
// Convert from seconds to minutes
value = value / 60
if (value < 60) {
return value + ' min'
}
// Convert from minutes to hours
value = value / 60
return value + ' hr'
}
/**
* Sort tests by duration
* @param {TestResult} a - first test
* @param {TestResult} b - second test
* @returns {Number} number indicating sort order
*/
function testSorter (a, b) {
return a.result.runDuration - b.result.runDuration
}
/**
* Write out individual test result
* @param {Object} test - the test params
* @param {String} test.launcher - the launcher (browser) for the test
* @param {TestResult} test.result - test to write out result of
* @param {Boolean} verbose - whether or not to show additional error information
*/
function testWriter (test, verbose) {
var launcher = test.launcher
var result = test.result
// Other properties that may be useful: logs, error, launcherId, items
var humanFriendlyDuration = getHumanReadableDuration(result.runDuration)
this.out.write('[' + humanFriendlyDuration + '] [' + launcher + '] ' + result.name + '\n')
if (verbose) {
if (result.logs && result.logs.length !== 0) {
this.out.write('\tLogs:\n')
result.logs.forEach((log) => {
this.out.write('\t\t' + log + '\n')
})
}
if (result.error) {
var e = result.error
this.out.write('\n\tError: ' + e.message + '\n')
if (e.stack) {
this.out.write('\n\t\t' + e.stack.toString().replace(/\n/g, '\n\t\t') + '\n')
}
}
}
}
/**
* Write passed tests
*/
function writePassedTests () {
if (this.passedTests.length !== 0) {
this.out.write('PASSED TESTS\n\n')
this.passedTests
.sort(testSorter)
.forEach(testWriter.bind(this))
this.out.write('\n')
}
}
/**
* Write out summary of test run
* @param {String} humanReadableDuration - human readable test run duration
*/
function writeSummary (humanReadableDuration) {
var passed = this.pass
var pending = this.skipped
var total = this.total
var failed = total - pending - passed
this.out.write(
'ran ' + total + ' tests in ' + humanReadableDuration + ' [' + passed +
' passed / ' + failed + ' failed / ' + pending + ' pending]\n\n'
)
}
function Reporter (silent, out) {
// Set options
this.out = out || process.stdout
this.silent = silent
// Set test arrays
this.failedTests = []
this.passedTests = []
// Set counters
this.pass = 0
this.skipped = 0
this.total = 0
// Other stuff
this.endTime = null
this.id = 1
this.stoppedOnError = null
this.startTime = new Date()
}
Reporter.prototype = {
finish: function () {
if (this.silent) {
return
}
this.endTime = new Date()
var duration = Math.round(this.endTime - this.startTime)
var humanReadableDuration = getHumanReadableDuration(duration)
if (this.failedTests.length === 0) {
writeSummary.call(this, humanReadableDuration)
writePassedTests.call(this)
} else {
this.out.write('\n')
}
writeSummary.call(this, humanReadableDuration)
},
report: function (prefix, data) {
var test = {
launcher: prefix,
result: data
}
if (data.pending) {
this.skipped++
} else if (data.passed) {
this.pass++
this.passedTests.push(test)
this.out.write('.')
} else {
this.out.write('\n\n')
this.failedTests.push(test)
testWriter.call(this, test, true)
}
this.total++
}
}
module.exports = Reporter