From 1a788b58a0e80c4504a0fdf5d47db41c46dc8d37 Mon Sep 17 00:00:00 2001 From: David Sherret Date: Tue, 14 May 2024 19:51:51 -0400 Subject: chore: fix flaky rejection_handled_web_process (#23817) Closes https://github.com/denoland/deno/issues/23712 --- .../rejection_handled_web_process/__test__.jsonc | 6 ++++ .../rejection_handled_web_process.ts | 35 ++++++++++++++++++++++ .../rejection_handled_web_process.ts.out | 6 ++++ 3 files changed, 47 insertions(+) create mode 100644 tests/specs/node/rejection_handled_web_process/__test__.jsonc create mode 100644 tests/specs/node/rejection_handled_web_process/rejection_handled_web_process.ts create mode 100644 tests/specs/node/rejection_handled_web_process/rejection_handled_web_process.ts.out (limited to 'tests/specs') diff --git a/tests/specs/node/rejection_handled_web_process/__test__.jsonc b/tests/specs/node/rejection_handled_web_process/__test__.jsonc new file mode 100644 index 000000000..8f60df4f5 --- /dev/null +++ b/tests/specs/node/rejection_handled_web_process/__test__.jsonc @@ -0,0 +1,6 @@ +// Ensure that Web `onrejectionhandled` is fired before +// Node's `process.on('rejectionHandled')`. +{ + "args": "run -A --quiet rejection_handled_web_process.ts", + "output": "rejection_handled_web_process.ts.out" +} diff --git a/tests/specs/node/rejection_handled_web_process/rejection_handled_web_process.ts b/tests/specs/node/rejection_handled_web_process/rejection_handled_web_process.ts new file mode 100644 index 000000000..c7957082f --- /dev/null +++ b/tests/specs/node/rejection_handled_web_process/rejection_handled_web_process.ts @@ -0,0 +1,35 @@ +import chalk from "npm:chalk"; +import process from "node:process"; + +console.log(chalk.red("Hello world!")); + +const { promise, resolve } = Promise.withResolvers(); + +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"); + resolve(); +}); + +const a = Promise.reject(1); +setTimeout(() => { + a.catch(() => console.log("Added catch handler to the promise")); +}, 100); + +const exitTimeout = setTimeout(() => { + console.error("timeout expired"); + Deno.exit(1); +}, 30_000); + +promise.then(() => { + console.log("Success"); + clearTimeout(exitTimeout); +}); diff --git a/tests/specs/node/rejection_handled_web_process/rejection_handled_web_process.ts.out b/tests/specs/node/rejection_handled_web_process/rejection_handled_web_process.ts.out new file mode 100644 index 000000000..e6fefede2 --- /dev/null +++ b/tests/specs/node/rejection_handled_web_process/rejection_handled_web_process.ts.out @@ -0,0 +1,6 @@ +Hello world! +globalThis.addEventListener("unhandledrejection"); +Added catch handler to the promise +Web rejectionhandled +Node rejectionHandled +Success -- cgit v1.2.3