Skip to content

Commit

Permalink
fix(runtime/testing): format aggregate errors (#12183)
Browse files Browse the repository at this point in the history
  • Loading branch information
caspervonb authored and ry committed Oct 4, 2021
1 parent b20a779 commit 96530df
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 2 deletions.
6 changes: 6 additions & 0 deletions cli/tests/integration/test_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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",
});
24 changes: 24 additions & 0 deletions cli/tests/testdata/test/aggregate_error.out
Original file line number Diff line number Diff line change
@@ -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
6 changes: 6 additions & 0 deletions cli/tests/testdata/test/aggregate_error.ts
Original file line number Diff line number Diff line change
@@ -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]);
});
22 changes: 20 additions & 2 deletions runtime/js/40_testing.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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() {
Expand Down

0 comments on commit 96530df

Please sign in to comment.