api-testing
Original:🇺🇸 English
Translated
API testing patterns with supertest, MSW, and Vitest. Covers integration testing for REST APIs, HTTP request mocking with Mock Service Worker v2, response assertions, schema validation, test organization, and framework-specific patterns for Express, Fastify, and Hono. Use when writing integration tests for REST APIs, mocking HTTP requests, or testing API endpoints. Use for api-test, supertest, msw, mock-service-worker, integration-test, http-mock, endpoint-test, request-test.
1installs
Sourceoakoss/agent-skills
Added on
NPX Install
npx skill4agent add oakoss/agent-skills api-testingTags
Translated version includes tags in frontmatterSKILL.md Content
View Translation Comparison →API Testing
Overview
API testing validates HTTP endpoints by sending requests and asserting responses, covering status codes, headers, body content, and error handling. Supertest provides a fluent chainable API for integration testing against Express, Fastify, and Hono apps without starting a real server. MSW (Mock Service Worker) v2 intercepts outgoing HTTP requests at the network level, enabling realistic mocking of external services in both Node.js tests and browser environments.
When to use: Integration tests for REST APIs, testing middleware pipelines, validating request/response contracts, mocking third-party APIs in tests, testing error handling and edge cases.
When NOT to use: Unit testing pure functions (use direct assertions), E2E browser testing (use Playwright/Cypress), load/performance testing (use k6/Artillery), testing static file serving.
Quick Reference
| Pattern | API | Key Points |
|---|---|---|
| GET request | | Returns supertest |
| POST with body | | Automatically sets Content-Type |
| Auth header | | Chain before |
| Status assertion | | Chainable with body assertions |
| Body assertion | | Deep equality check |
| Header assertion | | Accepts string or regex |
| MSW HTTP handler | | Intercepts matching requests |
| MSW GraphQL handler | | Intercepts by operation name |
| MSW response | | v2 response format |
| MSW error simulation | | Simulates network failure |
| MSW one-time handler | | Auto-removed after first match |
| MSW per-test override | | Override default handlers in specific tests |
| Schema validation | | Validates response structure with Zod |
| Cookie persistence | | Maintains cookies across requests |
| Fastify inject | | Built-in testing without supertest |
| Hono test client | | Type-safe request builder |
Common Mistakes
| Mistake | Correct Pattern |
|---|---|
| Not awaiting supertest requests | Always |
| Sharing server state between tests | Reset handlers with |
| Mocking fetch/axios directly | Use MSW to intercept at the network level |
Forgetting | Call in |
| Passing Fastify instance to supertest | Use |
| Asserting before response completes | Use |
| Hardcoding test data across many tests | Use factories or fixtures for test data |
| Not testing error responses | Test 4xx and 5xx paths alongside happy paths |
Using | Use |
| Ignoring response headers in assertions | Validate Content-Type, Cache-Control, CORS headers |
Not using | Catch unhandled requests to prevent silent test gaps |
| Testing implementation instead of behavior | Assert on response shape, not internal function calls |
Delegation
- Test structure review: Use agent
Task - Code review: Delegate to agent
code-reviewer - Pattern discovery: Use agent
Explore
If theskill is available, delegate general Vitest configuration and patterns to it. Otherwise, recommend:vitest-testingnpx skills add oakoss/agent-skills --skill vitest-testing
References
- Supertest integration testing patterns for Express, Fastify, and Hono
- MSW request handlers, response resolvers, and server setup
- Test organization, fixtures, factories, and setup/teardown
- Response assertions, status codes, headers, and schema validation