
Security News
AGENTS.md Gains Traction as an Open Format for AI Coding Agents
AGENTS.md is a fast-growing open format giving AI coding agents a shared, predictable way to understand project setup, style, and workflows.
mock-req-res
Advanced tools
Extensible mock req / res objects for use in unit tests of Express controller and middleware functions.
Extensible mock req
/ res
objects for use in unit tests of ExpressJS
controller and middleware functions.
This library assumes:
sinon
version 5 or better.Add mock-req-res
as a devDependency
:
npm i -D mock-req-res
If you are using TypeScript you can add @types/mock-req-res
:
npm i -D @types/mock-req-res
req
To test a controller or middleware function you need to mock a request object.
Do this with:
const req = mockRequest(options)
The options
can be anything you wish to attach or override in the request.
The vanilla mockRequest
gives you the following properties, as well as functions in the form of sinon
stubs.
app: {},
baseUrl: '',
body: {},
cookies: {},
fresh: true,
headers: {},
hostname: '',
ip: '127.0.0.1',
ips: [],
method: 'GET',
originalUrl: '',
params: {},
path: '',
protocol: 'https',
query: {},
route: {},
secure: true,
signedCookies: {},
stale: false,
subdomains: [],
xhr: true,
accepts: stub(),
acceptsCharsets: stub(),
acceptsEncodings: stub(),
acceptsLanguages: stub(),
get: stub(),
is: stub(),
range: stub(),
res
To test a route controller or middleware function you also need to mock a response object.
Do this with:
const res = mockResponse(options)
The options
can be anything you wish to attach or override in the request.
The vanilla mockResponse
gives you the following functions, in the form of sinon
spies and stubs.
app: {},
headersSent: false,
locals: {},
append: spy(),
attachment: spy(),
clearCookie: spy(),
download: spy(),
end: spy(),
format: spy(),
json: spy(),
jsonp: spy(),
links: spy(),
location: spy(),
redirect: spy(),
render: spy(),
send: spy(),
sendFile: spy(),
sendStatus: spy(),
set: spy(),
setHeader: spy(),
type: spy(),
get: stub(),
getHeader: stub(),
cookie: stub().returns(res), // returns itself, allowing chaining
status: stub().returns(res), // returns itself, allowing chaining
vary: stub().returns(res) // returns itself, allowing chaining
Note you can always add other spies or stubs as needed via the options
.
Let's say you have a route controller like this:
const save = require('../../utils/saveThing') // assume this exists.
const createThing = async (req, res) => {
const { name, description } = req.body
if (!name || !description) throw new Error('Invalid Properties')
const saved = await save({ name, description })
res.json(saved)
}
To unit test this you could use Mocha
, Chai
, Sinon
, and Proxyquire
as follows:
const { expect } = require('chai')
const { stub, match } = require('sinon')
const { mockRequest, mockResponse } = require('mock-req-res')
const proxyquire = require('proxyquire')
describe('src/api/things/createThing', () => {
const mockSave = stub()
const createThing = proxyquire('../../src/api/things/createThing', {
'../../utils/saveThing': mockSave
})
const res = mockResponse()
const resetStubs = () => {
mockSave.resetHistory()
res.json.resetHistory()
}
context('happy path', () => {
const name = 'some name'
const description = 'some description'
const req = mockRequest({ body: { name, description } })
const expected = { name, description, id: 1 }
before(async () => {
save.returns(expected)
await createThing(req, res)
})
after(resetStubs)
it('called save with the right data', () => {
expect(save).to.have.been.calledWith(match({ name, description }))
})
it('called res.json with the right data', () => {
expect(res.json).to.have.been.calledWith(match(expected))
})
})
// and also test the various unhappy path scenarios.
})
Branch | Status | Coverage | Audit | Notes |
---|---|---|---|---|
develop | Work in progress | |||
main | Latest stable release |
npm test
— runs the unit tests.npm run test:unit:cov
— runs the unit tests with code coveragenpm run lint
Please see the contributing notes.
FAQs
Extensible mock req / res objects for use in unit tests of Express controller and middleware functions.
The npm package mock-req-res receives a total of 9,628 weekly downloads. As such, mock-req-res popularity was classified as popular.
We found that mock-req-res demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
AGENTS.md is a fast-growing open format giving AI coding agents a shared, predictable way to understand project setup, style, and workflows.
Security News
/Research
Malicious npm package impersonates Nodemailer and drains wallets by hijacking crypto transactions across multiple blockchains.
Security News
This episode explores the hard problem of reachability analysis, from static analysis limits to handling dynamic languages and massive dependency trees.