{"id":18566,"library":"mock-req-res","title":"mock-req-res","description":"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.","status":"active","version":"1.2.1","language":"javascript","source_language":"en","source_url":"https://github.com/davesag/mock-req-res","tags":["javascript","express","mocks","testing","request","response"],"install":[{"cmd":"npm install mock-req-res","lang":"bash","label":"npm"},{"cmd":"yarn add mock-req-res","lang":"bash","label":"yarn"},{"cmd":"pnpm add mock-req-res","lang":"bash","label":"pnpm"}],"dependencies":[{"reason":"Peer dependency required for mocking; the library uses sinon stubs and spies internally.","package":"sinon","optional":false}],"imports":[{"note":"Named export; can be destructured from CJS require or ESM import.","wrong":"const mockRequest = require('mock-req-res').mockRequest","symbol":"mockRequest","correct":"import { mockRequest } from 'mock-req-res'"},{"note":"Not the default export; must destructure or use .mockResponse.","wrong":"const mockResponse = require('mock-req-res')","symbol":"mockResponse","correct":"import { mockResponse } from 'mock-req-res'"},{"note":"Both are exported as named exports.","wrong":null,"symbol":"mockRequest and mockResponse (both)","correct":"import { mockRequest, mockResponse } from 'mock-req-res'"}],"quickstart":{"code":"const { mockRequest, mockResponse } = require('mock-req-res');\nconst sinon = require('sinon');\n\n// Sample controller that uses req.body and res.json\nconst createThing = (req, res) => {\n  const { name, description } = req.body;\n  if (!name || !description) {\n    return res.status(400).json({ error: 'Invalid properties' });\n  }\n  res.json({ id: 1, name, description });\n};\n\n// Test setup\nconst req = mockRequest({ body: { name: 'test', description: 'desc' } });\nconst res = mockResponse();\n\n// Invoke controller\ncreateThing(req, res);\n\n// Assertions\nconsole.log(res.json.calledOnce); // true\nconsole.log(res.json.firstCall.args[0]); // { id: 1, name: 'test', description: 'desc' }\nconsole.log(res.status.called); // false (no error)","lang":"javascript","description":"Shows basic usage: creating mock req and res, invoking a controller, and asserting on sinon stubs/spies."},"warnings":[{"fix":"Replace mockReq with mockRequest and mockRes with mockResponse. Update import paths.","message":"Version 1.0.0 changed the API; mockRequest and mockResponse replaced earlier mockReq and mockRes functions.","severity":"breaking","affected_versions":"<1.0.0"},{"fix":"Upgrade sinon to at least 10.0.0.","message":"Older versions (≤1.1.6) used sinon <10; peer dep now requires sinon >10.0.0.","severity":"deprecated","affected_versions":">=1.0.0 <1.2.0"},{"fix":"Call normally: mockRequest() or mockResponse(), not new mockRequest().","message":"The mockRequest and mockResponse functions are not constructors; do not use 'new'.","severity":"gotcha","affected_versions":">=1.0.0"},{"fix":"Provide headers in options: mockRequest({ headers: { 'content-type': 'application/json' } }).","message":"Headers are not populated by default until version 1.1.0; test code may rely on headers being present.","severity":"gotcha","affected_versions":"<1.1.0"},{"fix":"Install @types/mock-req-res as a devDependency if using TypeScript.","message":"TypeScript definitions are provided by @types/mock-req-res, not included in the main package.","severity":"gotcha","affected_versions":">=1.0.0"}],"env_vars":null,"last_verified":"2026-04-25T00:00:00.000Z","next_check":"2026-07-24T00:00:00.000Z","problems":[{"fix":"Remove 'new': const req = mockRequest(options);","cause":"Attempting to use 'new mockRequest()' instead of calling it as a function.","error":"TypeError: mockRequest is not a constructor"},{"fix":"Run: npm install sinon --save-dev (ensure compatible version).","cause":"Sinon is a peer dependency and must be installed separately.","error":"Cannot find module 'sinon'"},{"fix":"Upgrade to latest version: npm install mock-req-res@latest --save-dev. The default mockResponse includes .json as a spy.","cause":"Likely using a very old version (<1.0.0) where .json did not exist; or failing to pass options that include json.","error":"TypeError: mockResponse(...).json is not a function"}],"ecosystem":"npm","meta_description":null,"install_score":null,"install_tag":null,"quickstart_score":null,"quickstart_tag":null}