-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
karma.js
246 lines (205 loc) · 6.2 KB
/
karma.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
var stringify = require('./stringify')
var constant = require('./constants')
var util = require('./util')
var Karma = function (socket, iframe, opener, navigator, location) {
var hasError = false
var startEmitted = false
var reloadingContext = false
var store = {}
var self = this
var queryParams = util.parseQueryParams(location.search)
var browserId = queryParams.id || util.generateId('manual-')
var returnUrl = queryParams['return_url' + ''] || null
var resultsBufferLimit = 50
var resultsBuffer = []
this.VERSION = constant.VERSION
this.config = {}
// Expose for testing purposes as there is no global socket.io
// registry anymore.
this.socket = socket
var childWindow = null
var navigateContextTo = function (url) {
reloadingContext = true
if (self.config.useIframe === false) {
if (childWindow === null || childWindow.closed === true) {
// If this is the first time we are opening the window, or the window is closed
childWindow = opener('about:blank')
}
childWindow.location = url
childWindow.onLoad = function () {
reloadingContext = false
}
} else {
iframe.src = url
iframe.onLoad = function () {
reloadingContext = false
}
}
}
this.setupContext = function (contextWindow) {
if (hasError) {
return
}
var getConsole = function (currentWindow) {
return currentWindow.console || {
log: function () {},
info: function () {},
warn: function () {},
error: function () {},
debug: function () {}
}
}
contextWindow.__karma__ = this
// This causes memory leak in Chrome (17.0.963.66)
contextWindow.onerror = function () {
return contextWindow.__karma__.error.apply(contextWindow.__karma__, arguments)
}
contextWindow.onbeforeunload = function (e, b) {
if (!reloadingContext) {
// TODO(vojta): show what test (with explanation about jasmine.UPDATE_INTERVAL)
contextWindow.__karma__.error('Some of your tests did a full page reload!')
}
}
if (self.config.captureConsole) {
// patch the console
var localConsole = contextWindow.console = getConsole(contextWindow)
var logMethods = ['log', 'info', 'warn', 'error', 'debug']
var patchConsoleMethod = function (method) {
var orig = localConsole[method]
if (!orig) {
return
}
localConsole[method] = function () {
self.log(method, arguments)
return Function.prototype.apply.call(orig, localConsole, arguments)
}
}
for (var i = 0; i < logMethods.length; i++) {
patchConsoleMethod(logMethods[i])
}
}
contextWindow.dump = function () {
self.log('dump', arguments)
}
contextWindow.alert = function (msg) {
self.log('alert', [msg])
}
}
this.log = function (type, args) {
var values = []
for (var i = 0; i < args.length; i++) {
values.push(this.stringify(args[i], 3))
}
this.info({log: values.join(', '), type: type})
}
this.stringify = stringify
var clearContext = function () {
navigateContextTo('about:blank')
}
// error during js file loading (most likely syntax error)
// we are not going to execute at all
this.error = function (msg, url, line) {
hasError = true
var message = msg
if (url) {
message = msg + '\nat ' + url + (line ? ':' + line : '')
}
socket.emit('karma_error', message)
this.complete()
return false
}
this.result = function (result) {
if (!startEmitted) {
socket.emit('start', {total: null})
startEmitted = true
}
if (resultsBufferLimit === 1) {
return socket.emit('result', result)
}
resultsBuffer.push(result)
if (resultsBuffer.length === resultsBufferLimit) {
socket.emit('result', resultsBuffer)
resultsBuffer = []
}
}
this.complete = function (result) {
if (resultsBuffer.length) {
socket.emit('result', resultsBuffer)
resultsBuffer = []
}
// give the browser some time to breath, there could be a page reload, but because a bunch of
// tests could run in the same event loop, we wouldn't notice.
setTimeout(function () {
clearContext()
}, 0)
socket.emit('complete', result || {}, function () {
if (returnUrl) {
location.href = returnUrl
}
})
}
this.info = function (info) {
// TODO(vojta): introduce special API for this
if (!startEmitted && util.isDefined(info.total)) {
socket.emit('start', info)
startEmitted = true
} else {
socket.emit('info', info)
}
}
var UNIMPLEMENTED_START = function () {
this.error('You need to include some adapter that implements __karma__.start method!')
}
// all files loaded, let's start the execution
this.loaded = function () {
// has error -> cancel
if (!hasError) {
this.start(this.config)
}
// remove reference to child iframe
this.start = UNIMPLEMENTED_START
}
this.store = function (key, value) {
if (util.isUndefined(value)) {
return store[key]
}
if (util.instanceOf(value, 'Array')) {
var s = store[key] = []
for (var i = 0; i < value.length; i++) {
s.push(value[i])
}
} else {
// TODO(vojta): clone objects + deep
store[key] = value
}
}
// supposed to be overriden by the context
// TODO(vojta): support multiple callbacks (queue)
this.start = UNIMPLEMENTED_START
socket.on('execute', function (cfg) {
// reset hasError and reload the iframe
hasError = false
startEmitted = false
self.config = cfg
navigateContextTo(constant.CONTEXT_URL)
// clear the console before run
// works only on FF (Safari, Chrome do not allow to clear console from js source)
if (window.console && window.console.clear) {
window.console.clear()
}
})
socket.on('stop', function () {
this.complete()
}.bind(this))
// report browser name, id
socket.on('connect', function () {
socket.io.engine.on('upgrade', function () {
resultsBufferLimit = 1
})
socket.emit('register', {
name: navigator.userAgent,
id: browserId
})
})
}
module.exports = Karma