Command Palette

Search for a command to run...

UnylyUnyly
Назад к скиллам

msw

БесплатноБез исполняемых скриптовНе проверен

Intercepts network requests to mock REST, GraphQL, SSE and WebSocket APIs with Mock Service Worker (MSW). Use when mocking APIs for unit tests, integration…

TerminalSkillsот TerminalSkills★ 221 звезда на GitHub

Об этом скилле

MSW (Mock Service Worker)

Overview

MSW intercepts requests at the network level: through a service worker in the browser and through request interception in Node.js. The same handlers serve tests, Storybook and local development, and they work with any client (fetch, axios, Apollo) without touching application code. Current release is 3.0 (September 2026). It is ESM-only, needs Node.js 22.12+ and TypeScript 5.9+, and changed several APIs from 2.x. Check npm ls msw first: the examples below are 3.x, and the 2.x differences are listed in Guidelines.

Instructions

  • Install with npm install msw --save-dev. Install graphql too if you mock GraphQL (it is now an optional peer dependency).
  • Define REST handlers with http.get(), http.post() and so on, imported from msw/http (the msw root still re-exports http, HttpResponse and delay). Return HttpResponse.json(body, { status }). In Node.js use absolute URLs; relative ones only resolve in the browser.
  • Define GraphQL handlers from a link: const api = graphql.link('https://api.shopwave.dev/graphql'), then api.query('GetPosts', resolver) and api.mutation('CreatePost', resolver). graphql comes from msw/graphql; the 2.x form graphql.query(...) no longer exists.
  • In Node.js tests, create setupServer(...handlers) from msw/node. Call server.listen() in beforeAll, server.resetHandlers() in afterEach, server.close() in afterAll. listen({ onUnhandledFrame: 'error' }) fails tests on any request without a handler (default is 'warn'; 'bypass' is silent). This option was called onUnhandledRequest in 2.x.
  • In the browser, create setupWorker(...handlers) from msw/browser and run npx msw init ./public --save once to copy mockServiceWorker.js. Start it with await worker.start() before rendering the app, only in development.
  • With Vite, the msw/vite plugin serves the worker script itself: add msw() (import { msw } from 'msw/vite') to plugins, then in src/main.ts call const { network } = await import('virtual:msw'); network.configure({ handlers }); await network.enable() inside if (import.meta.env.DEV). It works for SSR too and is left out of production builds.
  • GraphQL subscriptions are mocked with api.subscription('OnCreateMessage', ({ subscription }) => subscription.publish({ data })); failed WebSocket connections emit websocket:error.
  • Override per test with server.use(...); resetHandlers() removes the overrides again.
  • Simulate problems with await delay(300) (from msw/utils/delay or msw), HttpResponse.error() for a network failure, and non-2xx statuses for server errors.
  • Observe traffic without changing it: server.events.on('request:start', ({ request }) => console.log(request.method, request.url)).

Examples

Example 1: Mock a REST API for Vitest component tests

User request: "Mock my users API in Vitest so the list page can be tested with happy and error paths"

// src/mocks/handlers.ts
import { http, HttpResponse } from 'msw/http'

export const handlers = [
  http.get('http://localhost:3000/api/users', () =>
    HttpResponse.json([{ id: 'u_101', name: 'Maya Chen' }, { id: 'u_102', name: 'Dmitri Volkov' }]),
  ),
  http.post('http://localhost:3000/api/users', async ({ request }) => {
    const body = await request.json()
    return HttpResponse.json({ id: 'u_103', ...body }, { status: 201 })
  }),
]
// src/mocks/node.ts
import { setupServer } from 'msw/node'
import { handlers } from './handlers'
export const server = setupServer(...handlers)

// vitest.setup.ts (listed under setupFiles in vitest.config.ts)
import { beforeAll, afterEach, afterAll } from 'vitest'
import { server } from './src/mocks/node'
beforeAll(() => server.listen({ onUnhandledFrame: 'error' }))
afterEach(() => server.resetHandlers())
afterAll(() => server.close())

In the error-state test, add server.use(http.get('http://localhost:3000/api/users', () => HttpResponse.json(null, { status: 500 }))) before rendering.

Result: npx vitest run passes; the happy-path test sees two users, the error test sees the 500, and an accidental call to an unmocked URL fails with [MSW] Error: intercepted a request without a matching request handler.

Example 2: Mock a GraphQL API during local development

User request: "Run my Vite app against a fake GraphQL backend with a loading delay"

// src/mocks/handlers.ts
import { HttpResponse } from 'msw/http'
import { graphql } from 'msw/graphql'
import { delay } from 'msw/utils/delay'

const api = graphql.link('https://api.shopwave.dev/graphql')

export const handlers = [
  api.query('GetPosts', async () => {
    await delay(300)
    return HttpResponse.json({ data: { posts: [{ id: 1, title: 'Launch notes' }] } })
  }),
  api.mutation('CreatePost', ({ variables }) =>
    HttpResponse.json({ data: { createPost: { id: 2, title: variables.title } } }),
  ),
]

Add msw() from msw/vite to vite.config.ts, th

Полный SKILL.md на GitHub

Установить msw в Claude Code и Claude Desktop

Зарегайся, чтобы установить скилл

Создай бесплатный аккаунт, чтобы открыть команду установки и сохранить скилл в библиотеку.

  • Открой команду установки в одну строку
  • Сохраняй скиллы в синхронизируемую библиотеку
  • Уведомления, когда скиллы обновляются
Зарегаться бесплатноУ меня уже есть аккаунт

Разрешённые инструменты

Инструменты, которые скиллу разрешено вызывать.

Без ограничений — скилл может использовать любой инструмент.

Вложенные файлы

_scores.json

FAQ

Что делает скилл msw?

Intercepts network requests to mock REST, GraphQL, SSE and WebSocket APIs with Mock Service Worker (MSW). Use when mocking APIs for unit tests, integration tests, Storybook or local development without changing application code or running a mock server. Trigger words: msw, mock service worker, api mocking, request handlers, setupServer, setupWorker.

Как установить скилл msw?

Скопируй папку скилла в ~/.claude/skills (вкладка Claude Code выше делает это одной командой), либо поставь как плагин.

Скилл msw запускает скрипты?

Нет, скилл состоит только из инструкций (SKILL.md), без исполняемых скриптов.

Похожие скиллы

Сравнить msw с