Skip to content

Commit 6dfa45d

Browse files
committed
chore: tests
1 parent 9c136e9 commit 6dfa45d

1 file changed

Lines changed: 33 additions & 0 deletions

File tree

src/endpoint.test.ts

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -505,6 +505,39 @@ describe("response", () => {
505505
message: "error message",
506506
});
507507
});
508+
509+
it("custom validation errors", async () => {
510+
const endpoint = createEndpoint(
511+
"/endpoint",
512+
{
513+
method: "POST",
514+
body: z.string().min(10).max(1), // Impossible to satisfy
515+
onValidationError({ issues, message }) {
516+
expect(typeof message).toBe("string");
517+
expect(issues.length).toBeGreaterThan(0);
518+
throw new APIError("I'M_A_TEAPOT", {
519+
message: "Such a useful error status.",
520+
});
521+
},
522+
},
523+
async (c) => {
524+
return c.json({
525+
success: false, // Should never recieve this.
526+
});
527+
},
528+
);
529+
try {
530+
const response = await endpoint({ body: "Hello world!" });
531+
// This ensures that there is an error thrown.
532+
expect(response).not.toBeCalled();
533+
} catch (error) {
534+
expect(error).toBeInstanceOf(APIError);
535+
if (!(error instanceof APIError)) return;
536+
// Ensure it's the validation error we defined.
537+
expect(error.status).toBe("I'M_A_TEAPOT");
538+
expect(error.message).toBe("Such a useful error status.");
539+
}
540+
});
508541
});
509542
describe("json", async () => {
510543
it("should return the json directly", async () => {

0 commit comments

Comments
 (0)