/
error_handler_test.js
522 lines (415 loc) · 16 KB
/
error_handler_test.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
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
import Ember from 'ember';
import { run } from 'ember-metal';
import { DEBUG } from 'ember-env-flags';
const ONERROR = Ember.onerror;
const ADAPTER = Ember.Test && Ember.Test.adapter;
const TESTING = Ember.testing;
let WINDOW_ONERROR;
QUnit.module('error_handler', {
setup() {
// capturing this outside of module scope to ensure we grab
// the test frameworks own window.onerror to reset it
WINDOW_ONERROR = window.onerror;
},
teardown() {
Ember.onerror = ONERROR;
Ember.testing = TESTING;
window.onerror = WINDOW_ONERROR;
if (Ember.Test) {
Ember.Test.adapter = ADAPTER;
}
}
});
function runThatThrowsSync(message = 'Error for testing error handling') {
return run(() => {
throw new Error(message);
});
}
function runThatThrowsAsync(message = 'Error for testing error handling') {
return run.next(() => {
throw new Error(message);
});
}
test('by default there is no onerror - sync run', function(assert) {
assert.strictEqual(Ember.onerror, undefined, 'precond - there should be no Ember.onerror set by default');
assert.throws(runThatThrowsSync, Error, 'errors thrown sync are catchable');
});
test('when Ember.onerror (which rethrows) is registered - sync run', function(assert) {
assert.expect(2);
Ember.onerror = function(error) {
assert.ok(true, 'onerror called');
throw error;
};
assert.throws(runThatThrowsSync, Error, 'error is thrown');
});
test('when Ember.onerror (which does not rethrow) is registered - sync run', function(assert) {
assert.expect(2);
Ember.onerror = function(error) {
assert.ok(true, 'onerror called');
};
runThatThrowsSync();
assert.ok(true, 'no error was thrown, Ember.onerror can intercept errors');
});
if (DEBUG) {
test('when Ember.Test.adapter is registered and error is thrown - sync run', function(assert) {
assert.expect(1);
Ember.Test.adapter = {
exception(error) {
assert.notOk(true, 'adapter is not called for errors thrown in sync run loops');
}
};
assert.throws(runThatThrowsSync, Error);
});
test('when both Ember.onerror (which rethrows) and Ember.Test.adapter are registered - sync run', function(assert) {
assert.expect(2);
Ember.Test.adapter = {
exception(error) {
assert.notOk(true, 'adapter is not called for errors thrown in sync run loops');
}
};
Ember.onerror = function(error) {
assert.ok(true, 'onerror is called for sync errors even if Ember.Test.adapter is setup');
throw error;
};
assert.throws(runThatThrowsSync, Error, 'error is thrown');
});
test('when both Ember.onerror (which does not rethrow) and Ember.Test.adapter are registered - sync run', function(assert) {
assert.expect(2);
Ember.Test.adapter = {
exception(error) {
assert.notOk(true, 'adapter is not called for errors thrown in sync run loops');
}
};
Ember.onerror = function(error) {
assert.ok(true, 'onerror is called for sync errors even if Ember.Test.adapter is setup');
};
runThatThrowsSync();
assert.ok(true, 'no error was thrown, Ember.onerror can intercept errors');
});
QUnit.test('when Ember.Test.adapter is registered and error is thrown - async run', function(assert) {
assert.expect(3);
let done = assert.async();
let caughtInAdapter, caughtInCatch, caughtByWindowOnerror;
Ember.Test.adapter = {
exception(error) {
caughtInAdapter = error;
}
};
window.onerror = function(message) {
caughtByWindowOnerror = message;
// prevent "bubbling" and therefore failing the test
return true;
};
try {
runThatThrowsAsync();
} catch(e) {
caughtInCatch = e;
}
setTimeout(() => {
assert.equal(caughtInAdapter, undefined, 'test adapter should never catch errors in run loops');
assert.equal(caughtInCatch, undefined, 'a "normal" try/catch should never catch errors in an async run');
assert.pushResult({
result: /Error for testing error handling/.test(caughtByWindowOnerror),
actual: caughtByWindowOnerror,
expected: 'to include `Error for testing error handling`',
message: 'error should bubble out to window.onerror, and therefore fail tests (due to QUnit implementing window.onerror)'
});
done();
}, 20);
});
test('when both Ember.onerror and Ember.Test.adapter are registered - async run', function(assert) {
assert.expect(1);
let done = assert.async();
Ember.Test.adapter = {
exception(error) {
assert.notOk(true, 'Adapter.exception is not called for errors thrown in run.next');
}
};
Ember.onerror = function(error) {
assert.ok(true, 'onerror is invoked for errors thrown in run.next/run.later');
};
runThatThrowsAsync();
setTimeout(done, 10);
});
}
QUnit.test('does not swallow exceptions by default (Ember.testing = true, no Ember.onerror) - sync run', function(assert) {
Ember.testing = true;
let error = new Error('the error');
assert.throws(() => {
Ember.run(() => {
throw error;
});
}, error);
});
QUnit.test('does not swallow exceptions by default (Ember.testing = false, no Ember.onerror) - sync run', function(assert) {
Ember.testing = false;
let error = new Error('the error');
assert.throws(() => {
Ember.run(() => {
throw error;
});
}, error);
});
QUnit.test('does not swallow exceptions (Ember.testing = false, Ember.onerror which rethrows) - sync run', function(assert) {
assert.expect(2);
Ember.testing = false;
Ember.onerror = function(error) {
assert.ok(true, 'Ember.onerror was called');
throw error;
};
let error = new Error('the error');
assert.throws(() => {
Ember.run(() => {
throw error;
});
}, error);
});
QUnit.test('Ember.onerror can intercept errors (aka swallow) by not rethrowing (Ember.testing = false) - sync run', function(assert) {
assert.expect(1);
Ember.testing = false;
Ember.onerror = function(error) {
assert.ok(true, 'Ember.onerror was called');
};
let error = new Error('the error');
try {
Ember.run(() => {
throw error;
});
} catch(e) {
assert.notOk(true, 'Ember.onerror that does not rethrow is intentionally swallowing errors, try / catch wrapping does not see error');
}
});
QUnit.test('does not swallow exceptions by default (Ember.testing = true, no Ember.onerror) - async run', function(assert) {
let done = assert.async();
let caughtByWindowOnerror;
Ember.testing = true;
window.onerror = function(message) {
caughtByWindowOnerror = message;
// prevent "bubbling" and therefore failing the test
return true;
};
Ember.run.later(() => {
throw new Error('the error');
}, 10);
setTimeout(() => {
assert.pushResult({
result: /the error/.test(caughtByWindowOnerror),
actual: caughtByWindowOnerror,
expected: 'to include `the error`',
message: 'error should bubble out to window.onerror, and therefore fail tests (due to QUnit implementing window.onerror)'
});
done();
}, 20);
});
QUnit.test('does not swallow exceptions by default (Ember.testing = false, no Ember.onerror) - async run', function(assert) {
let done = assert.async();
let caughtByWindowOnerror;
Ember.testing = false;
window.onerror = function(message) {
caughtByWindowOnerror = message;
// prevent "bubbling" and therefore failing the test
return true;
};
Ember.run.later(() => {
throw new Error('the error');
}, 10);
setTimeout(() => {
assert.pushResult({
result: /the error/.test(caughtByWindowOnerror),
actual: caughtByWindowOnerror,
expected: 'to include `the error`',
message: 'error should bubble out to window.onerror, and therefore fail tests (due to QUnit implementing window.onerror)'
});
done();
}, 20);
});
QUnit.test('Ember.onerror can intercept errors (aka swallow) by not rethrowing (Ember.testing = false) - async run', function(assert) {
let done = assert.async();
Ember.testing = false;
window.onerror = function(message) {
assert.notOk(true, 'window.onerror is never invoked when Ember.onerror intentionally swallows errors');
// prevent "bubbling" and therefore failing the test
return true;
};
let thrown = new Error('the error');
Ember.onerror = function(error) {
assert.strictEqual(error, thrown, 'Ember.onerror is called with the error');
};
Ember.run.later(() => {
throw thrown;
}, 10);
setTimeout(done, 20);
});
function generateRSVPErrorHandlingTests(message, generatePromise, timeout = 10) {
test(`${message} when Ember.onerror which does not rethrow is present - rsvp`, function(assert) {
assert.expect(1);
let thrown = new Error('the error');
Ember.onerror = function(error) {
assert.strictEqual(error, thrown, 'Ember.onerror is called for errors thrown in RSVP promises');
};
generatePromise(thrown);
// RSVP.Promise's are configured to settle within the run loop, this
// ensures that run loop has completed
return new Ember.RSVP.Promise((resolve) => setTimeout(resolve, timeout));
});
test(`${message} when Ember.onerror which does rethrow is present - rsvp`, function(assert) {
assert.expect(2);
let thrown = new Error('the error');
Ember.onerror = function(error) {
assert.strictEqual(error, thrown, 'Ember.onerror is called for errors thrown in RSVP promises');
throw error;
};
window.onerror = function(message) {
assert.pushResult({
result: /the error/.test(message),
actual: message,
expected: 'to include `the error`',
message: 'error should bubble out to window.onerror, and therefore fail tests (due to QUnit implementing window.onerror)'
});
// prevent "bubbling" and therefore failing the test
return true;
};
generatePromise(thrown);
// RSVP.Promise's are configured to settle within the run loop, this
// ensures that run loop has completed
return new Ember.RSVP.Promise((resolve) => setTimeout(resolve, timeout));
});
test(`${message} when Ember.onerror which does not rethrow is present (Ember.testing = false) - rsvp`, function(assert) {
assert.expect(1);
Ember.testing = false;
let thrown = new Error('the error');
Ember.onerror = function(error) {
assert.strictEqual(error, thrown, 'Ember.onerror is called for errors thrown in RSVP promises');
};
generatePromise(thrown);
// RSVP.Promise's are configured to settle within the run loop, this
// ensures that run loop has completed
return new Ember.RSVP.Promise((resolve) => setTimeout(resolve, timeout));
});
test(`${message} when Ember.onerror which does rethrow is present (Ember.testing = false) - rsvp`, function(assert) {
assert.expect(2);
Ember.testing = false;
let thrown = new Error('the error');
Ember.onerror = function(error) {
assert.strictEqual(error, thrown, 'Ember.onerror is called for errors thrown in RSVP promises');
throw error;
};
window.onerror = function(message) {
assert.pushResult({
result: /the error/.test(message),
actual: message,
expected: 'to include `the error`',
message: 'error should bubble out to window.onerror, and therefore fail tests (due to QUnit implementing window.onerror)'
});
// prevent "bubbling" and therefore failing the test
return true;
};
generatePromise(thrown);
// RSVP.Promise's are configured to settle within the run loop, this
// ensures that run loop has completed
return new Ember.RSVP.Promise((resolve) => setTimeout(resolve, timeout));
});
if (DEBUG) {
test(`${message} when Ember.Test.adapter without \`exception\` method is present - rsvp`, function(assert) {
assert.expect(1);
let thrown = new Error('the error');
Ember.Test.adapter = Ember.Test.QUnitAdapter.create({
exception: undefined
});
window.onerror = function(message) {
assert.pushResult({
result: /the error/.test(message),
actual: message,
expected: 'to include `the error`',
message: 'error should bubble out to window.onerror, and therefore fail tests (due to QUnit implementing window.onerror)'
});
// prevent "bubbling" and therefore failing the test
return true;
};
generatePromise(thrown);
// RSVP.Promise's are configured to settle within the run loop, this
// ensures that run loop has completed
return new Ember.RSVP.Promise((resolve) => setTimeout(resolve, timeout));
});
test(`${message} when both Ember.onerror and Ember.Test.adapter without \`exception\` method are present - rsvp`, function(assert) {
assert.expect(1);
let thrown = new Error('the error');
Ember.Test.adapter = Ember.Test.QUnitAdapter.create({
exception: undefined
});
Ember.onerror = function(error) {
assert.pushResult({
result: /the error/.test(error.message),
actual: error.message,
expected: 'to include `the error`',
message: 'error should bubble out to window.onerror, and therefore fail tests (due to QUnit implementing window.onerror)'
});
};
generatePromise(thrown);
// RSVP.Promise's are configured to settle within the run loop, this
// ensures that run loop has completed
return new Ember.RSVP.Promise((resolve) => setTimeout(resolve, timeout));
});
test(`${message} when Ember.Test.adapter is present - rsvp`, function(assert) {
assert.expect(1);
let thrown = new Error('the error');
Ember.Test.adapter = Ember.Test.QUnitAdapter.create({
exception(error) {
assert.strictEqual(error, thrown, 'Adapter.exception is called for errors thrown in RSVP promises');
}
});
generatePromise(thrown);
// RSVP.Promise's are configured to settle within the run loop, this
// ensures that run loop has completed
return new Ember.RSVP.Promise((resolve) => setTimeout(resolve, timeout));
});
test(`${message} when both Ember.onerror and Ember.Test.adapter are present - rsvp`, function(assert) {
assert.expect(1);
let thrown = new Error('the error');
Ember.Test.adapter = Ember.Test.QUnitAdapter.create({
exception(error) {
assert.strictEqual(error, thrown, 'Adapter.exception is called for errors thrown in RSVP promises');
}
});
Ember.onerror = function(error) {
assert.notOk(true, 'Ember.onerror is not called if Test.adapter does not rethrow');
};
generatePromise(thrown);
// RSVP.Promise's are configured to settle within the run loop, this
// ensures that run loop has completed
return new Ember.RSVP.Promise((resolve) => setTimeout(resolve, timeout));
});
test(`${message} when both Ember.onerror and Ember.Test.adapter are present - rsvp`, function(assert) {
assert.expect(2);
let thrown = new Error('the error');
Ember.Test.adapter = Ember.Test.QUnitAdapter.create({
exception(error) {
assert.strictEqual(error, thrown, 'Adapter.exception is called for errors thrown in RSVP promises');
throw error;
}
});
Ember.onerror = function(error) {
assert.strictEqual(error, thrown, 'Ember.onerror is called for errors thrown in RSVP promises if Test.adapter rethrows');
};
generatePromise(thrown);
// RSVP.Promise's are configured to settle within the run loop, this
// ensures that run loop has completed
return new Ember.RSVP.Promise((resolve) => setTimeout(resolve, timeout));
});
}
}
generateRSVPErrorHandlingTests('errors in promise constructor', (error) => {
new Ember.RSVP.Promise(() => {
throw error;
});
});
generateRSVPErrorHandlingTests('errors in promise .then callback', (error) => {
Ember.RSVP.resolve().then(() => {
throw error;
});
});
generateRSVPErrorHandlingTests('errors in async promise .then callback', (error) => {
new Ember.RSVP.Promise((resolve) => setTimeout(resolve, 10)).then(() => {
throw error;
});
}, 20);