api-mocking

Implements MSW mock API handlers with stateful stores, filters, and paginated response envelopes.

10|3|Updated Oct 23, 2023
One-click install
npx skills add https://github.com/Layer-Fi/layer-react --skill api-mocking-layer-fi
Or copy as Structured Prompt for Agentβ–Ό
Please help me install this Agent Skill.
Skill: api-mocking
Source: https://github.com/Layer-Fi/layer-react/tree/main/src/msw
Command: npx skills add https://github.com/Layer-Fi/layer-react --skill api-mocking-layer-fi

SYSTEM DOCUMENTATION & REQUIREMENTS

πŸ’‘ This Skill requires msw, effect, date-fns, @internationalized/date.

What problem does it solve? Components and hooks in this repository call the Layer REST API, and every endpoint they touch needs a mock for vitest and Storybook, since unhandled requests fail loudly. This Skill defines the conventions for building those mocks so tests and stories stay consistent, stateful, and honest to the API schemas. ## Core Features & Use Cases - Route-mirroring file layout: Mock files under src/msw/api/** mirror the REST path and the @api hooks file for file, with a CI coverage check that fails when an endpoint lacks a handler. - createMockEndpoint and stateful stores: Each endpoint exposes a default handler plus per-test mock and mockError overrides, backed by in-memory stores that reset automatically between tests so POST-then-GET flows work. - Realistic query behavior: Shared list filters, sorters, and cursor-based pagination helpers make mocks honor query params like search, date ranges, and sorting. - Use Case: When adding a new customers endpoint hook, create the matching get.ts/post.ts mock files, register them in the enclosing handlers.ts, and the component works in both tests and Storybook. ## Quick Start Add an MSW mock for the new endpoint by creating a route-mirroring file under src/msw/api with createMockEndpoint and registering it in the enclosing handlers.ts.

Frequently Asked Questions about api-mocking

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I add a new MSW mock endpoint in this project?β–Ό

Create a file under src/msw/api that mirrors the REST route path, define it with createMockEndpoint using a path starting with a bare * wildcard, and register its handler in the enclosing handlers.ts. The msw:check-coverage script fails CI if an @api method file has no matching handler.

How do I override a mock response in a single test?β–Ό

Call the endpoint's mock(override) method and pass the returned handler to server.use(...) inside the test. Use mockError(body, { status }) for failure states, and onRequest to spy on the cloned request the test sent.

Do MSW mocks here support filtering and pagination query params?β–Ό

Yes. createListFilter applies per-param predicates like matchesQuery and date matchers, createListSorter reads sort_by and sort_order, and paginatedApiData implements real cursor and limit handling so infinite-scroll paths paginate correctly.

Why does my mocked endpoint return a 404 in tests or Storybook?β–Ό

The handler was likely not registered in the enclosing handlers.ts chain up to src/msw/handlers.ts, since unregistered endpoints silently 404. Also verify the path starts with a bare * wildcard so it matches any base URL.

Can MSW handlers import from hooks modules?β–Ό

No. MSW modules must not value-import from @hooks/* because handlers load in every vitest run before per-test mocks apply, which would break unrelated suites. This is enforced as an ESLint error; share contracts through @schemas instead.

How is mock state reset between tests?β–Ό

Every store created with createMockStore registers a reset callback, and resetMockStores() restores seed data automatically via afterEach in vitest.setup.ts and Storybook loaders. Manual resets are not needed and should be avoided.