Testing
For the complete documentation index, see llms.txt. Prefer markdown by appending .md to documentation URLs or sending Accept: text/markdown.

Unit tests

A configured Vitest setup for application logic and Worker-backed operations, with watch mode, coverage, and example tests to build on.

Vitest provides fast feedback on application logic without running the whole browser flow. Edge Kit includes a configured test setup with separate projects for Node logic and Worker-backed operations.

Use the Node project for pure logic and validation, and the Worker project for code that needs Cloudflare bindings. The shared commands, file conventions, watch mode, and coverage configuration are ready for tests around your own features.

Why Vitest?

Vitest fits the kit's Vite and TypeScript toolchain, with fast watch mode, assertions, and coverage in one setup. Cloudflare's Vitest integration also runs binding-backed tests in the Workers runtime, so pure logic and D1 operations can share the same testing workflow.

Commands

pnpm test
pnpm test:watch

To select the unit project:

pnpm test --project unit

The configured Istanbul coverage report is written under coverage/. Keep the coverage provider when testing Workers, where runtime support differs from Node.

Environments

ProjectTest filesScope
unit*.test.tsPure logic, validation, and billing policy in Node
worker*.worker.test.tsD1 and binding-backed operations in the Workers runtime

Keep pure logic tests independent of services they do not need. The Worker project provides a place to add tests that rely on Cloudflare bindings.

Application tests

Add tests around the behavior your feature promises: accepted and rejected input, resource ownership, plan requirements, calculations, and failure handling. Pure logic can be checked without a database; binding-backed operations can run in the Worker project.

The included billing suite demonstrates tests for plan selection, subscription states, discounts, and Stripe mapping. Use those cases as a reference for policy boundaries when adding another product feature.

For example, a validation test can check the title rule from the forms guide:

note.test.ts
import { expect, test } from "vitest";
import { z } from "zod";

const noteSchema = z.object({ title: z.string().trim().min(1).max(120) });

test("rejects a blank title", () => {
  expect(noteSchema.safeParse({ title: "   " }).success).toBe(false);
});

This self-contained example keeps the rule beside its assertion. In your feature, import its shared schema so the test exercises the same validation as the form and server.

Worker tests

For a database-backed test, apply the bundled migrations to the test database before making assertions. Configure any additional test bindings the feature needs, keeping test-only values separate from production settings.

Cloudflare's D1 test recipes and test APIs cover the setup.

pnpm test --project worker

Test data

The test database is separate from the development replica and remote D1. Prepare the records each test needs instead of relying on your running app's data.

Use browser tests for navigation, sessions, and complete UI flows.

How is this guide?

Last updated on

On this page

Ship globally on the edge. In minutes.Try Edge Kit