From 5dbc07935d47eaad48ba0a7b1a816c644f9446cd Mon Sep 17 00:00:00 2001 From: David Sherret Date: Tue, 6 Dec 2022 15:58:18 -0500 Subject: [PATCH] fix(test): handle scenario where --trace-ops would cause an unhandled promise rejection (#16970) Closes #16969 --- cli/tests/test_tests.rs | 6 ++++++ .../testdata/test/trace_ops_caught_error/main.out | 6 ++++++ .../testdata/test/trace_ops_caught_error/main.ts | 12 ++++++++++++ core/01_core.js | 8 +++++--- 4 files changed, 29 insertions(+), 3 deletions(-) create mode 100644 cli/tests/testdata/test/trace_ops_caught_error/main.out create mode 100644 cli/tests/testdata/test/trace_ops_caught_error/main.ts diff --git a/cli/tests/test_tests.rs b/cli/tests/test_tests.rs index 92b1c33c8e..c8fa5b8e51 100644 --- a/cli/tests/test_tests.rs +++ b/cli/tests/test_tests.rs @@ -227,6 +227,12 @@ mod test { output: "test/ops_sanitizer_multiple_timeout_tests_no_trace.out", }); + itest!(trace_ops_catch_error { + args: "test -A --trace-ops test/trace_ops_caught_error/main.ts", + exit_code: 0, + output: "test/trace_ops_caught_error/main.out", + }); + // TODO(@littledivy): re-enable this test, recent optimizations made output non deterministic. // https://github.com/denoland/deno/issues/14268 // diff --git a/cli/tests/testdata/test/trace_ops_caught_error/main.out b/cli/tests/testdata/test/trace_ops_caught_error/main.out new file mode 100644 index 0000000000..192ed3a6d4 --- /dev/null +++ b/cli/tests/testdata/test/trace_ops_caught_error/main.out @@ -0,0 +1,6 @@ +Check file:///[WILDCARD]/trace_ops_caught_error/main.ts +running 1 test from ./test/trace_ops_caught_error/main.ts +handle thrown error in async function ... ok ([WILDCARD]) + +ok | 1 passed | 0 failed ([WILDCARD]) + diff --git a/cli/tests/testdata/test/trace_ops_caught_error/main.ts b/cli/tests/testdata/test/trace_ops_caught_error/main.ts new file mode 100644 index 0000000000..8194a8b2a1 --- /dev/null +++ b/cli/tests/testdata/test/trace_ops_caught_error/main.ts @@ -0,0 +1,12 @@ +Deno.test("handle thrown error in async function", async () => { + const dirPath = Deno.makeTempDirSync(); + const filePath = `${dirPath}/file.txt`; + try { + await Deno.stat(filePath); + } catch { + await Deno.writeTextFile(filePath, ""); + } finally { + await Deno.remove(filePath); + await Deno.remove(dirPath); + } +}); diff --git a/core/01_core.js b/core/01_core.js index 5df11c382c..02150674e0 100644 --- a/core/01_core.js +++ b/core/01_core.js @@ -187,8 +187,8 @@ // Rethrow the error throw err; } - handleOpCallTracing("${name}", id, promise); - promise[promiseIdSymbol] = id; + promise = handleOpCallTracing("${name}", id, promise); + promise[promiseIdSymbol] = id; return promise; } `, @@ -218,10 +218,12 @@ if (opCallTracingEnabled) { const stack = StringPrototypeSlice(new Error().stack, 6); MapPrototypeSet(opCallTraces, promiseId, { opName, stack }); - p = PromisePrototypeFinally( + return PromisePrototypeFinally( p, () => MapPrototypeDelete(opCallTraces, promiseId), ); + } else { + return p; } }