Skip to content

Commit

Permalink
add typed validator tests
Browse files Browse the repository at this point in the history
  • Loading branch information
pi0 committed Jul 26, 2023
1 parent 721932f commit 3a9fd8b
Showing 1 changed file with 28 additions and 1 deletion.
29 changes: 28 additions & 1 deletion test/types.test-d.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
import { describe, it, expectTypeOf } from "vitest";
import type { QueryObject } from "ufo";
import { eventHandler, H3Event, getQuery, readBody } from "../src";
import {
eventHandler,
H3Event,
getQuery,
readBody,
readValidatedBody,
getValidatedQuery,
} from "../src";

describe("types", () => {
describe("eventHandler", () => {
Expand Down Expand Up @@ -40,6 +47,16 @@ describe("types", () => {
});
});

it("typed via validator", () => {
eventHandler(async (event) => {
// eslint-disable-next-line unicorn/consistent-function-scoping
const validator = (body: unknown) => body as { id: string };
const body = await readValidatedBody(event, validator);
expectTypeOf(body).not.toBeAny();
expectTypeOf(body).toEqualTypeOf<{ id: string }>();
});
});

it("typed via event handler", () => {
eventHandler<{ body: { id: string } }>(async (event) => {
const body = await readBody(event);
Expand All @@ -66,6 +83,16 @@ describe("types", () => {
});
});

it("typed via validator", () => {
eventHandler(async (event) => {
// eslint-disable-next-line unicorn/consistent-function-scoping
const validator = (body: unknown) => body as { id: string };
const body = await getValidatedQuery(event, validator);
expectTypeOf(body).not.toBeAny();
expectTypeOf(body).toEqualTypeOf<{ id: string }>();
});
});

it("typed via event handler", () => {
eventHandler<{ query: { id: string } }>((event) => {
const query = getQuery(event);
Expand Down

0 comments on commit 3a9fd8b

Please sign in to comment.