Lightpanda: the headless browser designed for AI and automation
Narrow uncaught error window with a testing.withReportedErrors capture
Karl Seguinffd7fd3e410d
diff --git a/src/browser/tests/custom_elements/clone_upgrade.html b/src/browser/tests/custom_elements/clone_upgrade.htmlindex 736161d62..09418daa1 100644--- a/src/browser/tests/custom_elements/clone_upgrade.html+++ b/src/browser/tests/custom_elements/clone_upgrade.html@@ -124,7 +124,6 @@</script><script id=failed_clone_upgrade_keeps_the_copied_node>- testing.expectUncaughtErrors();{let cloning = false;let constructed;diff --git a/src/browser/tests/custom_elements/constructor.html b/src/browser/tests/custom_elements/constructor.htmlindex 316dfff54..22d8f7bfb 100644--- a/src/browser/tests/custom_elements/constructor.html+++ b/src/browser/tests/custom_elements/constructor.html@@ -91,7 +91,6 @@<div id=clone_container></div><script id=clone>- testing.expectUncaughtErrors();{let calls = 0;class MyCloneElementA extends HTMLElement {@@ -102,7 +101,7 @@}}customElements.define('my-clone_element_a', MyCloneElementA);- const original = document.createElement('my-clone_element_a');+ const original = new MyCloneElementA();$('#clone_container').cloneNode(true);testing.expectEqual(2, calls);}@@ -111,7 +110,6 @@<div id=clone_siblings_container></div><script id=clone_self_appending_siblings>- testing.expectUncaughtErrors();{// Two trailing constructors append into the list being cloned.let calls = 0;@@ -124,8 +122,8 @@}}customElements.define('my-clone_element_b', MyCloneElementB);- document.createElement('my-clone_element_b');- document.createElement('my-clone_element_b');+ new MyCloneElementB();+ new MyCloneElementB();testing.expectEqual(2, calls);$('#clone_siblings_container').cloneNode(true);testing.expectEqual(4, calls);@@ -136,7 +134,6 @@<div id=fragment_clone_container></div><script id=clone_fragment>- testing.expectUncaughtErrors();{let calls = 0;class MyFragmentCloneElement extends HTMLElement {@@ -150,7 +147,7 @@// Create a DocumentFragment with a custom elementconst fragment = document.createDocumentFragment();- const customEl = document.createElement('my-fragment-clone-element');+ const customEl = new MyFragmentCloneElement();fragment.appendChild(customEl);// Clone the fragment - this should trigger the crash@@ -163,7 +160,6 @@<div id=range_clone_container></div><script id=clone_range>- testing.expectUncaughtErrors();{let calls = 0;class MyRangeCloneElement extends HTMLElement {@@ -177,7 +173,7 @@// Create a container with a custom elementconst container = document.createElement('div');- const customEl = document.createElement('my-range-clone-element');+ const customEl = new MyRangeCloneElement();container.appendChild(customEl);// Create a range that includes the custom elementdiff --git a/src/browser/tests/custom_elements/context_element_not_reconstructed.html b/src/browser/tests/custom_elements/context_element_not_reconstructed.htmlindex abe77856c..cc355c3f6 100644--- a/src/browser/tests/custom_elements/context_element_not_reconstructed.html+++ b/src/browser/tests/custom_elements/context_element_not_reconstructed.html@@ -2,7 +2,6 @@<script src="../testing.js"></script><script id="constructor_sets_innerhtml">- testing.expectUncaughtErrors();{// A custom element whose constructor sets innerHTML. The innerHTML// setter fragment-parses with this element as the parsing context.@@ -21,13 +20,11 @@}customElements.define('ce-ctor-innerhtml', CeCtorInnerHtml);- // Adding children during construction violates a post-condition of- // "create an element", so the result is a fallback HTMLUnknownElement- // rather than the constructed instance. What matters here is that the- // constructor ran exactly once instead of recursing.- const el = document.createElement('ce-ctor-innerhtml');+ // What matters here is that the constructor ran exactly once instead+ // of recursing.+ const el = new CeCtorInnerHtml();testing.expectEqual(1, calls);- testing.expectEqual(true, el instanceof HTMLUnknownElement);+ testing.expectEqual('<span>hi</span>', el.innerHTML);}</script>diff --git a/src/browser/tests/custom_elements/create_element_post_conditions.html b/src/browser/tests/custom_elements/create_element_post_conditions.htmlindex 8f988eed0..91b64ecba 100644--- a/src/browser/tests/custom_elements/create_element_post_conditions.html+++ b/src/browser/tests/custom_elements/create_element_post_conditions.html@@ -1,6 +1,5 @@<!DOCTYPE html><script src="../testing.js"></script>-<script>testing.expectUncaughtErrors();</script><script id="uses_constructor_return_value">{@@ -37,7 +36,9 @@}customElements.define('ce-adds-child', AddsChild);- const el = document.createElement('ce-adds-child');+ let el;+ const errors = testing.withReportedErrors(() => el = document.createElement('ce-adds-child'));+ testing.expectEqual(['NotSupportedError'], errors.map((e) => e.name));testing.expectEqual(true, el instanceof HTMLUnknownElement);testing.expectEqual('ce-adds-child', el.localName);}@@ -71,7 +72,10 @@}customElements.define('ce-adds-attribute', AddsAttribute);- testing.expectEqual(true, document.createElement('ce-adds-attribute') instanceof HTMLUnknownElement);+ let el;+ const errors = testing.withReportedErrors(() => el = document.createElement('ce-adds-attribute'));+ testing.expectEqual(['NotSupportedError'], errors.map((e) => e.name));+ testing.expectEqual(true, el instanceof HTMLUnknownElement);}</script>@@ -89,7 +93,9 @@}customElements.define('ce-attaches-self', AttachesSelf);- const el = document.createElement('ce-attaches-self');+ let el;+ const errors = testing.withReportedErrors(() => el = document.createElement('ce-attaches-self'));+ testing.expectEqual(['NotSupportedError'], errors.map((e) => e.name));testing.expectEqual(true, el instanceof HTMLUnknownElement);testing.expectEqual(null, el.parentNode);}@@ -109,7 +115,10 @@customElements.define('ce-parser-attaches-self', ParserAttachesSelf);const container = document.createElement('div');- container.innerHTML = '<ce-parser-attaches-self></ce-parser-attaches-self>';+ const errors = testing.withReportedErrors(() => {+ container.innerHTML = '<ce-parser-attaches-self></ce-parser-attaches-self>';+ });+ testing.expectEqual(['NotSupportedError'], errors.map((e) => e.name));testing.expectEqual(1, container.children.length);testing.expectEqual(true, container.firstElementChild instanceof HTMLUnknownElement);}diff --git a/src/browser/tests/custom_elements/error_reporting.html b/src/browser/tests/custom_elements/error_reporting.htmlindex 9ab2d8d0b..0b608c671 100644--- a/src/browser/tests/custom_elements/error_reporting.html+++ b/src/browser/tests/custom_elements/error_reporting.html@@ -1,6 +1,5 @@<!DOCTYPE html><script src="../testing.js"></script>-<script>testing.expectUncaughtErrors();</script><script id="create_element_reports_thrown_exception">{diff --git a/src/browser/tests/custom_elements/registry.html b/src/browser/tests/custom_elements/registry.htmlindex 7bc5afce2..0640b851b 100644--- a/src/browser/tests/custom_elements/registry.html+++ b/src/browser/tests/custom_elements/registry.html@@ -120,7 +120,6 @@</script><script id="constructor_self_insert_foster_parent">-testing.expectUncaughtErrors();{// Regression: custom element constructor inserting itself (via appendChild) during// innerHTML parsing. When the element is not valid table content, the HTML5 parser@@ -145,7 +144,13 @@ testing.expectUncaughtErrors();// ctor-self-insert is not valid table content; the parser foster-parents it// before the <table>, calling appendBeforeSiblingCallback(sibling=table, node=element).// At that point the element already has _parent=container from the constructor.- container.innerHTML = '<table><ctor-self-insert></ctor-self-insert></table>';+ // TODO: we construct synchronously during fragment parsing, so the parent+ // fails the create-an-element post-conditions. Per spec (and Firefox) the+ // element is upgraded after insertion instead, and nothing is reported.+ const errors = testing.withReportedErrors(() => {+ container.innerHTML = '<table><ctor-self-insert></ctor-self-insert></table>';+ });+ testing.expectEqual(['NotSupportedError'], errors.map((e) => e.name));testing.expectEqual(1, constructorCalled);}diff --git a/src/browser/tests/element/html/script/load_event.html b/src/browser/tests/element/html/script/load_event.htmlindex 9e0f09191..4e414b6ac 100644--- a/src/browser/tests/element/html/script/load_event.html+++ b/src/browser/tests/element/html/script/load_event.html@@ -1,7 +1,6 @@<!DOCTYPE html><head></head><script src="../../../testing.js"></script>-<script>testing.expectUncaughtErrors();</script><!--A <script> fires load/error at its own element only when it is "from an@@ -28,6 +27,14 @@<!-- an inline script that throws reports to window.onerror, it does not firean error event at its element -->+<script>+ window.addEventListener('error', (e) => {+ const msg = e.error?.message;+ if (msg === 'deliberate' || msg === 'external script that throws still fires load, not error') {+ e.preventDefault();+ }+ });+</script><script>throw new Error('deliberate');</script><script id=inline_fires_nothing>diff --git a/src/browser/tests/event/report_error.html b/src/browser/tests/event/report_error.htmlindex ade46af9b..cd5f96c01 100644--- a/src/browser/tests/event/report_error.html+++ b/src/browser/tests/event/report_error.html@@ -1,6 +1,5 @@<!DOCTYPE html><script src="../testing.js"></script>-<script>testing.expectUncaughtErrors();</script><script id=onerrorFiveArguments>let called = false;@@ -56,6 +55,7 @@if (typeof msg === 'string' && msg.startsWith('cb-')) {seen.push(msg);thrown[msg] = e.error instanceof Error;+ e.preventDefault();}};window.addEventListener('error', onError);diff --git a/src/browser/tests/events.html b/src/browser/tests/events.htmlindex 542921e48..95860c038 100644--- a/src/browser/tests/events.html+++ b/src/browser/tests/events.html@@ -811,7 +811,6 @@<div id=throw_host><div id=throw_mid><span id=throw_leaf></span></div></div><script id=listenerThrowDoesNotHaltDispatch>- testing.expectUncaughtErrors();// Per DOM §2.9 step 4 substep 8 "Inner invoke", a listener callback that// throws must have its exception reported, not propagated — subsequent// listeners on the same target and the rest of the propagation path must@@ -833,7 +832,11 @@// dispatchEvent must NOT throw to the caller — if it did, this assignment// would be skipped and the next testing.expectEqual would never run.- const result = leaf.dispatchEvent(new Event('throw_test', {bubbles: true}));+ let result;+ const errors = testing.withReportedErrors(() => {+ result = leaf.dispatchEvent(new Event('throw_test', {bubbles: true}));+ });+ testing.expectEqual(['listener-throw probe'], errors.map((e) => e.message));testing.expectEqual('leaf-throwing', hits[0]);testing.expectEqual('leaf-sibling', hits[1]);@@ -847,7 +850,6 @@<div id=throw_capture_host><div id=throw_capture_mid><span id=throw_capture_leaf></span></div></div><script id=listenerThrowDuringCapturePhase>- testing.expectUncaughtErrors();// Capture-phase variant: a throwing capture listener on an ancestor must// not skip the rest of the capture walk, the at-target listener, or the// bubble walk.@@ -866,7 +868,8 @@mid.addEventListener('cap_test', () => order.push('mid-bubble'));host.addEventListener('cap_test', () => order.push('host-bubble'));- leaf.dispatchEvent(new Event('cap_test', {bubbles: true}));+ const errors = testing.withReportedErrors(() => leaf.dispatchEvent(new Event('cap_test', {bubbles: true})));+ testing.expectEqual(['capture-throw probe'], errors.map((e) => e.message));testing.expectEqual('host-capture-throwing', order[0]);testing.expectEqual('mid-capture', order[1]);@@ -878,7 +881,6 @@</script><script id=listenerThrowOnWindowTarget>- testing.expectUncaughtErrors();// Direct-dispatch variant (non-DOM target — Window). Same contract: a// throwing listener must not stop later listeners from running.{@@ -891,7 +893,8 @@window.addEventListener('win_throw', b);window.addEventListener('win_throw', c);- window.dispatchEvent(new Event('win_throw'));+ const errors = testing.withReportedErrors(() => window.dispatchEvent(new Event('win_throw')));+ testing.expectEqual(['window-throw probe'], errors.map((e) => e.message));testing.expectEqual('a-throwing', calls[0]);testing.expectEqual('b', calls[1]);diff --git a/src/browser/tests/testing.js b/src/browser/tests/testing.jsindex 31860bc03..3e00fed4f 100644--- a/src/browser/tests/testing.js+++ b/src/browser/tests/testing.js@@ -11,32 +11,28 @@// runner will wait until this is empty (or timeout)let async_pending = new Set();- // tests that throw uncaught errors on purpose, see expectUncaughtErrors- let uncaught_errors_page = false;- let uncaught_errors_scripts = new Set();-// A script that throws after an expect* passed would otherwise count as ok.+ // Checked in assertOk, once dispatch is over: a test that expects the error+ // cancels it (window.onerror returning true, or preventDefault()).+ let reported_errors = [];window.addEventListener('error', (e) => {- const script_id = _currentScriptId();- if (uncaught_errors_page || uncaught_errors_scripts.has(script_id)) {- return;- }- failed = true;- if (script_id) {- observed_ids[script_id] = 'fail';- }- console.error(`uncaught error: ${e.message}\n script_id: ${script_id}`);+ reported_errors.push({event: e, script_id: _currentScriptId()});});- // Allows uncaught errors from the calling <script id=...>, or from the whole- // page when called from a script without an id.- function expectUncaughtErrors() {- const script_id = _currentScriptId();- if (script_id) {- uncaught_errors_scripts.add(script_id);- } else {- uncaught_errors_page = true;+ // Runs fn, cancelling the errors it reports to window, and returns them.+ function withReportedErrors(fn) {+ const errors = [];+ const onError = (e) => {+ errors.push(e.error);+ e.preventDefault();+ };+ window.addEventListener('error', onError);+ try {+ fn();+ } finally {+ window.removeEventListener('error', onError);}+ return errors;}function expectTrue(actual) {@@ -141,6 +137,12 @@throw new Error('Failed');}+ for (let {event, script_id} of reported_errors) {+ if (event.defaultPrevented === false) {+ throw new Error(`script id: '${script_id}' uncaught error: ${event.message}`);+ }+ }+if (async_pending.size > 0) {return false;}@@ -187,7 +189,7 @@expectEqual: expectEqual,expectError: expectError,withError: withError,- expectUncaughtErrors: expectUncaughtErrors,+ withReportedErrors: withReportedErrors,printTimeoutState: printTimeoutState,onload: onload,IS_TEST_RUNNER: IS_TEST_RUNNER,diff --git a/src/browser/tests/window/onerror.html b/src/browser/tests/window/onerror.htmlindex 827bfd0d6..89174cd79 100644--- a/src/browser/tests/window/onerror.html+++ b/src/browser/tests/window/onerror.html@@ -1,6 +1,5 @@<!DOCTYPE html><script src="../testing.js"></script>-<script>testing.expectUncaughtErrors();</script><script id=onerrorBasicCallback>{@@ -10,6 +9,7 @@window.onerror = function(message, source, lineno, colno, error) {callbackCalled = true;receivedArgs = { message, source, lineno, colno, error };+ return true;};const err = new Error('Test error');@@ -29,6 +29,7 @@window.onerror = function() {argCount = arguments.length;+ return true;};window.reportError(new Error('Five args test'));@@ -71,6 +72,7 @@window.onerror = function() {onerrorCalled = true;+ return true;};const listener = function() {@@ -94,6 +96,7 @@window.onerror = function() {callOrder.push('onerror');+ return true;};const listener = function() {@@ -153,6 +156,7 @@colno: typeof colno,error: typeof error};+ return true;};window.reportError(new Error('Type check'));@@ -177,6 +181,7 @@const listener = function(e) {eventDefaultPrevented = e.defaultPrevented;+ e.preventDefault();};window.addEventListener('error', listener);diff --git a/src/browser/tests/window/report_error.html b/src/browser/tests/window/report_error.htmlindex 55d3ffa2f..eb4b1124d 100644--- a/src/browser/tests/window/report_error.html+++ b/src/browser/tests/window/report_error.html@@ -1,6 +1,5 @@<!DOCTYPE html><script src="../testing.js"></script>-<script>testing.expectUncaughtErrors();</script><script id=reportErrorBasic>{@@ -8,6 +7,7 @@let capturedEvent = null;window.addEventListener('error', (e) => {+ e.preventDefault();errorEventFired = true;capturedEvent = e;});@@ -25,6 +25,7 @@{let evt = null;window.addEventListener('error', (e) => {+ e.preventDefault();evt = e;});@@ -64,7 +65,8 @@parentCalled = true;});- window.addEventListener('error', () => {+ window.addEventListener('error', (e) => {+ e.preventDefault();windowCalled = true;});@@ -80,6 +82,7 @@let evt = null;window.addEventListener('error', (e) => {+ e.preventDefault();evt = e;});@@ -96,7 +99,8 @@{let callCount = 0;- window.addEventListener('error', () => {+ window.addEventListener('error', (e) => {+ e.preventDefault();callCount++;});@@ -113,6 +117,7 @@let evt = null;window.addEventListener('error', (e) => {+ e.preventDefault();evt = e;});@@ -131,6 +136,7 @@let evt = null;window.addEventListener('error', (e) => {+ e.preventDefault();eventFired = true;evt = e;});@@ -151,6 +157,7 @@let evt = null;window.addEventListener('error', (e) => {+ e.preventDefault();eventFired = true;evt = e;});