2024-01-01 21:11:32 +00:00
|
|
|
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
|
2023-07-13 07:04:30 +00:00
|
|
|
import { AssertionError, assertIsError, assertThrows } from "./mod.ts";
|
|
|
|
|
2023-12-08 16:04:29 +00:00
|
|
|
class CustomError extends Error {}
|
|
|
|
class AnotherCustomError extends Error {}
|
|
|
|
|
|
|
|
Deno.test("assertIsError() throws when given value isn't error", () => {
|
2023-07-13 07:04:30 +00:00
|
|
|
assertThrows(
|
|
|
|
() => assertIsError("Panic!", undefined, "Panic!"),
|
|
|
|
AssertionError,
|
|
|
|
`Expected "error" to be an Error object.`,
|
|
|
|
);
|
|
|
|
|
|
|
|
assertThrows(
|
|
|
|
() => assertIsError(null),
|
|
|
|
AssertionError,
|
|
|
|
`Expected "error" to be an Error object.`,
|
|
|
|
);
|
|
|
|
|
|
|
|
assertThrows(
|
|
|
|
() => assertIsError(undefined),
|
|
|
|
AssertionError,
|
|
|
|
`Expected "error" to be an Error object.`,
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
2023-12-08 16:04:29 +00:00
|
|
|
Deno.test("assertIsError() allows subclass of Error", () => {
|
2023-07-13 07:04:30 +00:00
|
|
|
assertIsError(new AssertionError("Fail!"), Error, "Fail!");
|
|
|
|
});
|
|
|
|
|
2023-12-08 16:04:29 +00:00
|
|
|
Deno.test("assertIsError() allows custom error", () => {
|
2023-07-13 07:04:30 +00:00
|
|
|
assertIsError(new CustomError("failed"), CustomError, "fail");
|
|
|
|
assertThrows(
|
|
|
|
() => assertIsError(new AnotherCustomError("failed"), CustomError, "fail"),
|
|
|
|
AssertionError,
|
|
|
|
'Expected error to be instance of "CustomError", but was "AnotherCustomError".',
|
|
|
|
);
|
|
|
|
});
|
2023-08-29 11:23:07 +00:00
|
|
|
|
2023-12-08 16:04:29 +00:00
|
|
|
Deno.test("assertIsError() throws with message diff containing double quotes", () => {
|
2023-08-29 11:23:07 +00:00
|
|
|
assertThrows(
|
|
|
|
() =>
|
|
|
|
assertIsError(
|
|
|
|
new CustomError('error with "double quotes"'),
|
|
|
|
CustomError,
|
|
|
|
'doesn\'t include "this message"',
|
|
|
|
),
|
|
|
|
AssertionError,
|
|
|
|
`Expected error message to include "doesn't include \\"this message\\"", but got "error with \\"double quotes\\"".`,
|
|
|
|
);
|
|
|
|
});
|
2023-12-12 14:45:58 +00:00
|
|
|
|
|
|
|
Deno.test("assertIsError() throws when given value doesn't match regex ", () => {
|
|
|
|
assertIsError(new AssertionError("Regex test"), Error, /ege/);
|
|
|
|
assertThrows(
|
|
|
|
() => assertIsError(new AssertionError("Regex test"), Error, /egg/),
|
|
|
|
Error,
|
|
|
|
`Expected error message to include /egg/, but got "Regex test"`,
|
|
|
|
);
|
|
|
|
});
|
2024-05-07 00:08:16 +00:00
|
|
|
|
|
|
|
Deno.test("assertIsError() throws with custom message", () => {
|
|
|
|
assertThrows(
|
|
|
|
() =>
|
|
|
|
assertIsError(
|
|
|
|
new CustomError("failed"),
|
|
|
|
AnotherCustomError,
|
|
|
|
"fail",
|
|
|
|
"CUSTOM MESSAGE",
|
|
|
|
),
|
|
|
|
AssertionError,
|
|
|
|
'Expected error to be instance of "AnotherCustomError", but was "CustomError": CUSTOM MESSAGE',
|
|
|
|
);
|
|
|
|
});
|