Registry / testing / react-native-fetch-mock

react-native-fetch-mock

JSON →
library1.0.0jsnpmunverified

A fetch mock library for React Native, v1.0.0. Allows developers to define mock responses for API endpoints using patterns and Mock.js integration. Supports delayed responses, proxy to real servers, fallback to network, and exclusion of certain paths. Useful for testing and development without a backend. Alternatives like fetch-mock exist, but this is tailored for React Native's fetch implementation and offers inline mocking via a mock directory with flexible configuration. No recent updates; considered stable for its feature set.

npm install react-native-fetch-mock
INSTALL
IMPORT
SIG · REACT-NATIVE-FETCH
R
react-native-fetch-mock
testingjavascriptv1.0.0
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.

FetchMock
import FetchMock from 'react-native-fetch-mock'
const FetchMock = require('react-native-fetch-mock')
Default export is the FetchMock class. CommonJS require works but import is preferred for ES6.
fetch
global.fetch = new FetchMock(...).fetch
const fetch = new FetchMock(...)
The instance exposes a fetch property that should be assigned to global.fetch to override the native fetch. Do not call the instance directly.
Mock
import Mock from 'mockjs'
const Mock = require('react-native-fetch-mock').Mock
Mock.js is a peer dependency; its default export is used inside mock handler callbacks to generate data.

Sets up mock API endpoints for GET and POST, initializes FetchMock with options, and assigns it to global.fetch.

// mocks/index.js export default { 'GET /api/users': ({ method, url, params, urlparams, headers }) => { return Mock.mock({ 'list|2': [{ 'id|+1': 1, 'name': '@first @last', 'age|18-54': 1 }] }).list; }, 'POST /api/users': ({ method, url, params, urlparams, headers }) => { return { status: 201, body: { id: 3 } }; } }; // index.js import FetchMock from 'react-native-fetch-mock'; import Mock from 'mockjs'; import mocks from './mocks'; if (__DEV__) { const fetchMock = new FetchMock(mocks, { delay: 200, fetch: global.fetch, exclude: ['http://google.com'], fallbackToNetwork: true, }); global.fetch = fetchMock.fetch; } // Now fetch calls will be intercepted fetch('/api/users') .then(res => res.json()) .then(data => console.log(data));
Debug
Known issues
gotchaYou must call new FetchMock(...).fetch to get the wrapped fetch function; do not use the instance as a function.
fix
Use global.fetch = new FetchMock(mocks, options).fetch;
affects: >=1.0.0
gotchaThe mock handler returns data directly for status 200, but for other statuses you must return an object with status (and optionally body).
fix
Return { status: 201, body: ... } or { status: 204 } for non-200 responses.
affects: >=1.0.0
gotchaThe mock directory should export a default object with route strings as keys and handler functions as values. Nested modules are not supported.
fix
Ensure your mock file uses export default { ... };
affects: >=1.0.0
gotchaThe delay option sets a global delay for all mocked requests. You can override per-request by passing delay in the fetch options.
fix
Pass { delay: 2000 } as second argument to fetch() for a per-call delay.
affects: >=1.0.0
Errors
Common errors & fixes
TypeError: Cannot read property 'fetch' of undefined
FetchMock instance incorrectly created or options.fetch missing.
fix
Ensure you pass options.fetch: global.fetch to the constructor.
ReferenceError: __dev__ is not defined
Using __dev__ without defining it. Typically a babel plugin or environment variable.
fix
Replace with process.env.NODE_ENV !== 'production' or use a babel transform.
Mock is not defined
Mock.js not imported or incorrectly referenced inside handler.
fix
Add import Mock from 'mockjs' at the top of your mock file.
Module not found: Can't resolve 'mockjs'
mockjs peer dependency not installed.
fix
Run npm install mockjs --save-dev
Upgrade
Version history
1.0.0latest on npm
Audit
Dependencies
mockjsrequiredUsed for generating mock data via Mock.js syntax
Agent activity
7 hits · last 30 days
node
6
Amazon
1
Resources
react-native-fetch-mock — npm install react-native-fetch-mock · libregistry