Registry / testing / express-mock-middleware

express-mock-middleware

JSON →
library0.0.6jsnpmunverified

A simple mock middleware for Express.js that allows you to define mock API responses in separate JavaScript files. Version 0.0.6 is the latest and stable release. It uses a glob pattern to load mock files (e.g., 'mock/**/*.js') where each file exports an object mapping HTTP methods and paths to handler functions. The middleware intercepts matching requests and returns mock data without requiring a real backend. It is a lightweight alternative to full mocking libraries, focusing on simplicity and ease of use. The package has minimal dependencies and is suitable for development and testing environments.

npm install express-mock-middleware
INSTALL
IMPORT
SIG · EXPRESS-MOCK-MIDDL
E
express-mock-middleware
testingjavascriptv0.0.6
Install
Import
Disk
Pass rate
0/ 6
Env Coverage0 / 6
glibc
1822
musl
1822
Install & Compatibility
Where this runs
tested against v? · npm install
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
node 18226 runs
build_error
glibc
node 18226 runs
build_error
Code
Verified usage

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

default
import mockMiddleware from 'express-mock-middleware'
const mockMiddleware = require('express-mock-middleware')
CommonJS require works as well, but ESM import is preferred for modern projects.
mockMiddleware
const mockMiddleware = require('express-mock-middleware')
const { mockMiddleware } = require('express-mock-middleware')
The module exports a single function as default. Named destructuring will not work.
type
import mockMiddleware from 'express-mock-middleware'
import { default as mockMiddleware } from 'express-mock-middleware'
No named export; always import default symbol.

Shows how to set up express-mock-middleware with a glob pattern to load mock files and start an Express server.

const express = require('express'); const mockMiddleware = require('express-mock-middleware'); const path = require('path'); const app = express(); // Define mock files in a directory, e.g., mock/ app.use(mockMiddleware({ glob: 'mock/**/*.js', // relative to process.cwd() cwd: __dirname // optional, resolve from current directory })); app.listen(3000, () => { console.log('Server running on port 3000'); }); // Example mock file: mock/info.js // module.exports = { // 'GET /api/info': (req, res) => { // res.json({ success: true, data: { name: 'test' } }); // } // };
Debug
Known issues
gotchaGlob pattern is relative to process.cwd() by default; use cwd option to change base directory.
fix
Pass cwd: __dirname in options to base path on the current file.
affects: *
gotchaMock files must export an object with keys like 'METHOD /path'. Missing space causes routing failure.
fix
Use exact format: 'GET /api/info' or 'POST /api/submit'.
affects: *
gotchaOnly supports synchronous handlers; async/await or Promises not supported.
fix
Use callback-style middleware or provide a synchronous response.
affects: *
deprecatedPackage has not been updated since 2015 and may contain security vulnerabilities in dependencies.
fix
Consider using a more actively maintained mocking library like 'express-mock-api' or 'msw'.
affects: >=0.0.1
gotchaMiddleware overwrites routes; define mock middleware before other route handlers.
fix
Ensure app.use(mockMiddleware(...)) is placed before app.get(...) or other routes.
affects: *
Errors
Common errors & fixes
TypeError: mockMiddleware is not a function
Destructuring the default export as a named import: const { mockMiddleware } = require('express-mock-middleware');
fix
Use: const mockMiddleware = require('express-mock-middleware'); or import mockMiddleware from 'express-mock-middleware';
Cannot find module 'globby'
Missing dependency globby is not installed automatically in some environments.
fix
Run: npm install globby --save-dev
Mock file not loaded: no match for pattern 'mock/**/*.js'
The glob pattern does not match any files because the cwd is incorrect or the mock directory doesn't exist.
fix
Pass cwd: __dirname in options and ensure the mock directory exists relative to that path.
Route /api/info returned 404
Mock file key format is incorrect (e.g., 'GET/api/info' without space) or file not loaded.
fix
Ensure key is exactly 'GET /api/info' (space between method and path).
Upgrade
Version history
0.0.6latest on npm
Audit
Dependencies
expressrequiredPeer dependency required to use as middleware
globbyrequiredUsed internally to resolve glob patterns for mock files
Agent activity
2 hits · last 30 days
node
2
Resources