Registry / testing / reqres

reqres

JSON →
library3.0.1jsnpmunverified

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.

npm install reqres
INSTALL
IMPORT
SIG · REQRES
R
reqres
testingjavascriptv3.0.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.

req
const { req } = require('reqres');
const reqres = require('reqres'); const req = reqres.req;
CommonJS destructuring is typical; default export is the module itself, not a function.
res
const { res } = require('reqres');
const res = require('reqres').res;
Both work, but destructuring is idiomatic.
sinon
const reqres = require('reqres'); reqres.sinon = require('sinon');
import { sinon } from 'reqres';
sinon is not exported; set it as a property on the module. ESM not supported.

Shows creating mock req and res objects with custom properties and invoking middleware.

const { req, res } = require('reqres'); const myMiddleware = (req, res, next) => { req.session = req.session || {}; req.session.path = req.path; next(); }; const mockReq = req({ path: '/test', session: {} }); const mockRes = res(); myMiddleware(mockReq, mockRes, (err) => { console.log(mockReq.session.path); // '/test' });
Debug
Known issues
breakingDefault headers are not set on response object. res.setHeader and res.getHeader may be stubs that do not store values.
fix
Provide custom headers via res({ headers: {...} }) or manually set in tests.
affects: >=3.0.0
deprecatedUsing reqres.req() and reqres.res() as non-destructured calls is deprecated in favor of destructured imports.
fix
Use const { req, res } = require('reqres'); then call req(), res().
affects: >=3.0.0
gotchaThe 'end' event on res is emitted only for methods like json, send, sendStatus, etc. Not all response methods emit it.
fix
Check source or readme for full list of methods that emit 'end'.
affects: >=3.0.0
gotchareqres does not support ES modules (import). Must use require().
fix
Use CommonJS require() or find an ESM-compatible alternative.
affects: >=3.0.0
Errors
Common errors & fixes
TypeError: reqres.req is not a function
Importing named exports incorrectly in CommonJS when using default import pattern.
fix
Use const { req } = require('reqres');
Cannot find module 'sinon'
Sinon is a peer dependency; not installed automatically.
fix
Run npm install sinon --save-dev
AssertionError: expected res.json to have been called with arguments { user: '...' } but it was never called
Response method 'end' event may not be emitted if async middleware doesn't call next() or res.json() before assertion.
fix
Wrap assertion in res.on('end', ...) callback.
Upgrade
Version history
3.0.1latest on npm
Audit
Dependencies
sinonoptionalPeer dependency for stubbing request and response methods
Agent activity
4 hits · last 30 days
node
4
Resources
reqres — npm install reqres · libregistry