Chapter 14
Testing Hono Apps
Test routes with app.request and normal Response assertions.
Testing Hono Apps
Hono apps are easy to test because requests and responses use Web Standards. You can call the app directly with app.request().
const res = await app.request('/')
expect(res.status).toBe(200)
expect(await res.text()).toContain('Hono Book')
Test JSON routes
Use normal response methods in your assertions.
const res = await app.request('/api/chapters')
const body = await res.json()
expect(res.status).toBe(200)
expect(body.chapters).toHaveLength(3)
Test request methods
Pass method, headers, and body options when testing non-GET routes.
const res = await app.request('/api/chapters', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
title: 'Routing',
}),
})
expect(res.status).toBe(201)
Table-style route tests
For simple routes, loop through expected paths.
const cases = [
['/', 200],
['/toc', 302],
['/missing', 404],
] as const
for (const [path, status] of cases) {
const res = await app.request(path)
expect(res.status).toBe(status)
}
Testing through app.request() keeps tests close to real HTTP behavior without needing to start a local server.