-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
216 lines (189 loc) · 6.13 KB
/
index.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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
/* eslint no-console:0 */
'use strict'
// Prepare
const isWindows = process && process.platform && process.platform.indexOf('win') === 0
// Optional
let cliColor = null
if (process) {
try {
cliColor = require('cli-color')
}
catch (e) { }
}
/**
Console Reporter for Joe.
Joe attaches and calls the methods of this classes instance.
@example
const joe = require('joe').addReporter(require('joe-reporter-console').create({color: false}))
// joe.suite(...)
@constructor
@param {Object} [config]
@param {boolean} [config.color] - Enabled by default if not in the web browser, or `--no-colors` command line argument is missing
@param {boolean} [config.utf8] - Enabled by default if not on Windows
@param {string} [config.itemStart] - What to display when an item starts
@param {string} [config.markFail] - What to display when an item fails
@param {string} [config.markPass] - What to display when an item passes
@param {string} [config.itemArrow] - What to join item names with
@param {string} [config.summaryError] - What to display for error logs
@param {string} [config.summaryPass] - What to display if all goes well
@param {string} [config.summaryFail] - What to display if all went badly
@public
*/
class ConsoleReporter {
constructor (config = {}) {
this.errors = []
this.config = config
// Defaults
if (this.config.color == null) this.config.color = ((process && process.argv) || []).indexOf('--no-colors') === -1
if (this.config.utf8 == null) this.config.utf8 = !isWindows
if (this.config.markFail == null) this.config.markFail = this.config.utf8 ? '✘' : 'ERR!'
if (this.config.markPass == null) this.config.markPass = this.config.utf8 ? '✔' : 'OK '
if (this.config.itemNames == null) this.config.itemNames = this.config.utf8 ? '$a ➞ $b' : '$a > $b'
if (this.config.itemStart == null) this.config.itemStart = '$name'
if (this.config.itemFinish == null) this.config.itemFinish = '$name $mark'
if (this.config.summaryError == null) this.config.summaryError = '\nError #$index:\n$name\n$error'
if (this.config.summaryPass == null) this.config.summaryPass = '\n$totalPassedTests/$totalTests tests ran successfully, everything passed'
if (this.config.summaryFail == null) this.config.summaryFail = '\nFAILURE: $totalPassedTests/$totalTests tests ran successfully; $totalFailedTests failed, $totalIncompleteTests incomplete, $totalErrors errors'
// Colors
if (cliColor && this.config.color) {
this.config.markFail = cliColor.red(this.config.markFail)
this.config.markPass = cliColor.green(this.config.markPass)
this.config.itemArrow = cliColor.black(this.config.itemArrow)
this.config.summaryError = cliColor.red.underline(this.config.summaryError)
this.config.summaryPass = cliColor.green.underline(this.config.summaryPass)
this.config.summaryFail = cliColor.red.bold.underline(this.config.summaryFail)
}
}
/**
Creates and returns new instance of the current class.
@param {...*} args - The arguments to be forwarded along to the constructor.
@return {Object} The new instance.
@static
@public
*/
static create (...args) {
return new this(...args)
}
/**
Fetches the combined name of an item, paired with its parents.
@param {Suite|Test} item
@returns {string}
@access private
*/
getItemName (item) {
return item.names.filter((name) => name !== 'global joe suite').reduce((a, b) => this.formatMessage(this.config.itemNames, { a, b }))
}
/**
Injects the options into the message, when the option key is prefixed by a $
@param {string} message
@param {Object} opts
@returns {string}
@access private
*/
formatMessage (message, opts) {
Object.keys(opts).forEach(function (key) {
const value = opts[key]
message = message.replace('$' + key, value)
})
return message
}
/**
Report that a suite has started.
@param {Suite} suite
@returns {this}
@chainable
@access protected
*/
startSuite (suite) {
const format = this.config.itemStart
if (!format) return this
const name = this.getItemName(suite)
if (!name) return this
const message = this.formatMessage(format, { name })
if (!message) return this
console.log(message)
return this
}
/**
Report that a suite has finished.
@param {Suite} suite
@param {Error} [err]
@returns {this}
@chainable
@access protected
*/
finishSuite (suite, err) {
const format = this.config.itemFinish
if (!format) return this
const name = this.getItemName(suite)
if (!name) return this
const mark = err ? this.config.markFail : this.config.markPass
const message = this.formatMessage(format, { name, mark })
if (!message) return this
console.log(message)
return this
}
/**
Report that a test has started.
@param {Test} test
@returns {this}
@chainable
@access protected
*/
startTest (test) {
const format = this.config.itemStart
if (!format) return this
const name = this.getItemName(test)
if (!name) return this
const message = this.formatMessage(format, { name })
if (!message) return this
console.log(message)
return this
}
/**
Report that a test has finished.
@param {Test} test
@param {Error} [err]
@returns {this}
@chainable
@access protected
*/
finishTest (test, err) {
const format = this.config.itemFinish
if (!format) return this
const name = this.getItemName(test)
if (!name) return this
const mark = err ? this.config.markFail : this.config.markPass
const message = this.formatMessage(format, { name, mark })
if (!message) return this
console.log(message)
return this
}
/**
Report the summary when joe exits.
@param {number} exitCode
@returns {this}
@chainable
@access protected
*/
exit (exitCode) {
const totals = this.joe.getTotals()
console.log(exitCode
? this.formatMessage(this.config.summaryFail, totals)
: this.formatMessage(this.config.summaryPass, totals)
)
this.joe.getErrorLogs().forEach((errorLog, index) => {
const { suite, test, name, err } = errorLog
const item = test || suite
const message = this.formatMessage(this.config.summaryError, {
index: index + 1,
name: name || this.getItemName(item),
error: err.fullStack || err.stack || err.message || err
})
console.log(message)
})
return this
}
}
// Export
module.exports = ConsoleReporter