msw
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseMSW Best Practices
MSW 最佳实践
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.
为AI Agent和LLM打造的MSW v2应用全面API模拟指南。包含8个类别共45条规则,按影响优先级排序,用于指导自动化重构和代码生成。
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
参考以下指南的场景:
- 为测试或开发设置MSW
- 编写或整理请求处理程序
- 使用MSW配置测试环境
- 模拟REST或GraphQL API
- 调试处理程序匹配问题
- 测试错误状态和边缘情况
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 | |
| 优先级 | 类别 | 影响程度 | 前缀 |
|---|---|---|---|
| 1 | 安装与初始化 | 关键 | |
| 2 | 处理程序架构 | 关键 | |
| 3 | 测试集成 | 高 | |
| 4 | 响应模式 | 高 | |
| 5 | 请求匹配 | 中高 | |
| 6 | GraphQL模拟 | 中 | |
| 7 | 高级模式 | 中 | |
| 8 | 调试与性能 | 低 | |
Quick Reference
快速参考
1. Setup & Initialization (CRITICAL)
1. 安装与初始化(关键)
- - Use correct entrypoint for Node.js (msw/node)
setup-server-node-entrypoint - - Configure server lifecycle in test setup
setup-lifecycle-hooks - - Commit worker script to version control
setup-worker-script-commit - - Require Node.js 18+ for MSW v2
setup-node-version - - Configure unhandled request behavior
setup-unhandled-requests - - Configure TypeScript for MSW v2
setup-typescript-config
- - 为Node.js使用正确的入口(msw/node)
setup-server-node-entrypoint - - 在测试设置中配置服务器生命周期
setup-lifecycle-hooks - - 将Worker脚本提交到版本控制
setup-worker-script-commit - - MSW v2要求Node.js 18及以上版本
setup-node-version - - 配置未处理请求的行为
setup-unhandled-requests - - 为MSW v2配置TypeScript
setup-typescript-config
2. Handler Architecture (CRITICAL)
2. 处理程序架构(关键)
- - Define happy path handlers as baseline
handler-happy-path-first - - Group handlers by domain
handler-domain-grouping - - Use absolute URLs in handlers
handler-absolute-urls - - Extract shared response logic into resolvers
handler-shared-resolvers - - Use MSW v2 response syntax
handler-v2-response-syntax - - Explicitly parse request bodies
handler-request-body-parsing - - Destructure resolver arguments correctly
handler-resolver-argument - - Share handlers across environments
handler-reusability-environments
- - 将正常流程处理程序定义为基准
handler-happy-path-first - - 按域名分组处理程序
handler-domain-grouping - - 在处理程序中使用绝对URL
handler-absolute-urls - - 将共享响应逻辑提取到解析器中
handler-shared-resolvers - - 使用MSW v2响应语法
handler-v2-response-syntax - - 显式解析请求体
handler-request-body-parsing - - 正确解构解析器参数
handler-resolver-argument - - 在多环境间共享处理程序
handler-reusability-environments
3. Test Integration (HIGH)
3. 测试集成(高)
- - Reset handlers after each test
test-reset-handlers - - Avoid direct request assertions
test-avoid-request-assertions - - Use server.boundary() for concurrent tests
test-concurrent-boundary - - Configure fake timers to preserve queueMicrotask
test-fake-timers-config - - Use async testing utilities for mock responses
test-async-utilities - - Clear request library caches between tests
test-clear-request-cache - - Use correct JSDOM environment for Jest
test-jsdom-environment
- - 每次测试后重置处理程序
test-reset-handlers - - 避免直接断言请求
test-avoid-request-assertions - - 对并发测试使用server.boundary()
test-concurrent-boundary - - 配置假计时器以保留queueMicrotask
test-fake-timers-config - - 对模拟响应使用异步测试工具
test-async-utilities - - 在测试间清除请求库缓存
test-clear-request-cache - - 为Jest使用正确的JSDOM环境
test-jsdom-environment
4. Response Patterns (HIGH)
4. 响应模式(高)
- - Use HttpResponse static methods
response-http-response-helpers - - Add realistic response delays
response-delay-realistic - - Simulate error responses correctly
response-error-simulation - - Use one-time handlers for sequential scenarios
response-one-time-handlers - - Set response headers correctly
response-custom-headers - - Mock streaming responses with ReadableStream
response-streaming
- - 使用HttpResponse静态方法
response-http-response-helpers - - 添加真实的响应延迟
response-delay-realistic - - 正确模拟错误响应
response-error-simulation - - 为顺序场景使用一次性处理程序
response-one-time-handlers - - 正确设置响应头
response-custom-headers - - 使用ReadableStream模拟流式响应
response-streaming
5. Request Matching (MEDIUM-HIGH)
5. 请求匹配(中高)
- - Use URL path parameters correctly
match-url-patterns - - Access query parameters from request URL
match-query-params - - Use custom predicates for complex matching
match-custom-predicate - - Match HTTP methods explicitly
match-http-methods - - Order handlers from specific to general
match-handler-order
- - 正确使用URL路径参数
match-url-patterns - - 从请求URL中获取查询参数
match-query-params - - 对复杂匹配使用自定义断言
match-custom-predicate - - 显式匹配HTTP方法
match-http-methods - - 按从具体到通用的顺序排列处理程序
match-handler-order
6. GraphQL Mocking (MEDIUM)
6. GraphQL模拟(中)
- - Use operation name for GraphQL matching
graphql-operation-handlers - - Return GraphQL errors in correct format
graphql-error-responses - - Handle batched GraphQL queries
graphql-batched-queries - - Access GraphQL variables correctly
graphql-variables-access
- - 使用操作名称进行GraphQL匹配
graphql-operation-handlers - - 以正确格式返回GraphQL错误
graphql-error-responses - - 处理批量GraphQL查询
graphql-batched-queries - - 正确获取GraphQL变量
graphql-variables-access
7. Advanced Patterns (MEDIUM)
7. 高级模式(中)
- - Use bypass() for passthrough requests
advanced-bypass-requests - - Handle cookies and authentication
advanced-cookies-auth - - Implement dynamic mock scenarios
advanced-dynamic-scenarios - - Configure MSW for Vitest browser mode
advanced-vitest-browser - - Mock file upload endpoints
advanced-file-uploads
- - 对直通请求使用bypass()
advanced-bypass-requests - - 处理Cookie与身份验证
advanced-cookies-auth - - 实现动态模拟场景
advanced-dynamic-scenarios - - 为Vitest浏览器模式配置MSW
advanced-vitest-browser - - 模拟文件上传端点
advanced-file-uploads
8. Debugging & Performance (LOW)
8. 调试与性能(低)
- - Use lifecycle events for debugging
debug-lifecycle-events - - Verify request interception is working
debug-verify-interception - - Know common MSW issues and fixes
debug-common-issues - - Log request details for debugging
debug-request-logging
- - 使用生命周期事件进行调试
debug-lifecycle-events - - 验证请求拦截是否正常工作
debug-verify-interception - - 了解MSW常见问题及修复方法
debug-common-issues - - 记录请求详情用于调试
debug-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
阅读各参考文件获取详细说明和代码示例:
- 章节定义 - 类别结构和影响程度
- 规则模板 - 添加新规则的模板
- 单个规则:
references/{prefix}-{slug}.md
Related Skills
相关技能
- For generating MSW mocks from OpenAPI, see skill
orval - For consuming mocked APIs, see skill
tanstack-query - For test methodology, see or
test-vitestskillstest-tdd
- 如需从OpenAPI生成MSW模拟,请查看技能
orval - 如需使用模拟API,请查看技能
tanstack-query - 如需了解测试方法论,请查看或
test-vitest技能test-tdd
Full Compiled Document
完整编译文档
For the complete guide with all rules expanded:
AGENTS.md如需包含所有扩展规则的完整指南,请查看:
AGENTS.md