Skip to content

Commit

Permalink
simplify tests using assertThrows
Browse files Browse the repository at this point in the history
  • Loading branch information
jsejcksn committed Jul 28, 2024
1 parent 5f6c97c commit 29bb954
Showing 1 changed file with 16 additions and 30 deletions.
46 changes: 16 additions & 30 deletions assert/assertion_error_test.ts
Original file line number Diff line number Diff line change
@@ -1,39 +1,25 @@
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
import {
assert,
assertInstanceOf,
AssertionError,
assertStrictEquals,
} from "./mod.ts";
import { AssertionError, assertStrictEquals, assertThrows } from "./mod.ts";

Deno.test("AssertionError", async (t) => {
let expectedPathReached = false;
const originalError = new Error("foo");
try {
throw originalError;
} catch (cause) {
const assertionError = assertThrows(() => {
try {
throw new AssertionError("bar", { cause });
throw originalError;
} catch (cause) {
assertInstanceOf(cause, AssertionError);

await t.step(
'value of message parameter is accessible on property "message"',
() => assertStrictEquals(cause.message, "bar"),
);

await t.step("options parameter", async (t) => {
await t.step(
'value of property "cause" is accessible on property "cause"',
() => assertStrictEquals(cause.cause, originalError),
);
});

expectedPathReached = true;
throw new AssertionError("bar", { cause });
}
}
assert(
expectedPathReached,
"Expected all exception-handling code paths to be reached",
}, AssertionError);

await t.step(
'value of message parameter is accessible on property "message"',
() => assertStrictEquals(assertionError.message, "bar"),
);

await t.step("options parameter", async (t) => {
await t.step(
'value of property "cause" is accessible on property "cause"',
() => assertStrictEquals(assertionError.cause, originalError),
);
});
});

0 comments on commit 29bb954

Please sign in to comment.