Registry / testing / fakerest

fakerest

JSON →
library4.2.1jsnpmunverified

A browser library that intercepts AJAX calls to mock a REST server based on JSON data. Version 4.2.1 includes TypeScript types and supports MSW, fetch-mock, and Sinon for interception. It enables client-side testing of SPA REST clients without a live server by patching XMLHttpRequest or fetch. Differentiators include out-of-box support for multiple mocking libraries, automatic CRUD routes from a simple data object (resources with records), and compatibility with react-admin. Release cadence is stable with periodic updates.

npm install fakerest
INSTALL
IMPORT
SIG · FAKEREST
F
fakerest
testingjavascriptv4.2.1
harness data pending
Install & Compatibility
Where this runs

No compatibility data collected yet for this library.

Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

getMswHandler
import { getMswHandler } from 'fakerest'
const getMswHandler = require('fakerest').getMswHandler
ESM-only since v4; CJS require() fails in Node (browser lib).
getFetchMockHandler
import { getFetchMockHandler } from 'fakerest'
Named export, no default export. TypeScript types included.
getSinonHandler
import { getSinonHandler } from 'fakerest'
import FakeRest from 'fakerest'
No default export; import specific handler functions only.

Creates an MSW handler that intercepts all fetch requests to localhost:3000 and returns REST-like responses based on provided JSON data.

import { http } from 'msw'; import { setupWorker } from 'msw/browser'; import { getMswHandler } from 'fakerest'; const handler = getMswHandler({ baseUrl: 'http://localhost:3000', data: { authors: [ { id: 0, first_name: 'Leo', last_name: 'Tolstoi' }, { id: 1, first_name: 'Jane', last_name: 'Austen' } ], books: [ { id: 0, title: 'War and Peace' }, { id: 1, title: 'Pride and Prejudice' } ] } }); export const worker = setupWorker( http.all(/http:\/\/localhost:3000/, handler) ); worker.start({ quiet: true, onUnhandledRequest: 'bypass' });
Debug
Known issues
gotchaThe data object must have string keys for resource names; nested objects within a resource are treated as strings, not automatically expanded.
fix
Ensure each resource is an array of objects (for list) or a single object (for singleton).
affects: *
gotchaOnly supports RESTful requests for resources; non-REST endpoints (e.g., /auth) will return 404 unless manually handled.
fix
Add custom handlers via MSW/fetch-mock/Sinon for non-REST routes.
affects: *
gotchaUsing with MSW requires a RegExp or string matcher, not a function matcher, for the handler to work correctly.
fix
Use http.all(/\/api\//, handler) instead of http.all('*/api/*', handler).
affects: >=4.0
Errors
Common errors & fixes
TypeError: Cannot read properties of undefined (reading 'id')
Data object missing an 'id' field in one of the resource arrays.
fix
Ensure all resource items have an 'id' property (or configure idAttribute in options).
Error: [MSW] Failed to register a service worker
MSW service worker not initialized or wrong public directory.
fix
Run `npx msw init public` (or your public dir) to generate the mockServiceWorker.js file.
FakeRest: unknown resource 'settings'
Data object contains a non-array top-level key; FakeRest expects arrays for resources (except singleton).
fix
Wrap singleton data in an array or use a singleton resource pattern (see docs).
Upgrade
Version history
4.2.1latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
4 hits · last 30 days
node
4
Resources
fakerest — npm install fakerest · libregistry