Local Development and Testing
Use this skill when setting up local tests or deciding what must be tested remotely on Cloudflare.
Local development stance
- Use local development to test business logic, route behavior, type safety, binding wiring, and ordinary persistence flows.
- Treat local runtime behavior as a simulator, not perfect proof of production latency, cache propagation, cold starts, placement, or network behavior.
- Add remote smoke tests for features where geography, cache behavior, WebSockets, object placement, production database latency, or quotas matter.
Basic commands
npm create cloudflare@latest my-app
cd my-app
npm install
npx wrangler dev
npx wrangler types
npm test
Test shape
Use small units for pure functions, then Worker integration tests for handlers and bindings.
import { describe, expect, it } from "vitest";
import worker from "../src/index";
describe("health route", () => {
it("returns ok", async () => {
const env = { APP_ENV: "test" } as Env;
const request = new Request("https://example.com/health");
const response = await worker.fetch(request, env, {} as ExecutionContext);
expect(response.status).toBe(200);
await expect(response.json()).resolves.toMatchObject({ ok: true });
});
});
Fixture strategy
- Keep schema migrations and seed data in version control.
- Reset local D1/KV/R2 fixtures before tests that mutate state.
- Mock external APIs unless the test is explicitly an integration test.
- Use deterministic IDs and timestamps in tests.
Remote validation cases
Run at least one deployed smoke test when any of these are in scope:
- Durable Object routing, alarms, WebSockets, or hibernation.
- Queue consumer batching/retry behavior.
- Workflows retries/checkpointing.
- R2 signed uploads/downloads and public domain behavior.
- KV propagation expectations.
- D1 performance/latency assumptions.
- AI model availability or streaming.
CI checklist
1---2name: local-dev-testing3description: Develop and test Cloudflare Workers locally with wrangler dev, Miniflare-compatible bindings, fixtures, integration tests, remote validation, and CI checks. Use when building local development workflows for Workers, D1, Durable Objects, KV, R2, Queues, or Workflows.4---5# Local Development and Testing67Use this skill when setting up local tests or deciding what must be tested remotely on Cloudflare.89## Local development stance1011- Use local development to test business logic, route behavior, type safety, binding wiring, and ordinary persistence flows.12- Treat local runtime behavior as a simulator, not perfect proof of production latency, cache propagation, cold starts, placement, or network behavior.13- Add remote smoke tests for features where geography, cache behavior, WebSockets, object placement, production database latency, or quotas matter.1415## Basic commands1617```bash18npm create cloudflare@latest my-app19cd my-app20npm install21npx wrangler dev22npx wrangler types23npm test24```2526## Test shape2728Use small units for pure functions, then Worker integration tests for handlers and bindings.2930```ts31import { describe, expect, it } from "vitest";32import worker from "../src/index";3334describe("health route", () => {35 it("returns ok", async () => {36 const env = { APP_ENV: "test" } as Env;37 const request = new Request("https://example.com/health");38 const response = await worker.fetch(request, env, {} as ExecutionContext);3940 expect(response.status).toBe(200);41 await expect(response.json()).resolves.toMatchObject({ ok: true });42 });43});44```4546## Fixture strategy4748- Keep schema migrations and seed data in version control.49- Reset local D1/KV/R2 fixtures before tests that mutate state.50- Mock external APIs unless the test is explicitly an integration test.51- Use deterministic IDs and timestamps in tests.5253## Remote validation cases5455Run at least one deployed smoke test when any of these are in scope:5657- Durable Object routing, alarms, WebSockets, or hibernation.58- Queue consumer batching/retry behavior.59- Workflows retries/checkpointing.60- R2 signed uploads/downloads and public domain behavior.61- KV propagation expectations.62- D1 performance/latency assumptions.63- AI model availability or streaming.6465## CI checklist6667- [ ] Typecheck with generated `Env` types.68- [ ] Run lint and unit tests.69- [ ] Validate migrations can apply to a fresh local database.70- [ ] Build frontend assets if the Worker serves a full-stack app.71- [ ] Run `wrangler deploy --dry-run` or equivalent config validation when available.72- [ ] Do not require production secrets for tests.