Skip to content

Commit 47ade19

Browse files
committed
Catch errors on top window using uncaughtErrorFilter
Works around safari quirk when running in iframe
1 parent e306692 commit 47ade19

File tree

1 file changed

+21
-20
lines changed

1 file changed

+21
-20
lines changed

test/unit/strict-template-policy.html

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,6 @@
1818
import { setStrictTemplatePolicy } from '../../lib/utils/settings.js';
1919
import { Debouncer } from '../../lib/utils/debounce.js';
2020
setStrictTemplatePolicy(true);
21-
// Errors thrown in custom element reactions are not thrown up
22-
// the call stack to the dom methods that provoked them, so need
23-
// to catch them here and prevent mocha from complaining about them
24-
window.addEventListener('error', function(event) {
25-
event.stopImmediatePropagation();
26-
if (!window.globalError) {
27-
window.globalError = event;
28-
}
29-
});
3021
// Errors thrown in Polymer's debouncer queue get re-thrown
3122
// via setTimeout, making them particulary difficult to verify;
3223
// Wrap debouncer callbacks and store on the globalError to test later
@@ -36,7 +27,9 @@
3627
try {
3728
callback();
3829
} catch(error) {
39-
window.globalError = error;
30+
if (!window.uncaughtErrorFilter || !window.uncaughtErrorFilter(error)) {
31+
throw error;
32+
}
4033
}
4134
});
4235
};
@@ -72,26 +65,34 @@
7265
suite('strictTemplatePolicy', function() {
7366

7467
teardown(function() {
75-
window.globalError = null;
68+
window.uncaughtErrorFilter = window.top.uncaughtErrorFilter = null;
7669
});
7770

71+
// Errors thrown in custom element reactions are not thrown up
72+
// the call stack to the dom methods that provoked them, so this
73+
// wraps Chai's assert.throws to re-throw uncaught errors
7874
function assertThrows(fn, re) {
75+
let uncaughtError = null;
76+
// Catch uncaught errors; note when running in iframe sometimes
77+
// Safari errors are thrown on the top window, sometimes not, so
78+
// catch in both places
79+
window.uncaughtErrorFilter = window.top.uncaughtErrorFilter = function(err) {
80+
uncaughtError = err;
81+
return true;
82+
}
7983
assert.throws(function() {
8084
fn();
81-
// Throw any errors caught on window
82-
if (window.globalError) {
83-
throw new Error(window.globalError.message);
85+
// Re-throw any uncaughtErrors
86+
if (uncaughtError) {
87+
throw new Error(uncaughtError.message);
8488
}
8589
// Force polyfill reactions and/or async template stamping
8690
flush();
87-
// Throw any add'l errors caught on window
88-
if (window.globalError) {
89-
throw new Error(window.globalError.message);
91+
// Re-throw any uncaughtErrors
92+
if (uncaughtError) {
93+
throw new Error(uncaughtError.message);
9094
}
9195
}, re);
92-
if (HTMLTemplateElement.bootstrap) {
93-
HTMLTemplateElement.bootstrap();
94-
}
9596
}
9697

9798
test('dom-bind', function() {

0 commit comments

Comments
 (0)