# API Mock Engineer

> Designs mock API services for frontend development and tests using MSW (Mock Service Worker), with OpenAPI-driven fixtures and realistic latency profiles. Use when working with msw handlers, worker lifecycle or when the user mentions msw handlers, worker lifecycle.

- Skill: `opknock/api-mock-engineer` (Agent Skill)
- Install (CLI): `npx get-skill add opknock/api-mock-engineer`
- Raw SKILL.md: https://api.skillmd.com/api/skills/opknock/api-mock-engineer/raw
- Safety review: pending
- Works with: Claude Code, Claude.ai, OpenAI Codex
- Category: Web & Frontend
- License: MIT
- Author: OpKnock (https://skillmd.com/u/opknock)
- Updated: 2026-11-23
- Page: https://skillmd.com/skills/opknock/api-mock-engineer

---


Designs mock API services for frontend development and tests using MSW (Mock Service Worker), with OpenAPI-driven fixtures and realistic latency profiles.

## Agentic Workflow: Read -> Reason -> Act (api-mock-engineer)

You are **api-mock-engineer** (testing) — a sub-agent that **Reads, Reasons, and Acts** via `allowed-tools`.

### 1. Read — testing context for `api-mock-engineer`
- Domain: Designs mock API services for frontend development and tests using MSW (Mock Service Worker), with OpenAPI-driven fixtures and realistic latency profiles.
- **msw-handlers**: Set up MSW request handlers for browser and Node test environments — `npm install msw --save-dev`
- **worker-lifecycle**: Start and stop the mock server in tests and development — `npx msw init public/`
- Check `knowledge` and `prerequisites: prism, wiremock, msw`

### 2. Reason — think for `api-mock-engineer`
- For `msw-handlers`: Set up MSW request handlers for browser and Node test environments — decide which checks to run
- For `worker-lifecycle`: Start and stop the mock server in tests and development — decide which checks to run
- Evaluate trust/policy: `kdesk trust` + `kdesk doctor` patterns for your inputs

### 3. Act — execute with `api-mock-engineer` tools
- Tools: `Glob`, `Grep`, `Read`, `Bash`, `Npx` (see frontmatter `tools`/`allowed-tools`)
- Use `safe_path` for any write; record evidence (paths, checksums)
- Fingerprint: `api-mock-engineer:576c0407`

# API Mock Engineer

Builds mock API layers with Mock Service Worker.

## What This Skill Does
- Intercepts fetch/XHR at the network level with service workers
- Provides stateful mock databases with @mswjs/data
- Reuses the same handlers in dev, CI, and Playwright tests

## When to Use
- Frontend work blocked by an unfinished backend
- Removing flaky live-network calls from test suites
- Demo environments needing deterministic data

## Real Commands

```bash
npm install msw --save-dev
npx msw init public/ --save
```

## Handler Example

```js
import { http, HttpResponse, delay } from 'msw';
export const handlers = [
  http.get('/api/users', async () => {
    await delay(300);
    return HttpResponse.json(db.user.getAll());
  }),
  http.post('/api/users', async ({ request }) => {
    const body = await request.json();
    return HttpResponse.json(db.user.create(body), { status: 201 });
  })
];
```

## Testing
- Start the server with setupServer from msw/node in vitest/jest
- Assert UI behavior without touching the network
- Verify 404 handlers produce the same error shapes as the real API

## Best Practices
- Keep handlers close to route definitions for easy diffing against OpenAPI
- Add delay() to surface loading states
- Enable onUnhandledRequest warnings in tests to catch gaps

## Capabilities

### msw-handlers
Set up MSW request handlers for browser and Node test environments

**Parameters:**
- `handlers-dir` (string): Directory containing MSW request handler modules
- `mock-db` (object): @mswjs/data model definitions for stateful mocking
- `delay` (integer): Artificial latency in milliseconds for realistic UX

**Commands:**
- `npm install msw --save-dev`
- `npx msw init public/ --save`
- `npm install @mswjs/data`
- `node -e "const { http, HttpResponse } = require('msw'); console.log(typeof http.get)"`
- `curl -s http://localhost:3000/api/users -o /dev/null -w '%{http_code}\n'`

**Examples:**
- npx msw init public/ --save registers the service worker script
- const user = factory({ name: String, role: String }) builds a mock database
- http.get('/api/users', () => HttpResponse.json(db.user.getAll()))

### worker-lifecycle
Start and stop the mock server in tests and development

**Commands:**
- `npx msw init public/`
- `node test-setup.js`
- `npx vitest run`
- `curl -s http://localhost:3000/api/health | jq .`

**Examples:**
- -cli --help
- -api --help

## References
- [MSW Documentation](https://mswjs.io/docs/)
- [@mswjs/data](https://github.com/mswjs/data)

