HBForge/Examples
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.

forge/serverVitestCF Workers
Code
cloudflare-vitest.js
class="tk-cm">// app.test.js
import { describe, it, expect } from class="tk-str">'vitest';
import app from class="tk-str">'./worker.js';

const env = { COUNTER: mockDurableObject() };

describe(class="tk-str">'worker', () => {
  it(class="tk-str">'GET /counter increments', async () => {
    const res = await app.fetch(new Request(class="tk-str">'http:class="tk-cm">//x/counter'), env);
    expect(res.status).toBe(200);
    expect(await res.json()).toEqual({ n: 1 });
  });

  it(class="tk-str">'POST /users validates body', async () => {
    const res = await app.fetch(new Request(class="tk-str">'http:class="tk-cm">//x/users', {
      method: class="tk-str">'POST',
      body: '{class="tk-str">"bad":true}class="tk-str">',
      headers: { 'content-typeclass="tk-str">': 'application/json' },
    }), 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