Skip to Content
hono-bento

Verification

What catches each kind of mistake, from the type checker to tests generated from your specs.

A spec says what each route accepts, returns and requires. hono-bento checks that the code keeps to it at four levels, from the cheapest to run to the most thorough:

WhenWhatCatches
As you typeroute() and respondA handler that reads the wrong body type, sends an undocumented status or a storage row
In the test suitehono-bento checkLayout, contract and data-access mistakes: a handler in the wrong file, a feature reading the environment
On every test requestvalidateResponsesA response that doesn't match its documented schema, at any depth
Generated testsfuzzSpecsInputs the schema allows but the code can't handle, wrong auth, error bodies in the wrong shape

Tests generated from the specs

fuzzSpecs from hono-bento/testing sends every route requests built from its contract, and checks what must hold whatever the handler does:

  • a valid request never gets a 5xx, and a 2xx body matches its schema;
  • every error body has the standard shape;
  • a request without credentials gets a 401, unless the route is public;
  • an invalid body or query gets a 400;
  • a method the path doesn't support gets a 405 with an Allow header.

A stream's body isn't read, since it may never end: only its status and headers are checked.

It needs fast-check:

bun add --dev fast-check
apps/api/src/verify.test.ts
import { expect, it } from 'bun:test';

import { todoSchema } from '@todos/schemas/todos';
import { formatFuzzFailure, fuzzSpecs } from 'hono-bento/testing';

import { createApp } from './app';
import { specs } from './specs';
import { testDb } from './test-db';

const app = createApp({ db: testDb, log: false });

it('holds the contract invariants on every route', async () => {
  // A real todo, so routes with an `:id` reach their handlers too, not only 404s.
  const created = await app.request('/v1/todos', {
    method: 'POST',
    headers: { 'x-api-key': 'sk_admin', 'content-type': 'application/json' },
    body: JSON.stringify({ title: 'Seed' }),
  });
  const { id } = todoSchema.parse(await created.json());

  const failures = await fuzzSpecs(app, specs, {
    credentials: { apiKey: { 'x-api-key': 'sk_admin' } },
    params: { id: [id] },
  });
  expect(failures.map(formatFuzzFailure)).toEqual([]);
}, 60_000);

credentials authenticates as each strategy: use an identity that may call every route. params gives path params real values to try, alongside generated ones. Run it against a real database, such as the per-run test schema, so it reaches your queries.

A failure comes back shrunk to the smallest request that breaks it. The first run against the example found this one:

POST /v1/todos valid-request: A valid request got a 500
  POST /v1/todos {"title":"\u0000"}

The schema allowed any string, but Postgres can't store a NUL byte, and no mapError mapper knew that error, so a client's mistake became a 500. mapPostgresError now answers it with a 400, and the test keeps it that way.

Migrations

The example also checks that its committed migrations build exactly what schema.prisma declares, by diffing the test schema the migrations created against the datamodel (packages/db/src/migrations.test.ts). Editing the Prisma schema without creating a migration fails the tests, not a deploy.

On this page