|
18 | 18 | import { setStrictTemplatePolicy } from '../../lib/utils/settings.js'; |
19 | 19 | import { Debouncer } from '../../lib/utils/debounce.js'; |
20 | 20 | 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 | | - }); |
30 | 21 | // Errors thrown in Polymer's debouncer queue get re-thrown |
31 | 22 | // via setTimeout, making them particulary difficult to verify; |
32 | 23 | // Wrap debouncer callbacks and store on the globalError to test later |
|
36 | 27 | try { |
37 | 28 | callback(); |
38 | 29 | } catch(error) { |
39 | | - window.globalError = error; |
| 30 | + if (!window.uncaughtErrorFilter || !window.uncaughtErrorFilter(error)) { |
| 31 | + throw error; |
| 32 | + } |
40 | 33 | } |
41 | 34 | }); |
42 | 35 | }; |
|
72 | 65 | suite('strictTemplatePolicy', function() { |
73 | 66 |
|
74 | 67 | teardown(function() { |
75 | | - window.globalError = null; |
| 68 | + window.uncaughtErrorFilter = window.top.uncaughtErrorFilter = null; |
76 | 69 | }); |
77 | 70 |
|
| 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 |
78 | 74 | 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 | + } |
79 | 83 | assert.throws(function() { |
80 | 84 | 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); |
84 | 88 | } |
85 | 89 | // Force polyfill reactions and/or async template stamping |
86 | 90 | 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); |
90 | 94 | } |
91 | 95 | }, re); |
92 | | - if (HTMLTemplateElement.bootstrap) { |
93 | | - HTMLTemplateElement.bootstrap(); |
94 | | - } |
95 | 96 | } |
96 | 97 |
|
97 | 98 | test('dom-bind', function() { |
|
0 commit comments