install
source · Clone the upstream repo
git clone https://github.com/ComeOnOliver/skillshub
Claude Code · Install into ~/.claude/skills/
T=$(mktemp -d) && git clone --depth=1 https://github.com/ComeOnOliver/skillshub "$T" && mkdir -p ~/.claude/skills && cp -r "$T/skills/pproenca/dot-skills/msw" ~/.claude/skills/comeonoliver-skillshub-msw && rm -rf "$T"
manifest:
skills/pproenca/dot-skills/msw/SKILL.mdsource content
MSW Best Practices
Comprehensive API mocking guide for MSW v2 applications, designed for AI agents and LLMs. Contains 45 rules across 8 categories, prioritized by impact to guide automated refactoring and code generation.
When to Apply
Reference these guidelines when:
- Setting up MSW for testing or development
- Writing or organizing request handlers
- Configuring test environments with MSW
- Mocking REST or GraphQL APIs
- Debugging handler matching issues
- Testing error states and edge cases
Rule Categories by Priority
| Priority | Category | Impact | Prefix |
|---|---|---|---|
| 1 | Setup & Initialization | CRITICAL | |
| 2 | Handler Architecture | CRITICAL | |
| 3 | Test Integration | HIGH | |
| 4 | Response Patterns | HIGH | |
| 5 | Request Matching | MEDIUM-HIGH | |
| 6 | GraphQL Mocking | MEDIUM | |
| 7 | Advanced Patterns | MEDIUM | |
| 8 | Debugging & Performance | LOW | |
Quick Reference
1. Setup & Initialization (CRITICAL)
- Use correct entrypoint for Node.js (msw/node)setup-server-node-entrypoint
- Configure server lifecycle in test setupsetup-lifecycle-hooks
- Commit worker script to version controlsetup-worker-script-commit
- Require Node.js 18+ for MSW v2setup-node-version
- Configure unhandled request behaviorsetup-unhandled-requests
- Configure TypeScript for MSW v2setup-typescript-config
2. Handler Architecture (CRITICAL)
- Define happy path handlers as baselinehandler-happy-path-first
- Group handlers by domainhandler-domain-grouping
- Use absolute URLs in handlershandler-absolute-urls
- Extract shared response logic into resolvershandler-shared-resolvers
- Use MSW v2 response syntaxhandler-v2-response-syntax
- Explicitly parse request bodieshandler-request-body-parsing
- Destructure resolver arguments correctlyhandler-resolver-argument
- Share handlers across environmentshandler-reusability-environments
3. Test Integration (HIGH)
- Reset handlers after each testtest-reset-handlers
- Avoid direct request assertionstest-avoid-request-assertions
- Use server.boundary() for concurrent teststest-concurrent-boundary
- Configure fake timers to preserve queueMicrotasktest-fake-timers-config
- Use async testing utilities for mock responsestest-async-utilities
- Clear request library caches between teststest-clear-request-cache
- Use correct JSDOM environment for Jesttest-jsdom-environment
4. Response Patterns (HIGH)
- Use HttpResponse static methodsresponse-http-response-helpers
- Add realistic response delaysresponse-delay-realistic
- Simulate error responses correctlyresponse-error-simulation
- Use one-time handlers for sequential scenariosresponse-one-time-handlers
- Set response headers correctlyresponse-custom-headers
- Mock streaming responses with ReadableStreamresponse-streaming
5. Request Matching (MEDIUM-HIGH)
- Use URL path parameters correctlymatch-url-patterns
- Access query parameters from request URLmatch-query-params
- Use custom predicates for complex matchingmatch-custom-predicate
- Match HTTP methods explicitlymatch-http-methods
- Order handlers from specific to generalmatch-handler-order
6. GraphQL Mocking (MEDIUM)
- Use operation name for GraphQL matchinggraphql-operation-handlers
- Return GraphQL errors in correct formatgraphql-error-responses
- Handle batched GraphQL queriesgraphql-batched-queries
- Access GraphQL variables correctlygraphql-variables-access
7. Advanced Patterns (MEDIUM)
- Use bypass() for passthrough requestsadvanced-bypass-requests
- Handle cookies and authenticationadvanced-cookies-auth
- Implement dynamic mock scenariosadvanced-dynamic-scenarios
- Configure MSW for Vitest browser modeadvanced-vitest-browser
- Mock file upload endpointsadvanced-file-uploads
8. Debugging & Performance (LOW)
- Use lifecycle events for debuggingdebug-lifecycle-events
- Verify request interception is workingdebug-verify-interception
- Know common MSW issues and fixesdebug-common-issues
- Log request details for debuggingdebug-request-logging
How to Use
Read individual reference files for detailed explanations and code examples:
- Section definitions - Category structure and impact levels
- Rule template - Template for adding new rules
- Individual rules:
references/{prefix}-{slug}.md
Related Skills
- For generating MSW mocks from OpenAPI, see
skillorval - For consuming mocked APIs, see
skilltanstack-query - For test methodology, see
ortest-vitest
skillstest-tdd
Full Compiled Document
For the complete guide with all rules expanded:
AGENTS.md