-
Notifications
You must be signed in to change notification settings - Fork 161
/
Copy pathadapter.js
359 lines (304 loc) · 9.97 KB
/
adapter.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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
/* jshint globalstrict: true */
'use strict'
/**
* Decision maker for whether a stack entry is considered external to jasmine and karma.
* @param {String} entry Error stack entry.
* @return {Boolean} True if external, False otherwise.
*/
function isExternalStackEntry (entry) {
return (entry ? true : false) &&
// entries related to jasmine and karma-jasmine:
!/\/(jasmine-core|karma-jasmine)\//.test(entry) &&
// karma specifics, e.g. "at http://localhost:7018/karma.js:185"
!/\/(karma.js|context.html):/.test(entry)
}
/**
* Returns relevant stack entries.
* @param {String} stack Complete error stack trace.
* @return {Array} A list of relevant stack entries.
*/
function getRelevantStackFrom (stack) {
var filteredStack = [],
relevantStack = []
stack = stack.split('\n')
for (var i = 0; i < stack.length; i += 1) {
if (isExternalStackEntry(stack[i])) {
filteredStack.push(stack[i])
}
}
// If the filtered stack is empty, i.e. the error originated entirely from within jasmine or karma, then the whole stack
// should be relevant.
if (filteredStack.length === 0) {
filteredStack = stack
}
for (i = 0; i < filteredStack.length; i += 1) {
if (filteredStack[i]) {
relevantStack.push(filteredStack[i])
}
}
return relevantStack
}
/**
* Custom formatter for a failed step.
*
* Different browsers report stack trace in different ways. This function
* attempts to provide a concise, relevant error message by removing the
* unnecessary stack traces coming from the testing framework itself as well
* as possible repetition.
*
* @see https://github.com/karma-runner/karma-jasmine/issues/60
* @param {Object} step Step object with stack and message properties.
* @return {String} Formatted step.
*/
function formatFailedStep (step) {
// Safari seems to have no stack trace,
// so we just return the error message:
if (!step.stack) { return step.message; }
var relevantMessage = []
var relevantStack = []
// Remove the message prior to processing the stack to prevent issues like
// https://github.com/karma-runner/karma-jasmine/issues/79
var stack = step.stack.replace('Error: ' + step.message, '')
var dirtyRelevantStack = getRelevantStackFrom(stack)
// PhantomJS returns multiline error message for errors coming from specs
// (for example when calling a non-existing function). This error is present
// in both `step.message` and `step.stack` at the same time, but stack seems
// preferable, so we iterate relevant stack, compare it to message:
for (var i = 0; i < dirtyRelevantStack.length; i += 1) {
if (step.message && step.message.indexOf(dirtyRelevantStack[i]) === -1) {
// Stack entry is not in the message,
// we consider it to be a relevant stack:
relevantStack.push(dirtyRelevantStack[i])
} else {
// Stack entry is already in the message,
// we consider it to be a suitable message alternative:
relevantMessage.push(dirtyRelevantStack[i])
}
}
// In most cases the above will leave us with an empty message...
if (relevantMessage.length === 0) {
// Let's reuse the original message:
relevantMessage.push(step.message)
// Now we probably have a repetition case where:
// relevantMessage: ["Expected true to be false."]
// relevantStack: ["Error: Expected true to be false.", ...]
if (relevantStack.length && relevantStack[0].indexOf(step.message) !== -1) {
// The message seems preferable, so we remove the first value from
// the stack to get rid of repetition :
relevantStack.shift()
}
}
// Example output:
// --------------------
// Chrome 40.0.2214 (Mac OS X 10.9.5) xxx should return false 1 FAILED
// Expected true to be false
// at /foo/bar/baz.spec.js:22:13
// at /foo/bar/baz.js:18:29
return relevantMessage.concat(relevantStack).join('\n')
}
function SuiteNode (name, parent) {
this.name = name
this.parent = parent
this.children = []
this.addChild = function (name) {
var suite = new SuiteNode(name, this)
this.children.push(suite)
return suite
}
}
function processSuite (suite, pointer) {
var child
var childPointer
for (var i = 0; i < suite.children.length; i++) {
child = suite.children[i]
if (child.children) {
childPointer = pointer[child.description] = {_: []}
processSuite(child, childPointer)
} else {
if (!pointer._) {
pointer._ = []
}
pointer._.push(child.description)
}
}
}
function getAllSpecNames (topSuite) {
var specNames = {}
processSuite(topSuite, specNames)
return specNames
}
/**
* Very simple reporter for Jasmine.
*/
function KarmaReporter (tc, jasmineEnv) {
var currentSuite = new SuiteNode()
/**
* @param suite
* @returns {boolean} Return true if it is system jasmine top level suite
*/
function isTopLevelSuite (suite) {
return suite.description === 'Jasmine_TopLevel_Suite'
}
/**
* Jasmine 2.0 dispatches the following events:
*
* - jasmineStarted
* - jasmineDone
* - suiteStarted
* - suiteDone
* - specStarted
* - specDone
*/
this.jasmineStarted = function (data) {
// TODO(vojta): Do not send spec names when polling.
tc.info({
total: data.totalSpecsDefined,
specs: getAllSpecNames(jasmineEnv.topSuite())
})
}
this.jasmineDone = function () {
tc.complete({
coverage: window.__coverage__
})
}
this.suiteStarted = function (result) {
if (!isTopLevelSuite(result)) {
currentSuite = currentSuite.addChild(result.description)
}
}
this.suiteDone = function (result) {
// In the case of xdescribe, only "suiteDone" is fired.
// We need to skip that.
if (result.description !== currentSuite.name) {
return
}
currentSuite = currentSuite.parent
}
this.specStarted = function (specResult) {
specResult.startTime = new Date().getTime()
}
this.specDone = function (specResult) {
var skipped = specResult.status === 'disabled' || specResult.status === 'pending'
var result = {
description: specResult.description,
id: specResult.id,
log: [],
skipped: skipped,
success: specResult.failedExpectations.length === 0,
suite: [],
time: skipped ? 0 : new Date().getTime() - specResult.startTime,
executedExpectationsCount: specResult.failedExpectations.length + specResult.passedExpectations.length
}
// generate ordered list of (nested) suite names
var suitePointer = currentSuite
while (suitePointer.parent) {
result.suite.unshift(suitePointer.name)
suitePointer = suitePointer.parent
}
if (!result.success) {
var steps = specResult.failedExpectations
for (var i = 0, l = steps.length; i < l; i++) {
result.log.push(formatFailedStep(steps[i]))
}
}
tc.result(result)
delete specResult.startTime
}
}
/**
* Extract grep option from karma config
* @param {[Array|string]} clientArguments The karma client arguments
* @return {string} The value of grep option by default empty string
*/
var getGrepOption = function (clientArguments) {
var grepRegex = /^--grep=(.*)$/
if (Object.prototype.toString.call(clientArguments) === '[object Array]') {
var indexOfGrep = indexOf(clientArguments, '--grep')
if (indexOfGrep !== -1) {
return clientArguments[indexOfGrep + 1]
}
return map(filter(clientArguments ,function (arg) {
return grepRegex.test(arg)
}), function (arg) {
return arg.replace(grepRegex, '$1')
})[0] || ''
} else if (typeof clientArguments === 'string') {
var match = /--grep=([^=]+)/.exec(clientArguments)
return match ? match[1] : ''
}
}
/**
* Create jasmine spec filter
* @param {Object} options Spec filter options
*/
var KarmaSpecFilter = function (options) {
var filterString = options && options.filterString() && options.filterString().replace(/[-[\]{}()*+?.,\\^$|#\s]/g, '\\$&')
var filterPattern = new RegExp(filterString)
this.matches = function (specName) {
return filterPattern.test(specName)
}
}
/**
* @param {Object} config The karma config
* @param {Object} jasmineEnv jasmine environment object
*/
var createSpecFilter = function (config, jasmineEnv) {
var specFilter = new KarmaSpecFilter({
filterString: function () {
return getGrepOption(config.args)
}
})
jasmineEnv.specFilter = function (spec) {
return specFilter.matches(spec.getFullName())
}
}
/**
* Karma starter function factory.
*
* This function is invoked from the wrapper.
* @see adapter.wrapper
*
* @param {Object} karma Karma runner instance.
* @param {Object} [jasmineEnv] Optional Jasmine environment for testing.
* @return {Function} Karma starter function.
*/
function createStartFn (karma, jasmineEnv) {
// This function will be assigned to `window.__karma__.start`:
return function () {
jasmineEnv = jasmineEnv || window.jasmine.getEnv()
jasmineEnv.addReporter(new KarmaReporter(karma, jasmineEnv))
jasmineEnv.execute()
}
}
function indexOf(collection, find, i /*opt*/) {
if (collection.indexOf) {
return collection.indexOf(find, i);
}
if (i === undefined) {i = 0;}
if (i < 0) {i += collection.length;}
if (i < 0) {i = 0;}
for (var n = collection.length; i < n; i++) {
if (i in collection && collection[i] === find) {
return i;}}
return -1
}
function filter(collection, filter, that /*opt*/) {
if (collection.filter) {
return collection.filter(filter, that);
}
var other = [], v
for (var i = 0, n = collection.length; i < n; i++) {
if (i in collection && filter.call(that, v = collection[i], i, collection)) {
other.push(v);}}
return other
}
function map(collection, mapper, that /*opt*/) {
if (collection.map) {
return collection.map(mapper, that);
}
var other = new Array(collection.length)
for (var i = 0, n = collection.length; i < n; i++) {
if (i in collection) {
other[i] = mapper.call(that, collection[i], i, collection);}}
return other
}