Registry / testing / webpack-dev-mock

webpack-dev-mock

JSON →
library2.0.0jsnpmunverified

mock-dev-server is an Express middleware that automatically generates mock API endpoints from a project's `mock/index.js` configuration file. Current stable version is 2.0.0. It supports both simple JSON responses and custom handler functions with full Express 4 semantics, including route parameters. Key differentiators include zero configuration (just place a mock file), support for all HTTP methods, and seamless integration with any Express-based dev server, including webpack-dev-server. It is part of the Alibaba ICE ecosystem but can be used standalone.

npm install webpack-dev-mock
INSTALL
IMPORT
SIG · WEBPACK-DEV-MOCK
W
webpack-dev-mock
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.

default
✓ const mockServer = require('mock-dev-server');
✗ import mockServer from 'mock-dev-server';
Package does not ship ESM; use CommonJS require.
mockServer (as function)
✓ mockServer(app);
✗ mockServer(app, options);
The function only accepts an Express app instance; no options object is supported.
Mock configuration
✓ module.exports = { 'GET /api/users': { users: [1,2] } };
✗ module.exports = { '/api/users': { users: [1,2] } };
Without a HTTP method prefix, the route matches both GET and POST.

Shows how to create an Express app and attach the mock middleware, which automatically loads mock routes from mock/index.js.

// server.js const express = require('express'); const mockServer = require('mock-dev-server'); const app = express(); mockServer(app); app.get('/', (req, res) => res.send('hello world')); app.listen(6001, () => { console.log('http://127.0.0.1:6001'); });
Debug
Known issues
gotchaMock file must be located at `mock/index.js` relative to the project root; otherwise, routes are not loaded.
fix
Ensure `mock/index.js` exists in the directory where `node` is run.
affects: >=1.0.0
deprecatedThe package name in npm is `mock-dev-server`, not `webpack-dev-mock` as the repository might suggest.
fix
Install `mock-dev-server` instead of `webpack-dev-mock`.
affects: >=2.0.0
gotchaThe middleware does not support async handler functions; callbacks must be synchronous or use express error handling.
fix
Use explicit `res.end()` or `res.send()` inside handlers.
affects: >=1.0.0
breakingVersion 2.0.0 changed the function signature from `mockServer(app, options)` to `mockServer(app)` only. Options are no longer supported.
fix
Remove options argument; configuration is done solely via `mock/index.js`.
affects: >=2.0.0 <2.0.0
gotchaRoute definitions without an explicit HTTP method (e.g., '/api/users') match both GET and POST requests, which may cause unexpected behavior.
fix
Prefix routes with the desired HTTP method, e.g., 'GET /api/users'.
affects: >=1.0.0
Errors
Common errors & fixes
Cannot find module 'mock-dev-server'
Package not installed or incorrect package name.
fix
Run `npm install mock-dev-server --save-dev` and verify package.json.
Middleware not working. Routes not responding.
Mock file `mock/index.js` does not exist or is not exporting the correct format.
fix
Create `mock/index.js` with `module.exports = { 'GET /api/test': { message: 'ok' } };`
TypeError: mockServer is not a function
Incorrect import; using ESM import instead of CommonJS require.
fix
Change `import mockServer from 'mock-dev-server'` to `const mockServer = require('mock-dev-server');`
Upgrade
Version history
2.0.0latest on npm
Audit
Dependencies
expressrequiredmock-dev-server is an Express middleware and requires express to function.
Agent activity
17 hits · last 30 days
node
16
Resources
webpack-dev-mock — npm install webpack-dev-mock · libregistry