From 7471587d29096a8de95a530f2132214ab9c08afa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bartek=20Iwa=C5=84czuk?= Date: Fri, 12 Jan 2024 23:10:42 +0100 Subject: feat: "rejectionhandled" Web event and "rejectionHandled" Node event (#21875) This commit adds support for "rejectionhandled" Web Event and "rejectionHandled" Node event. ```js import process from "node:process"; process.on("rejectionHandled", (promise) => { console.log("rejectionHandled", reason, promise); }); window.addEventListener("rejectionhandled", (event) => { console.log("rejectionhandled", event.reason, event.promise); }); ``` --------- Co-authored-by: Matt Mastracci --- cli/tests/integration/node_unit_tests.rs | 9 ++++++++ cli/tests/integration/run_tests.rs | 5 +++++ .../testdata/node/rejection_handled_web_process.ts | 26 ++++++++++++++++++++++ .../node/rejection_handled_web_process.ts.out | 7 ++++++ cli/tests/testdata/run/rejection_handled.out | 5 +++++ cli/tests/testdata/run/rejection_handled.ts | 17 ++++++++++++++ cli/tsc/dts/lib.deno.window.d.ts | 4 ++++ 7 files changed, 73 insertions(+) create mode 100644 cli/tests/testdata/node/rejection_handled_web_process.ts create mode 100644 cli/tests/testdata/node/rejection_handled_web_process.ts.out create mode 100644 cli/tests/testdata/run/rejection_handled.out create mode 100644 cli/tests/testdata/run/rejection_handled.ts (limited to 'cli') diff --git a/cli/tests/integration/node_unit_tests.rs b/cli/tests/integration/node_unit_tests.rs index 273066b09..351bf1eec 100644 --- a/cli/tests/integration/node_unit_tests.rs +++ b/cli/tests/integration/node_unit_tests.rs @@ -193,3 +193,12 @@ itest!(unhandled_rejection_web_process { envs: env_vars_for_npm_tests(), http_server: true, }); + +// Ensure that Web `onrejectionhandled` is fired before +// Node's `process.on('rejectionHandled')`. +itest!(rejection_handled_web_process { + args: "run -A node/rejection_handled_web_process.ts", + output: "node/rejection_handled_web_process.ts.out", + envs: env_vars_for_npm_tests(), + http_server: true, +}); diff --git a/cli/tests/integration/run_tests.rs b/cli/tests/integration/run_tests.rs index 2a349a5f2..999dc1177 100644 --- a/cli/tests/integration/run_tests.rs +++ b/cli/tests/integration/run_tests.rs @@ -3672,6 +3672,11 @@ itest!(unhandled_rejection_dynamic_import2 { output: "run/unhandled_rejection_dynamic_import2/main.ts.out", }); +itest!(rejection_handled { + args: "run --check run/rejection_handled.ts", + output: "run/rejection_handled.out", +}); + itest!(nested_error { args: "run run/nested_error/main.ts", output: "run/nested_error/main.ts.out", diff --git a/cli/tests/testdata/node/rejection_handled_web_process.ts b/cli/tests/testdata/node/rejection_handled_web_process.ts new file mode 100644 index 000000000..00d943feb --- /dev/null +++ b/cli/tests/testdata/node/rejection_handled_web_process.ts @@ -0,0 +1,26 @@ +import chalk from "npm:chalk"; +import process from "node:process"; + +console.log(chalk.red("Hello world!")); + +globalThis.addEventListener("unhandledrejection", (e) => { + console.log('globalThis.addEventListener("unhandledrejection");'); + e.preventDefault(); +}); + +globalThis.addEventListener("rejectionhandled", (_) => { + console.log("Web rejectionhandled"); +}); + +process.on("rejectionHandled", (_) => { + console.log("Node rejectionHandled"); +}); + +const a = Promise.reject(1); +setTimeout(() => { + a.catch(() => console.log("Added catch handler to the promise")); +}, 10); + +setTimeout(() => { + console.log("Success"); +}, 50); diff --git a/cli/tests/testdata/node/rejection_handled_web_process.ts.out b/cli/tests/testdata/node/rejection_handled_web_process.ts.out new file mode 100644 index 000000000..3a4e2ac23 --- /dev/null +++ b/cli/tests/testdata/node/rejection_handled_web_process.ts.out @@ -0,0 +1,7 @@ +[WILDCARD] +Hello world! +globalThis.addEventListener("unhandledrejection"); +Added catch handler to the promise +Web rejectionhandled +Node rejectionHandled +Success diff --git a/cli/tests/testdata/run/rejection_handled.out b/cli/tests/testdata/run/rejection_handled.out new file mode 100644 index 000000000..5c06fcd2b --- /dev/null +++ b/cli/tests/testdata/run/rejection_handled.out @@ -0,0 +1,5 @@ +[WILDCARD] +unhandledrejection 1 Promise { 1 } +Added catch handler to the promise +rejectionhandled 1 Promise { 1 } +Success diff --git a/cli/tests/testdata/run/rejection_handled.ts b/cli/tests/testdata/run/rejection_handled.ts new file mode 100644 index 000000000..f058ff966 --- /dev/null +++ b/cli/tests/testdata/run/rejection_handled.ts @@ -0,0 +1,17 @@ +window.addEventListener("unhandledrejection", (event) => { + console.log("unhandledrejection", event.reason, event.promise); + event.preventDefault(); +}); + +window.addEventListener("rejectionhandled", (event) => { + console.log("rejectionhandled", event.reason, event.promise); +}); + +const a = Promise.reject(1); +setTimeout(async () => { + a.catch(() => console.log("Added catch handler to the promise")); +}, 10); + +setTimeout(() => { + console.log("Success"); +}, 50); diff --git a/cli/tsc/dts/lib.deno.window.d.ts b/cli/tsc/dts/lib.deno.window.d.ts index c518c5356..eaab7c3c2 100644 --- a/cli/tsc/dts/lib.deno.window.d.ts +++ b/cli/tsc/dts/lib.deno.window.d.ts @@ -12,6 +12,7 @@ declare interface WindowEventMap { "error": ErrorEvent; "unhandledrejection": PromiseRejectionEvent; + "rejectionhandled": PromiseRejectionEvent; } /** @category Web APIs */ @@ -25,6 +26,9 @@ declare interface Window extends EventTarget { onunhandledrejection: | ((this: Window, ev: PromiseRejectionEvent) => any) | null; + onrejectionhandled: + | ((this: Window, ev: PromiseRejectionEvent) => any) + | null; close: () => void; readonly closed: boolean; alert: (message?: string) => void; -- cgit v1.2.3