Skip to content

Commit 480aeac

Browse files
feat: added tests for typebox (#184)
1 parent 1b546a5 commit 480aeac

File tree

4 files changed

+245
-14
lines changed

4 files changed

+245
-14
lines changed

package.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,8 @@
4949
},
5050
"peerDependencies": {
5151
"@hono/standard-validator": "^0.1.2",
52-
"@standard-community/standard-json": "^0.3.1",
53-
"@standard-community/standard-openapi": "^0.2.5",
52+
"@standard-community/standard-json": "^0.3.4",
53+
"@standard-community/standard-openapi": "^0.2.6",
5454
"@types/json-schema": "^7.0.15",
5555
"hono": "^4.8.3",
5656
"openapi-types": "^12.1.3"
@@ -74,6 +74,7 @@
7474
"nano-staged": "^0.8.0",
7575
"pkg-pr-new": "^0.0.60",
7676
"pkgroll": "^2.13.1",
77+
"typebox": "^1.0.17",
7778
"typescript": "^5.8.3",
7879
"valibot": "^1.1.0",
7980
"vitest": "^3.2.4",

pnpm-lock.yaml

Lines changed: 28 additions & 12 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html
2+
3+
exports[`typebox > basic 1`] = `
4+
{
5+
"components": {},
6+
"info": {
7+
"description": "Development documentation",
8+
"title": "Hono Documentation",
9+
"version": "0.0.0",
10+
},
11+
"openapi": "3.1.0",
12+
"paths": {
13+
"/": {
14+
"get": {
15+
"description": "This is a test route",
16+
"operationId": "getIndex",
17+
"requestBody": {
18+
"content": {
19+
"application/json": {
20+
"schema": {
21+
"properties": {
22+
"message": {
23+
"type": "string",
24+
},
25+
},
26+
"required": [
27+
"message",
28+
],
29+
"type": "object",
30+
},
31+
},
32+
},
33+
},
34+
"responses": {
35+
"200": {
36+
"content": {
37+
"application/json": {
38+
"schema": {
39+
"properties": {
40+
"message": {
41+
"type": "string",
42+
},
43+
},
44+
"required": [
45+
"message",
46+
],
47+
"type": "object",
48+
},
49+
},
50+
},
51+
"description": "Success",
52+
},
53+
},
54+
"summary": "Test route",
55+
"tags": [
56+
"test",
57+
],
58+
},
59+
},
60+
},
61+
"tags": undefined,
62+
}
63+
`;
64+
65+
exports[`typebox > with metadata 1`] = `
66+
{
67+
"components": {
68+
"schemas": {
69+
"SuccessResponse": {
70+
"properties": {
71+
"message": {
72+
"type": "string",
73+
},
74+
},
75+
"required": [
76+
"message",
77+
],
78+
"type": "object",
79+
},
80+
},
81+
},
82+
"info": {
83+
"description": "Development documentation",
84+
"title": "Hono Documentation",
85+
"version": "0.0.0",
86+
},
87+
"openapi": "3.1.0",
88+
"paths": {
89+
"/": {
90+
"get": {
91+
"description": "This is a test route",
92+
"operationId": "getIndex",
93+
"requestBody": {
94+
"content": {
95+
"application/json": {
96+
"schema": {
97+
"properties": {
98+
"message": {
99+
"type": "string",
100+
},
101+
},
102+
"required": [
103+
"message",
104+
],
105+
"type": "object",
106+
},
107+
},
108+
},
109+
},
110+
"responses": {
111+
"200": {
112+
"content": {
113+
"application/json": {
114+
"schema": {
115+
"$ref": "#/components/schemas/SuccessResponse",
116+
},
117+
},
118+
},
119+
"description": "Success",
120+
},
121+
},
122+
"summary": "Test route",
123+
"tags": [
124+
"test",
125+
],
126+
},
127+
},
128+
},
129+
"tags": undefined,
130+
}
131+
`;

src/__tests__/typebox.test.ts

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
import { Hono } from "hono";
2+
import Type from "typebox";
3+
import { Compile } from "typebox/compile";
4+
import { describe, expect, it } from "vitest";
5+
import { generateSpecs } from "../handler.js";
6+
import { describeRoute, resolver, validator } from "../middlewares.js";
7+
8+
describe("typebox", () => {
9+
it("basic", async () => {
10+
const app = new Hono().get(
11+
"/",
12+
describeRoute({
13+
tags: ["test"],
14+
summary: "Test route",
15+
description: "This is a test route",
16+
responses: {
17+
200: {
18+
description: "Success",
19+
content: {
20+
"application/json": {
21+
schema: resolver(
22+
Compile(
23+
Type.Object({
24+
message: Type.String(),
25+
}),
26+
),
27+
),
28+
},
29+
},
30+
},
31+
},
32+
}),
33+
validator("json", Compile(Type.Object({ message: Type.String() }))),
34+
async (c) => {
35+
return c.json({ message: "Hello, world!" });
36+
},
37+
);
38+
39+
const specs = await generateSpecs(app);
40+
41+
expect(specs).toMatchSnapshot();
42+
});
43+
44+
it("with metadata", async () => {
45+
const app = new Hono().get(
46+
"/",
47+
describeRoute({
48+
tags: ["test"],
49+
summary: "Test route",
50+
description: "This is a test route",
51+
responses: {
52+
200: {
53+
description: "Success",
54+
content: {
55+
"application/json": {
56+
schema: resolver(
57+
Compile(
58+
Type.Object(
59+
{
60+
message: Type.String(),
61+
},
62+
{
63+
$id: "SuccessResponse",
64+
},
65+
),
66+
),
67+
),
68+
},
69+
},
70+
},
71+
},
72+
}),
73+
validator("json", Compile(Type.Object({ message: Type.String() }))),
74+
async (c) => {
75+
return c.json({ message: "Hello, world!" });
76+
},
77+
);
78+
79+
const specs = await generateSpecs(app);
80+
81+
expect(specs).toMatchSnapshot();
82+
});
83+
});

0 commit comments

Comments
 (0)