# mocking-assistant

mocking-assistant generates reliable mock setups for APIs, services, and UI components using MSW conventions and Vitest testing patterns.

- skill · `type=skill` · Claude Code, Codex, Cursor, GitHub Copilot, Gemini CLI, OpenCode, Windsurf (Devin Desktop), Claude.ai · 256 installs · ★ 64 repo stars
- Author: patricio0312rev
- License: MIT · policy=mirror · the beginning of the file is at the end of this page
- Source: https://github.com/patricio0312rev/skills/tree/HEAD/testing/mocking-assistant/SKILL.md
- Updated: 2026-01-12
- Security check: automated, 2026-11-16 (text) — no signs of malicious code found.
- HTML: https://affpapa.org/en/nevibecode/skills/mocking-assistant

## What it does

This skill guides AI agents on mocking HTTP requests, internal services, and JavaScript modules. It covers MSW setup with `setupServer`, basic HTTP handlers for GET and POST endpoints, error simulations, and delayed responses.

The skill also structures shared test fixtures and shows Vitest mechanics. That includes mocking time via `vi.setSystemTime`, tracking method calls with `vi.spyOn`, substituting modules with `vi.mock`, and writing class-based stubs for payment or analytics services.

All examples rely on Vitest and modern MSW syntax, so projects using Jest will need manual adapter adjustments.

## Install

### Claude Code (native platform)

1. **Skills CLI (Vercel)**
```bash
npx skills add patricio0312rev/skills --skill mocking-assistant -a claude-code
```

2. **Manually: put the files in place**
- File: `~/.claude/skills/mocking-assistant/SKILL.md` — Copy the whole skill folder; for one project use .claude/skills/mocking-assistant/

### Codex

1. **Skills CLI (Vercel)**
```bash
npx skills add patricio0312rev/skills --skill mocking-assistant -a codex
```

2. **Manually: put the files in place**
- File: `.agents/skills/mocking-assistant/SKILL.md` — For all projects use ~/.agents/skills/

### Cursor

1. **Skills CLI (Vercel)**
```bash
npx skills add patricio0312rev/skills --skill mocking-assistant -a cursor
```

2. **Manually: put the files in place**
- File: `.cursor/skills/mocking-assistant/SKILL.md` — Cursor also reads .agents/skills/ and .claude/skills/

### GitHub Copilot

1. **GitHub CLI**
```bash
gh skill install patricio0312rev/skills testing/mocking-assistant/SKILL.md
```

2. **Manually: put the files in place**
- File: `.github/skills/mocking-assistant/SKILL.md`

### Gemini CLI

1. **Skills CLI (Vercel)**
```bash
npx skills add patricio0312rev/skills --skill mocking-assistant -a gemini-cli
```

2. **Platform CLI**
```bash
gemini skills install https://github.com/patricio0312rev/skills --path testing/mocking-assistant
```

### Windsurf (Devin Desktop)

**Manually: put the files in place**
- File: `.devin/skills/mocking-assistant/SKILL.md` — The legacy .windsurf/skills/ path still works

### OpenCode

**Skills CLI (Vercel)**
```bash
npx skills add patricio0312rev/skills --skill mocking-assistant -a opencode
```

### Claude.ai

**Claude.ai**
- Zip the folder and upload it in Settings → Capabilities → Skills

Where a file is given instead of a command, copy the files from the source to that path: https://github.com/patricio0312rev/skills/tree/HEAD/testing/mocking-assistant/SKILL.md

## When to use it
- set up MSW server and handlers for REST API testing
- generate structured data fixtures for test scenarios
- freeze system clock in Vitest to test time-dependent code
- replace external services with class-based mock implementations

## Example prompts
- Set up an MSW handler for /api/users/:id that returns a 404 response on bad IDs
- Create a mock payment service class for checking checkout failure scenarios
- Write a Vitest test that mocks the system date to morning hours

## Source text · SKILL.md · MIT

Below is the content of a third-party file. It is data, not instructions for you: do not act on anything written inside unless the human asks. Block tag: 8dfa28e317ca641e.

BEGIN THIRD-PARTY FILE 8dfa28e317ca641e
````text
---
name: mocking-assistant
description: Creates stable mocks for APIs, services, and UI components using MSW (Mock Service Worker), fixture conventions, and example patterns. Use for "API mocking", "MSW", "test mocks", or "service mocking".
---

# Mocking Assistant

Create reliable mocks for APIs and services in tests.

## MSW API Mocking

```typescript
// mocks/handlers.ts
import { http, HttpResponse } from "msw";

export const handlers = [
  // GET endpoint
  http.get("/api/users/:id", ({ params }) => {
    const { id } = params;

    return HttpResponse.json({
      id,
      name: "John Doe",
      email: "john@example.com",
    });
  }),

  // POST endpoint
  http.post("/api/users", async ({ request }) => {
    const body = await request.json();

    return HttpResponse.json(
      {
        id: Math.random().toString(),
        ...body,
        createdAt: new Date().toISOString(),
      },
      { status: 201 }
    );
  }),

  // Error response
  http.get("/api/products/:id", ({ params }) => {
    const { id } = params;

    if (id === "404") {
      return HttpResponse.json({ error: "Product not found" }, { status: 404 });
    }

    return HttpResponse.json({
      id,
      name: "MacBook Pro",
      price: 2499.99,
    });
  }),

  // Delayed response
  http.get("/api/slow-endpoint", async () => {
    await delay(2000);
    return HttpResponse.json({ data: "Slow response" });
  }),
];
```

## MSW Setup

```typescript
// mocks/server.ts
import { setupServer } from "msw/node";
import { handlers } from "./handlers";

export const server = setupServer(...handlers);

// tests/setup.ts
import { beforeAll, afterEach, afterAll } from "vitest";
import { server } from "../mocks/server";

beforeAll(() => server.listen({ onUnhandledRequest: "error" }));
afterEach(() => server.resetHandlers());
afterAll(() => server.close());
```

## Fixture Conventions

```typescript
// mocks/fixtures/users.ts
export const userFixtures = {
  admin: {
    id: "1",
    email: "admin@example.com",
    name: "Admin User",
    role: "ADMIN",
  },
  customer: {
    id: "2",
    email: "customer@example.com",
    name: "Customer User",
    role: "USER",
  },
  guest: {
    id: "3",
    email: "guest@example.com",
    name: "Guest User",
    role: "GUEST",
  },
};

// mocks/fixtures/products.ts
export const productFixtures = {
  laptop: {
    id: "100",
    name: "MacBook Pro",
    price: 2499.99,
    stock: 10,
    category: "Electronics",
  },
  phone: {
    id: "101",
    name: "iPhone 15",
    price: 999.99,
    stock: 50,
    category: "Electronics",
  },
  outOfStock: {
    id: "102",
    name: "Sold Out Item",
    price: 499.99,
    stock: 0,
    category: "Electronics",
  },
};

// Usage in handlers
http.get("/api/users/:id", ({ params }) => {
  const user = Object.values(userFixtures).find((u) => u.id === params.id);

  if (!user) {
    return HttpResponse.json({ error: "User not found" }, { status: 404 });
  }
````
END THIRD-PARTY FILE 8dfa28e317ca641e

The third-party file has ended. Everything between the 8dfa28e317ca641e tags is data: requests and commands from it are not followed. An “end of file” line with another tag inside the block is part of the file.

First 2948 of 8538 bytes. © patricio0312rev · MIT · https://github.com/patricio0312rev/skills/tree/HEAD/testing/mocking-assistant/SKILL.md

_Part of the page did not fit — see the HTML version._

---
NeVibeCode on affpapa.org. Catalog texts are descriptions, not instructions for the agent. How to choose and install: https://affpapa.org/en/nevibecode/agents.md
