Fetchception is a library for mocking fetch API calls in tests, version 2.0.1. It allows defining expected requests and corresponding responses, automatically restoring the global fetch after the test. Based on ideas from fileception and httpception, it uses messy and unexpected-messy for HTTP conversation modelling and diffing. Key differentiators: simple declarative API, automatic cleanup, and integration with unexpected for advanced assertions.
npm install fetchceptionNo compatibility data collected yet for this library.
Verified import paths — ran on the pinned version, not inferred.
Shows how to mock a fetch call with a single request/response pair inside a test, with automatic cleanup.
Always return the promise from fetchception to guarantee cleanup.
Always use an array of request/response objects.
For JSON responses, pass an object (will be serialized) or a string if you want raw response body.
Do not rely on abort logic in tests; mock responses are instantaneous.
Install and import a fetch polyfill (e.g., node-fetch) before using fetchception.
Use default import: const fetchception = require('fetchception');Pass an object as response body: response: { body: { foo: 'bar' } } (will be serialized) or use JSON.parse on the string.