15 · Integrations
Test on Cloudflare with Vitest
Because app.fetch takes a Request and returns a Response, the entire app is testable in-process with zero infrastructure.
Code
cloudflare-vitest.js
class="tk-cm">// app.test.js import { describe, it, expect } from class="tk-str">39;vitest39;; import app from class="tk-str">39;./worker.js39;; const env = { COUNTER: mockDurableObject() }; describe(class="tk-str">39;worker39;, () => { it(class="tk-str">39;GET /counter increments39;, async () => { const res = await app.fetch(new Request(class="tk-str">39;http:class="tk-cm">//x/counter39;), env); expect(res.status).toBe(200); expect(await res.json()).toEqual({ n: 1 }); }); it(class="tk-str">39;POST /users validates body39;, async () => { const res = await app.fetch(new Request(class="tk-str">39;http:class="tk-cm">//x/users39;, { method: class="tk-str">39;POST39;, body: 39;{class="tk-str">"bad":true}class="tk-str">39;, headers: { 39;content-typeclass="tk-str">39;: 39;application/json39; }, }), env); expect(res.status).toBe(400); }); });
How it works
The contract is just (Request, env, executionCtx) → Response. Mock env, feed Requests, assert on Responses.
No miniflare or worker emulator needed for unit tests — those are reserved for integration testing of CF-specific APIs (KV reads, DO calls).
Test runs in milliseconds because there's no HTTP server boot — it's just a function call.
Try it
Quickstart
vitest
Related modules
This is HBForge's port of Hono's example. Read the original at hono.dev/examples/cloudflare-vitest.