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-resVerified import paths — ran on the pinned version, not inferred.
Shows basic usage: creating mock req and res, invoking a controller, and asserting on sinon stubs/spies.
Replace mockReq with mockRequest and mockRes with mockResponse. Update import paths.
Upgrade sinon to at least 10.0.0.
Call normally: mockRequest() or mockResponse(), not new mockRequest().
Provide headers in options: mockRequest({ headers: { 'content-type': 'application/json' } }).Install @types/mock-req-res as a devDependency if using TypeScript.
Remove 'new': const req = mockRequest(options);
Run: npm install sinon --save-dev (ensure compatible version).
Upgrade to latest version: npm install mock-req-res@latest --save-dev. The default mockResponse includes .json as a spy.