$ show humanwhocodes/mentoss
Mentoss is a utility library for mocking JavaScript fetch() requests and responses in testing environments. It provides three primary classes: MockServer for defining request/response routes, FetchMocker for creating mocked fetch functions that work with one or more MockServers, and MockAgent for undici Dispatcher support in Node.js environments. The library handles complex HTTP features including CORS, credentials, redirects, and various request matching scenarios (URL parameters, headers, body content). The architecture is modular and well-separated, with distinct responsibilities for route matching (request-matcher.js), CORS handling (cors.js), cookie management (cookie-credentials.js), and request/response processing. Each MockServer instance maintains its own route registry and tracks which routes have been called, enabling precise test assertions. The FetchMocker coordinates multiple servers and handles cross-origin requests with proper CORS preflighting and credential attachment. Mentoss is designed for use in test suites where developers need to simulate HTTP interactions without making actual network calls. It supports both browser-like fetch() behavior (with baseUrl and credentials options) and Node.js undici/fetch() usage (via MockAgent). The library includes comprehensive testing helpers to verify which routes were called, assert all routes were exercised, and diagnose mismatched requests through detailed tracing.
The LLM identified 2 files worth highlighting for reuse:
Implements sophisticated HTTP request matching logic supporting URL patterns, query parameters, headers, and body content (JSON, FormData, ArrayBuffer). Provides detailed trace output for debugging mismatches, showing exactly which parts of a request pattern failed to match. The module is self-contained with minimal dependencies (only urlpattern-polyfill) and could be extracted for use in any project needing precise HTTP request matching with comprehensive error reporting.
Complete implementation of the Fetch API CORS specification handling simple requests, preflighted requests, and credentialed requests. Includes validation of request/response headers, method checking, and proper error messaging according to the spec. The module has no internal dependencies and implements complex CORS logic including Access-Control-* header processing, preflight caching considerations, and secure credential handling. Could be reused in any project requiring standards-compliant CORS handling for fetch-based HTTP clients.