Registry / testing / mock-req-res

mock-req-res

JSON →
library1.2.1jsnpmunverified

Library for creating mock Express request and response objects for unit testing controllers and middleware. Version 1.2.1 works with Sinon >10.0.0 and Node >=8.10.0. Provides mockRequest and mockResponse functions that return sinon stubs/spies for all standard Express req/res properties. Supports extensibility via options parameter. Lightweight and focused on isolated unit tests, unlike full integration tools like supertest. Includes TypeScript definitions via @types/mock-req-res. Release cadence is slow, with minor updates as needed.

npm install mock-req-res
INSTALL
IMPORT
SIG · MOCK-REQ-RES
M
mock-req-res
testingjavascriptv1.2.1
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.

mockRequest
import { mockRequest } from 'mock-req-res'
const mockRequest = require('mock-req-res').mockRequest
Named export; can be destructured from CJS require or ESM import.
mockResponse
import { mockResponse } from 'mock-req-res'
const mockResponse = require('mock-req-res')
Not the default export; must destructure or use .mockResponse.
mockRequest and mockResponse (both)
import { mockRequest, mockResponse } from 'mock-req-res'
Both are exported as named exports.

Shows basic usage: creating mock req and res, invoking a controller, and asserting on sinon stubs/spies.

const { mockRequest, mockResponse } = require('mock-req-res'); const sinon = require('sinon'); // Sample controller that uses req.body and res.json const createThing = (req, res) => { const { name, description } = req.body; if (!name || !description) { return res.status(400).json({ error: 'Invalid properties' }); } res.json({ id: 1, name, description }); }; // Test setup const req = mockRequest({ body: { name: 'test', description: 'desc' } }); const res = mockResponse(); // Invoke controller createThing(req, res); // Assertions console.log(res.json.calledOnce); // true console.log(res.json.firstCall.args[0]); // { id: 1, name: 'test', description: 'desc' } console.log(res.status.called); // false (no error)
Debug
Known issues
breakingVersion 1.0.0 changed the API; mockRequest and mockResponse replaced earlier mockReq and mockRes functions.
fix
Replace mockReq with mockRequest and mockRes with mockResponse. Update import paths.
affects: <1.0.0
deprecatedOlder versions (≤1.1.6) used sinon <10; peer dep now requires sinon >10.0.0.
fix
Upgrade sinon to at least 10.0.0.
affects: >=1.0.0 <1.2.0
gotchaThe mockRequest and mockResponse functions are not constructors; do not use 'new'.
fix
Call normally: mockRequest() or mockResponse(), not new mockRequest().
affects: >=1.0.0
gotchaHeaders are not populated by default until version 1.1.0; test code may rely on headers being present.
fix
Provide headers in options: mockRequest({ headers: { 'content-type': 'application/json' } }).
affects: <1.1.0
gotchaTypeScript definitions are provided by @types/mock-req-res, not included in the main package.
fix
Install @types/mock-req-res as a devDependency if using TypeScript.
affects: >=1.0.0
Errors
Common errors & fixes
TypeError: mockRequest is not a constructor
Attempting to use 'new mockRequest()' instead of calling it as a function.
fix
Remove 'new': const req = mockRequest(options);
Cannot find module 'sinon'
Sinon is a peer dependency and must be installed separately.
fix
Run: npm install sinon --save-dev (ensure compatible version).
TypeError: mockResponse(...).json is not a function
Likely using a very old version (<1.0.0) where .json did not exist; or failing to pass options that include json.
fix
Upgrade to latest version: npm install mock-req-res@latest --save-dev. The default mockResponse includes .json as a spy.
Upgrade
Version history
1.2.1latest on npm
Audit
Dependencies
sinonrequiredPeer dependency required for mocking; the library uses sinon stubs and spies internally.
Agent activity
1 hits · last 30 days
ahrefsbot
1
Resources
mock-req-res — npm install mock-req-res · libregistry