Registry / testing / mockjs-fetch

mockjs-fetch

JSON →
library2.0.0jsnpmunverified

Extends Mock.js to support intercepting fetch-based AJAX requests, enabling seamless mocking of fetch API calls alongside existing XMLHttpRequest mocks. Current stable version is 2.0.0, released with a breaking change in import syntax (named export instead of default). Release cadence is low, with only a few versions. Key differentiators: integrates with Mock.js for full mock syntax support, or can be used standalone for simple fetch-only mocking without Mock.js dependency. Important: must be disabled in production to avoid runtime side effects.

npm install mockjs-fetch
INSTALL
IMPORT
SIG · MOCKJS-FETCH
M
mockjs-fetch
testingjavascriptv2.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.

mockFetch
import { mockFetch } from 'mockjs-fetch'
import mockFetch from 'mockjs-fetch'
v2.x uses named export; v1.x used default export
Mock
import { Mock } from 'mockjs-fetch'
import Mock from 'mockjs-fetch'
Standalone Mock is a named export, not default. Use this for independent fetch mocking.
mockFetch (with Mock.js)
import Mock from 'mockjs'; import { mockFetch } from 'mockjs-fetch'; mockFetch(Mock);
import mockFetch from 'mockjs-fetch'; mockFetch();
When integrating with Mock.js, call mockFetch(Mock) to attach to the Mock instance.

Integrates with Mock.js to mock fetch requests with full mock syntax support including data templates.

import Mock from 'mockjs'; import { mockFetch } from 'mockjs-fetch'; mockFetch(Mock); Mock.setup({ timeout: '200-400', debug: true }); Mock.mock(/\/api\/test\.json/, { code: 0, 'data|3': [{ name: '@cname', age: 18 }] }); const response = await fetch('/api/test.json').then(res => res.json()); console.log(response); // Mocked data
Debug
Known issues
breakingImport syntax changed from default import to named import in v2.0.0
fix
Use `import { mockFetch } from 'mockjs-fetch'` instead of `import mockFetch from 'mockjs-fetch'`
affects: >=2.0.0
deprecatedCalling mockFetch without arguments in v2.x does nothing; it must receive Mock instance
fix
Always pass Mock as argument: `mockFetch(Mock)`
affects: >=2.0.0
gotchaMust not include mock code in production; will interfere with real requests
fix
Wrap mock imports in conditional or use build tools to exclude production builds
affects: all
gotchaStandalone mode does not support Mock.js special syntax like 'id|+1'
fix
Use full integration with Mock.js for template syntax, or write plain static data in standalone mode
affects: all
Errors
Common errors & fixes
import mockFetch from 'mockjs-fetch'; // TypeError: mockFetch is not a function
v2.x uses named export, not default export
fix
import { mockFetch } from 'mockjs-fetch';
fetch('/api/test.json').then(...) returns undefined
mockFetch was not called or not passed Mock properly
fix
Ensure mockFetch(Mock) is executed before any fetch calls
TypeError: Mock.mock is not a function
Using standalone Mock from 'mockjs-fetch' incorrectly as default import
fix
import { Mock } from 'mockjs-fetch'; // not import Mock from ...
Upgrade
Version history
2.0.0latest on npm
Audit
Dependencies
mockjsoptionalRequired for full mock syntax support when using mockFetch(Mock); optional if using standalone Mock from mockjs-fetch
Agent activity
2 hits · last 30 days
node
2
Resources
mockjs-fetch — npm install mockjs-fetch · libregistry