-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
karma.js
161 lines (141 loc) · 4.83 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
// Load our dependencies
var stringify = require('../common/stringify')
// Define our context Karma constructor
function ContextKarma (callParentKarmaMethod) {
// Define local variables
var hasError = false
var self = this
var isLoaded = false
// Define our loggers
// DEV: These are intentionally repeated in client and context
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
// Define our proxy error handler
// DEV: We require one in our context to track `hasError`
this.error = function () {
hasError = true
callParentKarmaMethod('error', [].slice.call(arguments))
return false
}
// Define our start handler
function UNIMPLEMENTED_START () {
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 && !isLoaded) {
isLoaded = true
try {
this.start(this.config)
} catch (error) {
this.error(error.stack || error.toString())
}
}
// remove reference to child iframe
this.start = UNIMPLEMENTED_START
}
// supposed to be overriden by the context
// TODO(vojta): support multiple callbacks (queue)
this.start = UNIMPLEMENTED_START
// Define proxy methods
// DEV: This is a closured `for` loop (same as a `forEach`) for IE support
var proxyMethods = ['complete', 'info', 'result']
for (var i = 0; i < proxyMethods.length; i++) {
(function bindProxyMethod (methodName) {
self[methodName] = function boundProxyMethod () {
callParentKarmaMethod(methodName, [].slice.call(arguments))
}
}(proxyMethods[i]))
}
// Define bindings for context window
this.setupContext = function (contextWindow) {
// If we clear the context after every run and we already had an error
// then stop now. Otherwise, carry on.
if (self.config.clearContext && hasError) {
return
}
// Perform window level bindings
// DEV: We return `self.error` since we want to `return false` to ignore errors
contextWindow.onerror = function () {
return self.error.apply(self, arguments)
}
// DEV: We must defined a function since we don't want to pass the event object
contextWindow.onbeforeunload = function (e, b) {
callParentKarmaMethod('onbeforeunload', [])
}
contextWindow.dump = function () {
self.log('dump', arguments)
}
var _confirm = contextWindow.confirm
var _prompt = contextWindow.prompt
contextWindow.alert = function (msg) {
self.log('alert', [msg])
}
contextWindow.confirm = function (msg) {
self.log('confirm', [msg])
return _confirm(msg)
}
contextWindow.prompt = function (msg, defaultVal) {
self.log('prompt', [msg, defaultVal])
return _prompt(msg, defaultVal)
}
// If we want to overload our console, then do it
function getConsole (currentWindow) {
return currentWindow.console || {
log: function () {},
info: function () {},
warn: function () {},
error: function () {},
debug: function () {}
}
}
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)
try {
return Function.prototype.apply.call(orig, localConsole, arguments)
} catch (error) {
self.log('warn', ['Console method ' + method + ' threw: ' + error])
}
}
}
for (var i = 0; i < logMethods.length; i++) {
patchConsoleMethod(logMethods[i])
}
}
}
}
// Define call/proxy methods
ContextKarma.getDirectCallParentKarmaMethod = function (parentWindow) {
return function directCallParentKarmaMethod (method, args) {
// If the method doesn't exist, then error out
if (!parentWindow.karma[method]) {
parentWindow.karma.error('Expected Karma method "' + method + '" to exist but it doesn\'t')
return
}
// Otherwise, run our method
parentWindow.karma[method].apply(parentWindow.karma, args)
}
}
ContextKarma.getPostMessageCallParentKarmaMethod = function (parentWindow) {
return function postMessageCallParentKarmaMethod (method, args) {
parentWindow.postMessage({ __karmaMethod: method, __karmaArguments: args }, window.location.origin)
}
}
// Export our module
module.exports = ContextKarma