-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Expand file tree
/
Copy pathbuiltins.js
More file actions
326 lines (304 loc) · 11.5 KB
/
builtins.js
File metadata and controls
326 lines (304 loc) · 11.5 KB
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
describe('wrapped built-ins', function () {
it('should capture exceptions from event listeners', function () {
return runInSandbox(sandbox, function () {
var div = document.createElement('div');
document.body.appendChild(div);
div.addEventListener(
'click',
function () {
window.element = div;
window.context = this;
foo();
},
false
);
var click = new MouseEvent('click');
div.dispatchEvent(click);
}).then(function (summary) {
// Make sure we preserve the correct context
assert.equal(summary.window.element, summary.window.context);
delete summary.window.element;
delete summary.window.context;
assert.match(summary.events[0].exception.values[0].value, /baz/);
});
});
it('should transparently remove event listeners from wrapped functions', function () {
return runInSandbox(sandbox, function () {
var div = document.createElement('div');
document.body.appendChild(div);
var fooFn = function () {
foo();
};
var barFn = function () {
bar();
};
div.addEventListener('click', fooFn);
div.addEventListener('click', barFn);
div.removeEventListener('click', barFn);
div.dispatchEvent(new MouseEvent('click'));
}).then(function (summary) {
assert.lengthOf(summary.events, 1);
});
});
it('should remove the original callback if it was registered before Sentry initialized (w. original method)', function () {
return runInSandbox(sandbox, function () {
var div = document.createElement('div');
document.body.appendChild(div);
window.capturedCall = false;
var captureFn = function () {
window.capturedCall = true;
};
// Use original addEventListener to simulate non-wrapped behavior (callback is attached without __sentry_wrapped__)
window.originalBuiltIns.addEventListener.call(div, 'click', captureFn);
// Then attach the same callback again, but with already wrapped method
div.addEventListener('click', captureFn);
div.removeEventListener('click', captureFn);
div.dispatchEvent(new MouseEvent('click'));
}).then(function (summary) {
assert.equal(summary.window.capturedCall, false);
delete summary.window.capturedCalls;
});
});
it('should capture exceptions inside setTimeout', function () {
return runInSandbox(sandbox, function () {
setTimeout(function () {
foo();
});
}).then(function (summary) {
assert.match(summary.events[0].exception.values[0].value, /baz/);
});
});
it('should capture exceptions inside setInterval', function () {
return runInSandbox(sandbox, function () {
var exceptionInterval = setInterval(function () {
clearInterval(exceptionInterval);
foo();
}, 0);
}).then(function (summary) {
assert.match(summary.events[0].exception.values[0].value, /baz/);
});
});
describe('requestAnimationFrame', function () {
it('should capture exceptions inside callback', function () {
// needs to be visible or requestAnimationFrame won't ever fire
sandbox.style.display = 'block';
return runInSandbox(sandbox, { manual: true }, function () {
requestAnimationFrame(function () {
window.finalizeManualTest();
foo();
});
}).then(function (summary) {
assert.match(summary.events[0].exception.values[0].value, /baz/);
});
});
it('wrapped callback should preserve correct context - window (not-bound)', function () {
// needs to be visible or requestAnimationFrame won't ever fire
sandbox.style.display = 'block';
return runInSandbox(sandbox, { manual: true }, function () {
requestAnimationFrame(function () {
window.capturedCtx = this;
window.finalizeManualTest();
});
}).then(function (summary) {
assert.strictEqual(summary.window.capturedCtx, summary.window);
delete summary.window.capturedCtx;
});
});
it('wrapped callback should preserve correct context - class bound method', function () {
// needs to be visible or requestAnimationFrame won't ever fire
sandbox.style.display = 'block';
return runInSandbox(sandbox, { manual: true }, function () {
// TypeScript-transpiled class syntax
var Foo = (function () {
function Foo() {
var _this = this;
this.magicNumber = 42;
this.getThis = function () {
window.capturedCtx = _this;
window.finalizeManualTest();
};
}
return Foo;
})();
var foo = new Foo();
requestAnimationFrame(foo.getThis);
}).then(function (summary) {
assert.strictEqual(summary.window.capturedCtx.magicNumber, 42);
delete summary.window.capturedCtx;
});
});
it('wrapped callback should preserve correct context - `bind` bound method', function () {
// needs to be visible or requestAnimationFrame won't ever fire
sandbox.style.display = 'block';
return runInSandbox(sandbox, { manual: true }, function () {
function foo() {
window.capturedCtx = this;
window.finalizeManualTest();
}
requestAnimationFrame(foo.bind({ magicNumber: 42 }));
}).then(function (summary) {
assert.strictEqual(summary.window.capturedCtx.magicNumber, 42);
delete summary.window.capturedCtx;
});
});
});
it('should capture exceptions from XMLHttpRequest event handlers (e.g. onreadystatechange)', function () {
return runInSandbox(sandbox, { manual: true }, function () {
var xhr = new XMLHttpRequest();
xhr.open('GET', '/base/subjects/example.json');
// intentionally assign event handlers *after* open, since this is what jQuery does
xhr.onreadystatechange = function wat() {
window.finalizeManualTest();
// replace onreadystatechange with no-op so exception doesn't
// fire more than once as XHR changes loading state
xhr.onreadystatechange = function () {};
foo();
};
xhr.send();
}).then(function (summary) {
assert.match(summary.events[0].exception.values[0].value, /baz/);
if (IS_LOADER) {
assert.ok(summary.events[0].exception.values[0].mechanism);
} else {
var handler = summary.events[0].exception.values[0].mechanism.data.handler;
delete summary.events[0].exception.values[0].mechanism.data.handler;
if (summary.window.canReadFunctionName()) {
assert.equal(handler, 'wat');
} else {
assert.equal(handler, '<anonymous>');
}
assert.deepEqual(summary.events[0].exception.values[0].mechanism, {
type: 'instrument',
handled: false,
data: {
function: 'onreadystatechange',
},
});
}
});
});
it('should not call XMLHttpRequest onreadystatechange more than once per state', function () {
return runInSandbox(sandbox, { manual: true }, function () {
window.calls = {};
var xhr = new XMLHttpRequest();
xhr.open('GET', '/base/subjects/example.json');
xhr.onreadystatechange = function wat() {
window.calls[xhr.readyState] = window.calls[xhr.readyState] ? window.calls[xhr.readyState] + 1 : 1;
if (xhr.readyState === 4) {
window.finalizeManualTest();
}
};
xhr.send();
}).then(function (summary) {
for (var state in summary.window.calls) {
assert.equal(summary.window.calls[state], 1);
}
// IE Triggers all states (1-4), including 1 (open), despite it being assigned before
// the `onreadystatechange` handler.
assert.isAtLeast(Object.keys(summary.window.calls).length, 3);
assert.isAtMost(Object.keys(summary.window.calls).length, 4);
delete summary.window.calls;
});
});
it(optional("should capture built-in's mechanism type as instrument", IS_LOADER), function () {
return runInSandbox(sandbox, function () {
setTimeout(function () {
foo();
});
}).then(function (summary) {
if (IS_LOADER) {
// The async loader doesn't wrap setTimeout
// so we don't receive the full mechanism
assert.ok(summary.events[0].exception.values[0].mechanism);
} else {
var fn = summary.events[0].exception.values[0].mechanism.data.function;
delete summary.events[0].exception.values[0].mechanism.data;
if (summary.window.canReadFunctionName()) {
assert.equal(fn, 'setTimeout');
} else {
assert.equal(fn, '<anonymous>');
}
assert.deepEqual(summary.events[0].exception.values[0].mechanism, {
type: 'instrument',
handled: false,
});
}
});
});
it(optional("should capture built-in's handlers fn name in mechanism data", IS_LOADER), function () {
return runInSandbox(sandbox, function () {
var div = document.createElement('div');
document.body.appendChild(div);
div.addEventListener(
'click',
function namedFunction() {
foo();
},
false
);
var click = new MouseEvent('click');
div.dispatchEvent(click);
}).then(function (summary) {
if (IS_LOADER) {
// The async loader doesn't wrap addEventListener
// so we don't receive the full mechanism
assert.ok(summary.events[0].exception.values[0].mechanism);
} else {
var handler = summary.events[0].exception.values[0].mechanism.data.handler;
delete summary.events[0].exception.values[0].mechanism.data.handler;
var target = summary.events[0].exception.values[0].mechanism.data.target;
delete summary.events[0].exception.values[0].mechanism.data.target;
if (summary.window.canReadFunctionName()) {
assert.equal(handler, 'namedFunction');
} else {
assert.equal(handler, '<anonymous>');
}
// IE vs. Rest of the world
assert.oneOf(target, ['Node', 'EventTarget']);
assert.deepEqual(summary.events[0].exception.values[0].mechanism, {
type: 'instrument',
handled: false,
data: {
function: 'addEventListener',
},
});
}
});
});
it(
optional('should fallback to <anonymous> fn name in mechanism data if one is unavailable', IS_LOADER),
function () {
return runInSandbox(sandbox, function () {
var div = document.createElement('div');
document.body.appendChild(div);
div.addEventListener(
'click',
function () {
foo();
},
false
);
var click = new MouseEvent('click');
div.dispatchEvent(click);
}).then(function (summary) {
if (IS_LOADER) {
// The async loader doesn't wrap
assert.ok(summary.events[0].exception.values[0].mechanism);
} else {
var target = summary.events[0].exception.values[0].mechanism.data.target;
delete summary.events[0].exception.values[0].mechanism.data.target;
// IE vs. Rest of the world
assert.oneOf(target, ['Node', 'EventTarget']);
assert.deepEqual(summary.events[0].exception.values[0].mechanism, {
type: 'instrument',
handled: false,
data: {
function: 'addEventListener',
handler: '<anonymous>',
},
});
}
});
}
);
});