Skip to main content

Overview

Testing is crucial for building reliable applications. Hono provides excellent testing utilities that make it easy to test your routes, middleware, and handlers.

Test Client

The testClient helper allows you to test your Hono app with full type safety:

Using app.request()

You can also test directly using app.request():

Testing with Vitest

Hono works great with Vitest:

Testing with Jest

Hono also works with Jest:

Testing Middleware

Test custom middleware:

Testing with Environment Variables

Test with bindings (environment variables):

Testing Validation

Test routes with validation:

Testing Error Handling

Test error handlers:

Testing Headers and Cookies

Test requests with headers and cookies:

Mocking External Services

Mock external API calls:

Best Practices

The testClient helper provides full type safety and better developer experience.
Test error conditions, invalid input, and edge cases, not just happy paths.
Always mock external APIs and databases in unit tests.
Create focused tests for middleware before testing full request flows.
Write clear test descriptions that explain what is being tested.