msw

利用 Mock Service Worker (MSW) 拦截网络请求以实现 API 模拟。适用于在不修改应用代码或运行模拟服务器的情况下,为单元测试、集成测试或本地开发模拟 REST 或 GraphQL API。触发词包含 msw、mock service worker、api mocking、test mocking、request handlers、setupServer。

仓库最近推送
GitHub Stars
219
Forks
38
Issues / PRs
7

安装方式

先用 Prompt 核对来源与依赖。也可下载安装包。

用 AI 助手安装

复制提示词,让助手先核对固定版本和依赖。

查看 Prompt收起 Prompt
先检查当前 Agent 是否已安装 SkillPI Skill Finder。如果尚未安装,请按官方仓库的说明安装并启用:https://github.com/Findskills/skillpi。然后使用 Skill Finder 打开此 SkillPI 详情页并定位 "msw":https://skillpi.com/zh/skills/deepskill-a7cef40e9bbd7fcb403d889bde43b3ab4e864ae3a72a1ae5c9c19dbbbffd3cbb。安装前核对上游来源和版本:https://github.com/TerminalSkills/skills。来源不一致时停止并说明差异。核对通过后,用 Skill Finder 安装目标 Skill,并报告安装路径和来源版本。

来源与配置

github.com

配置方式待核实

请查看原始来源中的适用工具、依赖和权限要求。

安装实测待补充

利用 Mock Service Worker (MSW) 拦截网络请求以实现 API 模拟。适用于在不修改应用代码或运行模拟服务器的情况下,为单元测试、集成测试或本地开发模拟 REST 或 GraphQL API。触发词包含 msw、mock service worker、api mocking、test mocking、request handlers、setupServer。

0 下载

介绍来源: third_party · TerminalSkills · 1.0.0 参考来源

SkillPi 解读

实测状态
未测试

文件浏览

在原站浏览文件 ↗

原始 SKILL.md

打开原文 ↗
Frontmatter
name: msw
description: >-
  Assists with intercepting network requests for API mocking using Mock Service Worker (MSW).
  Use when mocking REST or GraphQL APIs for unit tests, integration tests, or local
  development without modifying application code or running mock servers. Trigger words:
  msw, mock service worker, api mocking, test mocking, request handlers, setupServer.
license: Apache-2.0
compatibility: "Requires Node.js 18+"
metadata:
  author: terminal-skills
  version: "1.0.0"
  category: development
  tags: ["msw", "testing", "api-mocking", "rest", "graphql"]

MSW (Mock Service Worker)

Overview

MSW intercepts network requests at the service worker level (browser) or in-memory (Node.js) to mock REST and GraphQL APIs for tests and local development. It uses the same handlers for both environments, keeping mocks consistent, and works transparently with any HTTP client (fetch, axios, Apollo) without modifying application code.

Instructions

  • When setting up handlers, define REST handlers with http.get(), http.post(), etc. and GraphQL handlers with graphql.query() and graphql.mutation(), returning responses via HttpResponse.json().
  • When testing in Node.js, use setupServer(...handlers) with server.listen() before tests, server.resetHandlers() between tests, and server.close() after all tests.
  • When developing in the browser, use setupWorker(...handlers) and run npx msw init ./public to generate the service worker file, which intercepts requests visible in DevTools.
  • When overriding per test, use server.use() to add temporary handlers for error states or edge cases, which scope to the current test and reset afterward.
  • When simulating network issues, use delay(ms) for latency, HttpResponse.error() for failures, and custom status codes for error responses.
  • When organizing handlers, keep shared handlers in src/mocks/handlers.ts for reuse across test files and the dev server, with per-test overrides via server.use().

Examples

Example 1: Mock a REST API for component tests

User request: "Set up MSW to mock my user API for React Testing Library tests"

Actions:

  1. Define handlers in src/mocks/handlers.ts for GET /api/users, POST /api/users, and GET /api/users/:id
  2. Set up setupServer(...handlers) in the test setup file with beforeAll/afterEach/afterAll hooks
  3. Write component tests that render with data from the mock API
  4. Add per-test error overrides with server.use(http.get("/api/users", () => HttpResponse.json(null, { status: 500 })))

Output: Component tests with realistic API mocking, including happy path and error state coverage.

Example 2: Mock a GraphQL API for development

User request: "Set up MSW to mock my GraphQL API during local development"

Actions:

  1. Define GraphQL handlers for queries (GetPosts, GetUser) and mutations (CreatePost)
  2. Set up setupWorker(...handlers) in the browser entry point with conditional activation
  3. Add delay(300) to simulate realistic network latency
  4. Run npx msw init ./public and start the dev server

Output: A development environment with mocked GraphQL API visible in browser DevTools, with realistic latency.

Guidelines

  • Use MSW in both tests and development with the same handlers to keep mocks consistent.
  • Define handlers in a shared src/mocks/handlers.ts file for reuse across test files and the dev server.
  • Use server.use() for per-test overrides; keep the default happy path in shared handlers.
  • Always mock error states in tests to verify error handling works correctly.
  • Use delay() in development mocks to simulate real latency and catch loading state bugs.
  • Reset handlers after each test with afterEach(() => server.resetHandlers()) to prevent test pollution.

当前收录版本

1.0.0

核对来源版本 ↗

安全与评测信息

质量评测

满分100 · 来自 SkillPi LAB 实验室的评测报告。

场景价值75/100

亮点

  • 提供 MSW API 模拟的完整解决方案,适用于测试和开发场景
  • 解决了前端开发中不修改代码即可模拟 API 的常见痛点
触发精准85/100

亮点

  • 触发词覆盖全面,包含 msw、mock service worker、api mocking 等多个相关术语
  • 边界清晰,不易与其他 Skill 混淆
结构规范90/100

亮点

  • 文档结构完整,包含概述、使用说明、示例和最佳实践
  • 提供了 REST 和 GraphQL 两种 API 类型的具体示例
  • frontmatter 信息完整,包含兼容性要求和元数据
执行可靠88/100

亮点

  • 工具使用方式描述准确,参数说明清晰
  • 覆盖了测试环境和开发环境的完整 setup 流程
  • 提供了错误状态模拟和网络延迟等高级用法
证据可验 (前四项均值)85/100

取前四项原始分值的算术平均,四舍五入取整。可通过收录报告核对各项评分与说明,运行记录需单独查证。

正文静态检测

100 / 100 · skillpi-20261123-no-r3-v1

正文未命中当前规则。

分数仅反映正文中的规则命中,供使用前参考。附件与运行行为不在此评分范围内。

权限与使用前检查

根据当前主文档自动整理,命中内容可能是示例或防护说明;不是实际权限声明,也未覆盖附件、外部依赖或运行行为。

  • 安装或获取外部依赖

    使用官方来源并锁定版本,在隔离环境检查安装脚本。当前文档检查不覆盖后续下载的代码。

    主文档提及 · 第 27, 54 行

本站展示提示不会授予权限。涉及文件改动、密钥、外部发送或费用时,仍由你使用的工具按需确认。

安全评级与访问范围

safe · 静态证据核验,不代表运行安全保证。

使用前请由宿主核对并授权实际文件读写、外部服务和联网目的地;本站的核验不会授予任何运行权限。