Code & Dev Compatible: claude

MSW Request Mocks for the Browser and Tests

Complexity Level: Expert
Prompt Code Block

Set up Mock Service Worker so tests and local UI can share one set of handlers. Handlers live next to the API shape, not inside components. API Base URL: [URL] Endpoints to mock first: [LIST WITH METHOD AND A SAMPLE JSON] Auth: [COOKIE | BEARER] Error cases we need: [401, 422 WITH FIELD ERRORS, 500] DELIVER 1) The handler file: success, and one forced error via a query param or a named override so a test can opt in. 2) The test server lifecycle: listen before all, reset after each, close after all. A test that awaits the UI, not a fixed sleep. 3) The browser worker for local dev, gated by an env flag, and the warning so we do not ship the worker to production. Say where the worker file is generated and that it is not a secret. 4) A contract comment: the sample JSON should match the type we already have. If it drifts, which test fails. 5) What not to mock: the module under test. Do not mock fetch globally in a way that hides a request we forgot.

🌟 Example Output / Preview

### Generated Component Preview: ```typescript // Fully validated modern structure import { z } from 'zod'; export const RequestSchema = z.object({ id: z.string().uuid(), createdAt: z.date().default(() => new Date()), data: z.record(z.string(), z.any()) }); export type ValidatedRequest = z.infer<typeof RequestSchema>; ```

Prompt Metadata

DifficultyExpert
Compatibilityclaude

Primary Use Cases:

  • •Legacy code modernization & technical refactoring
  • •Full-stack layout generation & component structuring
  • •CI/CD workflow automation & unit/E2E testing suites

Associated Tags:

#msw #testing #api #typescript

💡 Pro Tips & Advice

1. Use bracketed items: Be sure to fill out all [PLACEHOLDER] elements with specific details before sending the prompt to the AI model.

2. Adjust temperature: For creative tasks, set AI temperature higher (e.g., 0.8), or lower (e.g., 0.2) for strict coding/technical tasks.

🔗 Related AI Prompts

Code & Dev
★ Featured

Full SaaS Landing Page Build Spec (SEO + Conversion)

You are a Staff Product Designer + Frontend Engineer + SEO strategist. Build a complete, production-ready marketing landing page f...

Compatible:claudeDiff:Advanced
#landing-page #saas #seo #nextjs #tailwind
Code & Dev
★ Featured 🔥 Trending

SaaS Analytics Dashboard Full Build Spec

You are a Principal Product Designer + Frontend Lead. Design and specify a complete [LIGHT | DARK] analytics dashboard for [PRODUC...

Compatible:claudeDiff:Intermediate
#dashboard #saas #ui #tailwind #seo
Code & Dev

SaaS Admin Panel Dashboard Build Spec

Design a full internal Admin Dashboard for [PRODUCT_NAME] used by [ADMIN_ROLE: support | ops | super-admin]. --- 🎯 CONTEXT Adm...

Compatible:claudeDiff:Intermediate
#admin #dashboard #saas #crud #tailwind