Skip to content

Lightpanda: the headless browser designed for AI and automation

dev: unhandled rejections in test now fail the test

Karl Seguinf470a760e816
diff --git a/src/browser/tests/testing.js b/src/browser/tests/testing.js
index 3e00fed4f..2eefa8411 100644
--- a/src/browser/tests/testing.js
+++ b/src/browser/tests/testing.js
@@ -19,6 +19,13 @@
reported_errors.push({event: e, script_id: _currentScriptId()});
});
+ // Same for a rejection nothing handled: a test that expects one cancels its
+ // unhandledrejection event.
+ let unhandled_rejections = [];
+ window.addEventListener('unhandledrejection', (e) => {
+ unhandled_rejections.push({event: e, script_id: _currentScriptId()});
+ });
+
// Runs fn, cancelling the errors it reports to window, and returns them.
function withReportedErrors(fn) {
const errors = [];
@@ -143,6 +150,12 @@
}
}
+ for (let {event, script_id} of unhandled_rejections) {
+ if (event.defaultPrevented === false) {
+ throw new Error(`script id: '${script_id}' unhandled rejection: ${event.reason}`);
+ }
+ }
+
if (async_pending.size > 0) {
return false;
}