From 96530df7fb1b8c6bca4336c95661502283ef7ff0 Mon Sep 17 00:00:00 2001 From: Casper Beyer Date: Fri, 1 Oct 2021 03:54:56 +0800 Subject: [PATCH] fix(runtime/testing): format aggregate errors (#12183) --- cli/tests/integration/test_tests.rs | 6 ++++++ cli/tests/testdata/test/aggregate_error.out | 24 +++++++++++++++++++++ cli/tests/testdata/test/aggregate_error.ts | 6 ++++++ runtime/js/40_testing.js | 22 +++++++++++++++++-- 4 files changed, 56 insertions(+), 2 deletions(-) create mode 100644 cli/tests/testdata/test/aggregate_error.out create mode 100644 cli/tests/testdata/test/aggregate_error.ts diff --git a/cli/tests/integration/test_tests.rs b/cli/tests/integration/test_tests.rs index 0baf944223..24ceeefb4c 100644 --- a/cli/tests/integration/test_tests.rs +++ b/cli/tests/integration/test_tests.rs @@ -180,3 +180,9 @@ itest!(shuffle_with_seed { exit_code: 0, output: "test/shuffle.out", }); + +itest!(aggregate_error { + args: "test test/aggregate_error.ts", + exit_code: 1, + output: "test/aggregate_error.out", +}); diff --git a/cli/tests/testdata/test/aggregate_error.out b/cli/tests/testdata/test/aggregate_error.out new file mode 100644 index 0000000000..ed1ee1a94f --- /dev/null +++ b/cli/tests/testdata/test/aggregate_error.out @@ -0,0 +1,24 @@ +Check [WILDCARD]/testdata/test/aggregate_error.ts +running 1 test from [WILDCARD]/testdata/test/aggregate_error.ts +test aggregate ... FAILED ([WILDCARD]) + +failures: + +aggregate +AggregateError + Error: Error 1 + at [WILDCARD]/testdata/test/aggregate_error.ts:2:18 + [WILDCARD] + Error: Error 2 + at [WILDCARD]/testdata/test/aggregate_error.ts:3:18 + [WILDCARD] + at [WILDCARD]/testdata/test/aggregate_error.ts:5:9 + at [WILDCARD] + +failures: + + aggregate + +test result: FAILED. 0 passed; 1 failed; 0 ignored; 0 measured; 0 filtered out ([WILDCARD]) + +error: Test failed diff --git a/cli/tests/testdata/test/aggregate_error.ts b/cli/tests/testdata/test/aggregate_error.ts new file mode 100644 index 0000000000..0661ea2495 --- /dev/null +++ b/cli/tests/testdata/test/aggregate_error.ts @@ -0,0 +1,6 @@ +Deno.test("aggregate", function () { + const error1 = new Error("Error 1"); + const error2 = new Error("Error 2"); + + throw new AggregateError([error1, error2]); +}); diff --git a/runtime/js/40_testing.js b/runtime/js/40_testing.js index 38382f9fda..f4f5373c6f 100644 --- a/runtime/js/40_testing.js +++ b/runtime/js/40_testing.js @@ -186,6 +186,23 @@ finishing test case.`; ArrayPrototypePush(tests, testDef); } + function formatFailure(error) { + if (error.errors) { + const message = error + .errors + .map((error) => + inspectArgs([error]).replace(/^(?!\s*$)/gm, " ".repeat(4)) + ) + .join("\n"); + + return { + failed: error.name + "\n" + message + error.stack, + }; + } + + return { failed: inspectArgs([error]) }; + } + function createTestFilter(filter) { return (def) => { if (filter) { @@ -213,10 +230,11 @@ finishing test case.`; try { await fn(); - return "ok"; } catch (error) { - return { "failed": inspectArgs([error]) }; + return formatFailure(error); } + + return "ok"; } function getTestOrigin() {