Test
Run the Vitest suite, which executes inside workerd against a real D1 database.
npm test runs the suite in test/ with the Cloudflare Workers Vitest integration. Tests execute inside workerd, the same runtime that serves production, against a real local D1 database. No Docker and no mocks for storage.
Run the suite#
npm test
npm run typecheckTest Files 1 passed (1)
Tests 7 passed (7)How it works#
Three files set the stage:
| File | Role |
|---|---|
vitest.config.ts | Loads @cloudflare/vitest-plugin, reads migrations/ and exposes them as the TEST_MIGRATIONS binding. |
test/apply-migrations.ts | Setup file that applies the migrations to D1 before any test runs. |
test/helpers.ts | api(), which calls the Hono app with real bindings, and registerAndLogin(), which returns headers with a valid token. |
Each test creates its own user with a unique name through registerAndLogin(), so tests never depend on each other's data.
Adding a test#
Import the helpers and call api() with a path and a fetch init:
import { describe, expect, it } from 'vitest';
import { api, registerAndLogin, uniqueName } from './helpers';
describe('recipes', () => {
it('rejects an unknown classification', async () => {
const headers = await registerAndLogin(uniqueName('user'));
const res = await api('/api/v1/recipes', {
method: 'POST',
headers,
body: JSON.stringify({ recipe_name: 'Ghost', classification: 9999 }),
});
expect(res.status).toBe(400);
});
});The suite covers the auth guards (403, 401) and the registration paths (200, 409, 422). One end-to-end flow creates a classification, a recipe and an ingredient, maps and unmaps a weekly menu, reviews both, and walks every delete path.