secondsky/claude-skills / plugins/cloudflare-workers/skills/cloudflare-workers-testing
cloudflare-workers-testing
VerifiedComprehensive testing guide for Cloudflare Workers using Vitest and @cloudflare/vitest-pool-workers. Use for test setup, binding mocks (D1/KV/R2/DO), integration tests, or encountering test failures, mock errors, coverage issues.
| File | Kind | Size |
|---|---|---|
| SKILL.md | skill md | 20.6 KB |
| references/binding-mocks.md | markdown | 12.1 KB |
| references/coverage-optimization.md | markdown | 7.4 KB |
| references/integration-testing.md | markdown | 8.9 KB |
| references/troubleshooting.md | markdown | 7.3 KB |
| references/vitest-setup.md | markdown | 6.8 KB |
| scripts/run-tests.sh | script | 4.2 KB |
| scripts/setup-vitest.sh | script | 5.0 KB |
| templates/basic-test.ts | script | 5.3 KB |
| templates/binding-mock-test.ts | script | 8.7 KB |
| templates/vitest-config.ts | script | 3.0 KB |
templates/basic-test.ts
View on GitHub/**
* Basic Test Template for Cloudflare Workers
*
* This template demonstrates:
* - Importing worker and test utilities
* - Testing GET/POST requests
* - Using execution context
* - Testing error cases
* - Setup/teardown with beforeEach/afterEach
*
* Usage:
* 1. Copy to test/ directory
* 2. Rename to match file being tested (e.g., index.test.ts)
* 3. Update worker import path
* 4. Add your test cases
*/
import { describe, it, expect, beforeEach, afterEach } from 'vitest';
import { env, createExecutionContext, waitOnExecutionContext } from 'cloudflare:test';
import worker from '../src/index'; // Update path to your worker
describe('Worker', () => {
// Setup runs before each test
beforeEach(async () => {
// Seed test data if needed
// await env.DB.exec('CREATE TABLE IF NOT EXISTS users ...');
// await env.KV.put('test-key', 'test-value');
});
// Cleanup runs after each test (usually not needed due to automatic isolation)
afterEach(async () => {
// Manual cleanup if needed (bindings auto-reset between tests)
});
describe('GET requests', () => {
it('responds to GET /', async () => {
const request = new Request('http://example.com/');
const ctx = createExecutionContext();
const response = await worker.fetch(request, env, ctx);
await waitOnExecutionContext(ctx);
expect(response.status).toBe(200);
const text = await response.text();
expect(text).toContain('Hello'); // Update expected content
});
it('handles query parameters', async () => {
const request = new Request('http://example.com/search?q=test&limit=10');
const ctx = createExecutionContext();
const response = await worker.fetch(request, env, ctx);
await waitOnExecutionContext(ctx);
expect(response.status).toBe(200);
const data = await response.json();
expect(data.query).toBe('test');
expect(data.limit).toBe(10);
});
});
describe('POST requests', () => {
it('creates resource via POST', async () => {
const request = new Request('http://example.com/api/users', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({
name: 'Alice',
email: 'alice@example.com'
})
});
const ctx = createExecutionContext();
const response = await worker.fetch(request, env, ctx);
await waitOnExecutionContext(ctx);
expect(response.status).toBe(201);
const user = await response.json();
expect(user).toHaveProperty('id');
expect(user.name).toBe('Alice');
});
it('validates required fields', async () => {
const request = new Request('http://example.com/api/users', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({
// Missing required fields
})
});
const ctx = createExecutionContext();
const response = await worker.fetch(request, env, ctx);
await waitOnExecutionContext(ctx);
expect(response.status).toBe(400);
const error = await response.json();
expect(error).toHaveProperty('errors');
});
});
describe('Error handling', () => {
it('returns 404 for unknown routes', async () => {
const request = new Request('http://example.com/nonexistent');
const ctx = createExecutionContext();
const response = await worker.fetch(request, env, ctx);
await waitOnExecutionContext(ctx);
expect(response.status).toBe(404);
});
it('handles invalid JSON', async () => {
const request = new Request('http://example.com/api/data', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: 'invalid json'
});
const ctx = createExecutionContext();
const response = await worker.fetch(request, env, ctx);
await waitOnExecutionContext(ctx);
expect(response.status).toBe(400);
});
it('returns proper CORS headers', async () => {
const request = new Request('http://example.com/api/data', {
method: 'OPTIONS',
headers: {
'Origin': 'https://example.com',
'Access-Control-Request-Method': 'POST'
}
});
const ctx = createExecutionContext();
const response = await worker.fetch(request, env, ctx);
await waitOnExecutionContext(ctx);
expect(response.headers.get('Access-Control-Allow-Origin')).toBeDefined();
expect(response.headers.get('Access-Control-Allow-Methods')).toBeDefined();
});
});
describe('Headers and metadata', () => {
it('sets correct Content-Type', async () => {
const request = new Request('http://example.com/api/data');
const ctx = createExecutionContext();
const response = await worker.fetch(request, env, ctx);
await waitOnExecutionContext(ctx);
expect(response.headers.get('Content-Type')).toBe('application/json');
});
it('includes cache headers', async () => {
const request = new Request('http://example.com/static/file.txt');
const ctx = createExecutionContext();
const response = await worker.fetch(request, env, ctx);
await waitOnExecutionContext(ctx);
expect(response.headers.get('Cache-Control')).toBeDefined();
});
});
});
Related skills
| Skill | Stars | Updated | Works with | Status | Install |
|---|---|---|---|---|---|
| finishing-a-development-branchobra/superpowersUse when implementation is complete, all tests pass, and you need to decide how to integrate the work | Stars: 322k | Updated | Claude CodeCodex | Verified | Install |
| systematic-debuggingobra/superpowersUse when encountering any bug, test failure, or unexpected behavior, before proposing fixes | Stars: 322k | Updated | Claude CodeCodex | Verified | Install |
| test-driven-developmentobra/superpowersUse when implementing any feature or bugfix, before writing implementation code | Stars: 322k | Updated | Claude CodeCodex | Verified | Install |
| skill-creatoranthropics/skillsCreate new skills, modify and improve existing skills, and measure skill performance. Use when users want to create a skill from scratch, edit, or optimize an existing skill, run evals to test a skill, benchmark skill performance with variance analysis, or optimize a skill's description for better triggering accuracy. | Stars: 197k | Updated | Claude CodeCodex | Verified | Install |