Skip to content

Commit

Permalink
Merge pull request #6 from udibo/dev
Browse files Browse the repository at this point in the history
Improve test coverage
  • Loading branch information
KyleJune authored Jul 4, 2023
2 parents 063e674 + 8db044e commit fb70481
Showing 1 changed file with 30 additions and 5 deletions.
35 changes: 30 additions & 5 deletions mod_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {
ErrorResponse,
HttpError,
HttpErrorOptions,
isErrorResponse,
isHttpError,
optionsFromArgs,
} from "./mod.ts";
Expand Down Expand Up @@ -391,7 +392,7 @@ it(httpErrorTests, "json", () => {

const fromTests = describe(httpErrorTests, "from");

it(fromTests, "from non HttpError", () => {
it(fromTests, "non HttpError", () => {
const cause = new Error("fail");
const error = HttpError.from(cause);
assertEquals(error.toString(), "InternalServerError: fail");
Expand All @@ -402,7 +403,7 @@ it(fromTests, "from non HttpError", () => {
assertEquals(error.cause, cause);
});

it(fromTests, "from Error with status", () => {
it(fromTests, "Error with status", () => {
const originalError = new Error("fail");
(originalError as HttpError).status = 400;
const error = HttpError.from(originalError);
Expand Down Expand Up @@ -585,7 +586,9 @@ it(ErrorResponseTests, "from external HttpError", () => {
);
});

it(ErrorResponseTests, "toError with internal ErrorResponse", () => {
const toErrorTests = describe(ErrorResponseTests, "toError");

it(toErrorTests, "with internal ErrorResponse", () => {
const errorResponse = new ErrorResponse(new HttpError("oops"));
const error = ErrorResponse.toError(errorResponse);
assertEquals(error.toString(), "InternalServerError");
Expand All @@ -596,7 +599,7 @@ it(ErrorResponseTests, "toError with internal ErrorResponse", () => {
assertEquals(error.cause, undefined);
});

it(ErrorResponseTests, "toError with external ErrorResponse", () => {
it(toErrorTests, "with external ErrorResponse", () => {
const errorResponse = new ErrorResponse(new HttpError(400, "oops"));
const error = ErrorResponse.toError(errorResponse);
assertEquals(error.toString(), "BadRequestError: oops");
Expand All @@ -607,7 +610,7 @@ it(ErrorResponseTests, "toError with external ErrorResponse", () => {
assertEquals(error.cause, undefined);
});

it(ErrorResponseTests, "toError with ErrorResponse JSON", () => {
it(toErrorTests, "with ErrorResponse JSON", () => {
const errorResponse = {
error: {
name: "BadRequestError",
Expand All @@ -623,3 +626,25 @@ it(ErrorResponseTests, "toError with ErrorResponse JSON", () => {
assertEquals(error.expose, true);
assertEquals(error.cause, undefined);
});

it("isErrorResponse", () => {
assertEquals(isErrorResponse({}), false);
assertEquals(isErrorResponse({ success: true }), false);
assertEquals(isErrorResponse({ error: {} }), true);
assertEquals(
isErrorResponse({
error: { status: 400, message: "something went wrong" },
}),
true,
);
assertEquals(
isErrorResponse({
error: {
status: 400,
name: "CustomError",
message: "something went wrong",
},
}),
true,
);
});

0 comments on commit fb70481

Please sign in to comment.