From 47ade191debd1daa9d25846a3118119fd82e72ee Mon Sep 17 00:00:00 2001 From: Kevin Schaaf Date: Mon, 9 Jul 2018 21:31:29 -0700 Subject: [PATCH] Catch errors on top window using uncaughtErrorFilter Works around safari quirk when running in iframe --- test/unit/strict-template-policy.html | 41 ++++++++++++++------------- 1 file changed, 21 insertions(+), 20 deletions(-) diff --git a/test/unit/strict-template-policy.html b/test/unit/strict-template-policy.html index 955fdcd799..35c3e486ca 100644 --- a/test/unit/strict-template-policy.html +++ b/test/unit/strict-template-policy.html @@ -18,15 +18,6 @@ import { setStrictTemplatePolicy } from '../../lib/utils/settings.js'; import { Debouncer } from '../../lib/utils/debounce.js'; setStrictTemplatePolicy(true); - // Errors thrown in custom element reactions are not thrown up - // the call stack to the dom methods that provoked them, so need - // to catch them here and prevent mocha from complaining about them - window.addEventListener('error', function(event) { - event.stopImmediatePropagation(); - if (!window.globalError) { - window.globalError = event; - } - }); // Errors thrown in Polymer's debouncer queue get re-thrown // via setTimeout, making them particulary difficult to verify; // Wrap debouncer callbacks and store on the globalError to test later @@ -36,7 +27,9 @@ try { callback(); } catch(error) { - window.globalError = error; + if (!window.uncaughtErrorFilter || !window.uncaughtErrorFilter(error)) { + throw error; + } } }); }; @@ -72,26 +65,34 @@ suite('strictTemplatePolicy', function() { teardown(function() { - window.globalError = null; + window.uncaughtErrorFilter = window.top.uncaughtErrorFilter = null; }); + // Errors thrown in custom element reactions are not thrown up + // the call stack to the dom methods that provoked them, so this + // wraps Chai's assert.throws to re-throw uncaught errors function assertThrows(fn, re) { + let uncaughtError = null; + // Catch uncaught errors; note when running in iframe sometimes + // Safari errors are thrown on the top window, sometimes not, so + // catch in both places + window.uncaughtErrorFilter = window.top.uncaughtErrorFilter = function(err) { + uncaughtError = err; + return true; + } assert.throws(function() { fn(); - // Throw any errors caught on window - if (window.globalError) { - throw new Error(window.globalError.message); + // Re-throw any uncaughtErrors + if (uncaughtError) { + throw new Error(uncaughtError.message); } // Force polyfill reactions and/or async template stamping flush(); - // Throw any add'l errors caught on window - if (window.globalError) { - throw new Error(window.globalError.message); + // Re-throw any uncaughtErrors + if (uncaughtError) { + throw new Error(uncaughtError.message); } }, re); - if (HTMLTemplateElement.bootstrap) { - HTMLTemplateElement.bootstrap(); - } } test('dom-bind', function() {