mock-req-res

raw JSON →
1.2.1 verified Sat Apr 25 auth: no javascript

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.

error TypeError: mockRequest is not a constructor
cause Attempting to use 'new mockRequest()' instead of calling it as a function.
fix
Remove 'new': const req = mockRequest(options);
error Cannot find module 'sinon'
cause Sinon is a peer dependency and must be installed separately.
fix
Run: npm install sinon --save-dev (ensure compatible version).
error TypeError: mockResponse(...).json is not a function
cause 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.
breaking Version 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.
deprecated Older versions (≤1.1.6) used sinon <10; peer dep now requires sinon >10.0.0.
fix Upgrade sinon to at least 10.0.0.
gotcha The mockRequest and mockResponse functions are not constructors; do not use 'new'.
fix Call normally: mockRequest() or mockResponse(), not new mockRequest().
gotcha Headers 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' } }).
gotcha TypeScript 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.
npm install mock-req-res
yarn add mock-req-res
pnpm add mock-req-res

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)