Pretender is a mock server library for XMLHttpRequest and Fetch in the browser, providing an express/sinatra-style DSL for defining routes and handlers. Current stable version is 3.4.7, released with regular updates. It works by temporarily replacing native XMLHttpRequest and Fetch to intercept all requests. Key differentiators: minimal setup, supports dynamic route segments, timing simulation, and multiple map registration. Note: Only works in browsers, not Node.js.
npm install pretenderNo compatibility data collected yet for this library.
Verified import paths — ran on the pinned version, not inferred.
Shows how to define GET, POST, and dynamic route handlers using Pretender's DSL, with fetch interception.
Use appropriate mocking libraries like nock or msw for Node.js environments.
Use async handlers with setTimeout or similar to simulate latency.
Parse `request.params.id` with `Number()` or `parseInt()` before using as a numeric value.
Update code to pass timing as argument: `this.get(path, handler, timing)` instead of an options object.
Run tests in a browser-like environment (e.g., jsdom, Karma, or Playwright) that provides XMLHttpRequest.
Either import Pretender via ES module (`import Pretender from 'pretender'`) or include the UMD bundle in the page.
Always return an array with exactly three elements: `[statusCode, headerObject, bodyString]`.