{"id":18744,"library":"reqres","title":"reqres","description":"Stub request and response objects for testing Express applications and middleware. Provides `req()` and `res()` functions that create mock objects with sensible defaults and sinon stubs on methods. Version 3.0.1 is current, maintained as needed. Key differentiator: integrates sinon for stub/spy assertions, customisable sinon instance, emits 'end' events on response methods. Alternative to node-mocks-http or express-mocks-http.","status":"active","version":"3.0.1","language":"javascript","source_language":"en","source_url":"https://github.com/lennym/reqres","tags":["javascript","express","stub","testing"],"install":[{"cmd":"npm install reqres","lang":"bash","label":"npm"},{"cmd":"yarn add reqres","lang":"bash","label":"yarn"},{"cmd":"pnpm add reqres","lang":"bash","label":"pnpm"}],"dependencies":[{"reason":"Peer dependency for stubbing request and response methods","package":"sinon","optional":true}],"imports":[{"note":"CommonJS destructuring is typical; default export is the module itself, not a function.","wrong":"const reqres = require('reqres'); const req = reqres.req;","symbol":"req","correct":"const { req } = require('reqres');"},{"note":"Both work, but destructuring is idiomatic.","wrong":"const res = require('reqres').res;","symbol":"res","correct":"const { res } = require('reqres');"},{"note":"sinon is not exported; set it as a property on the module. ESM not supported.","wrong":"import { sinon } from 'reqres';","symbol":"sinon","correct":"const reqres = require('reqres'); reqres.sinon = require('sinon');"}],"quickstart":{"code":"const { req, res } = require('reqres');\nconst myMiddleware = (req, res, next) => {\n  req.session = req.session || {};\n  req.session.path = req.path;\n  next();\n};\n\nconst mockReq = req({ path: '/test', session: {} });\nconst mockRes = res();\n\nmyMiddleware(mockReq, mockRes, (err) => {\n  console.log(mockReq.session.path); // '/test'\n});","lang":"javascript","description":"Shows creating mock req and res objects with custom properties and invoking middleware."},"warnings":[{"fix":"Provide custom headers via res({ headers: {...} }) or manually set in tests.","message":"Default headers are not set on response object. res.setHeader and res.getHeader may be stubs that do not store values.","severity":"breaking","affected_versions":">=3.0.0"},{"fix":"Use const { req, res } = require('reqres'); then call req(), res().","message":"Using reqres.req() and reqres.res() as non-destructured calls is deprecated in favor of destructured imports.","severity":"deprecated","affected_versions":">=3.0.0"},{"fix":"Check source or readme for full list of methods that emit 'end'.","message":"The 'end' event on res is emitted only for methods like json, send, sendStatus, etc. Not all response methods emit it.","severity":"gotcha","affected_versions":">=3.0.0"},{"fix":"Use CommonJS require() or find an ESM-compatible alternative.","message":"reqres does not support ES modules (import). Must use require().","severity":"gotcha","affected_versions":">=3.0.0"}],"env_vars":null,"last_verified":"2026-04-25T00:00:00.000Z","next_check":"2026-07-24T00:00:00.000Z","problems":[{"fix":"Use const { req } = require('reqres');","cause":"Importing named exports incorrectly in CommonJS when using default import pattern.","error":"TypeError: reqres.req is not a function"},{"fix":"Run npm install sinon --save-dev","cause":"Sinon is a peer dependency; not installed automatically.","error":"Cannot find module 'sinon'"},{"fix":"Wrap assertion in res.on('end', ...) callback.","cause":"Response method 'end' event may not be emitted if async middleware doesn't call next() or res.json() before assertion.","error":"AssertionError: expected res.json to have been called with arguments { user: '...' } but it was never called"}],"ecosystem":"npm","meta_description":null,"install_score":null,"install_tag":null,"quickstart_score":null,"quickstart_tag":null}