Generate Mock Service Worker Handlers for Frontend Tests
Writing MSW handlers by hand is tedious when you have dozens of API endpoints. Paste your route definitions or controller files and ask Claude to generate a complete set of Mock Service Worker handlers — including realistic response payloads and error cases.
cat src/api/routes.ts | claude -p "Generate MSW 2.x handlers for all these routes with realistic mock data and a 500 error variant for each endpoint"
Claude generates properly structured handlers:
import { http, HttpResponse } from 'msw';
export const handlers = [
http.get('/api/users/:id', ({ params }) => {
return HttpResponse.json({
id: Number(params.id),
name: 'Jane Smith',
email: 'jane@example.com',
role: 'admin',
createdAt: '2024-01-15T09:00:00Z',
});
}),
http.post('/api/users', async ({ request }) => {
const body = await request.json();
return HttpResponse.json({ id: 42, ...body }, { status: 201 });
}),
];
// Error handlers for testing failure states
export const errorHandlers = [
http.get('/api/users/:id', () => new HttpResponse(null, { status: 500 })),
];
Ask Claude to match the response shape your actual API returns — inferred from your TypeScript types or OpenAPI spec — rather than inventing arbitrary mock data. This means your tests catch real type mismatches instead of passing against unrealistic stubs.
Realistic mocks mean your tests catch the integration bugs that actually matter.
Log in to leave a comment.
Set up Claude Code as an automated reviewer in your CI pipeline — on every pull request, it reads the diff, checks for bugs, security issues, missing tests, and convention violations, then posts its findings as a PR comment. Your human reviewers get a head start because the obvious issues are already flagged before they look.
Before deploying, tell Claude to read your project — migrations, environment variables, queue workers, scheduled tasks, caching, third-party integrations — and generate a deployment checklist that's specific to your app. Not a generic "did you run migrations?" list, but one that knows YOUR infrastructure and catches the things YOUR deploy can break.
Instead of writing a README from memory or copying a template, tell Claude to read your project and generate one that's actually accurate — real setup instructions from your config, real architecture from your directory structure, real API examples from your routes, and real prerequisites from your dependency files.