Installations
npm install jest-mock-req-res
Developer Guide
Typescript
Yes
Module System
CommonJS
Node Version
14.1.0
NPM Version
6.14.4
Score
55.6
Supply Chain
47.5
Quality
70.5
Maintenance
100
Vulnerability
97.3
License
Releases
Unable to fetch releases
Contributors
Unable to fetch Contributors
Languages
TypeScript (100%)
Love this project? Help keep it running — sponsor us today! 🚀
Developer
Carnubak
Download Statistics
Total Downloads
1,230,131
Last Day
2,403
Last Week
11,505
Last Month
47,284
Last Year
587,278
GitHub Statistics
MIT License
3 Stars
20 Commits
2 Watchers
15 Branches
1 Contributors
Updated on Dec 02, 2024
Bundle Size
1.53 kB
Minified
649.00 B
Minified + Gzipped
Package Meta Information
Latest Version
1.0.2
Package Id
jest-mock-req-res@1.0.2
Unpacked Size
14.57 kB
Size
4.37 kB
File Count
10
NPM Version
6.14.4
Node Version
14.1.0
Total Downloads
Cumulative downloads
Total Downloads
1,230,131
Last Day
20.2%
2,403
Compared to previous day
Last Week
6.7%
11,505
Compared to previous week
Last Month
11.7%
47,284
Compared to previous month
Last Year
52.2%
587,278
Compared to previous year
Daily Downloads
Weekly Downloads
Monthly Downloads
Yearly Downloads
jest-mock-req-res
Jest mocks for Express Request
and Response
objects. Inspired by mock-req-res.
Motivation
The mocks provided by mock-req-res
are very useful due to their simplicity of use, but they depend on sinon
, whereas I've been working in teams that use jest
as their weapon of choice for testing. That's why I decided to replace the Sinon stubs with Jest spies. In addition, this package is written in Typescript and typings are provided.
Much like the original mock-req-res
package, the intention of these mocks is to allow you to write properly isolated tests for your Express middleware functions.
Mocks are designed based on the specs for Express version 4.x. These mocks should be compatible with version 5.x, as far as I can tell, except that the host
property is not present in the Request
interface for version 4. You can add it as a custom property when mocking requests (see below).
Dependencies
The only dependency (marked as a peer dependency) is jest
version 13.0.0 or newer, as the syntax for creating mocks is different in earlier versions.
Installation
Simply add this package as a development dependency to your project:
1npm i -D jest-mock-req-res
API
mockRequest(options)
The mockRequest()
function creates a mock Request
object. The options
parameter is an optional object with any keys you want to override or add to the created object. For instance:
1const req1 = mockRequest({ awesome: 'value' }); 2console.log(req1.awesome); 3// => 'value' 4 5const req2 = mockRequest({ hostname: 'example.com' }); 6console.log(req2.hostname); 7// => 'example.com'
The default mock contains the following members:
1app: {}, 2baseUrl: '', 3body: {}, 4cookies: {}, 5fresh: true, 6hostname: '', 7ip: '127.0.0.1', 8ips: [], 9method: '', 10originalUrl: '', 11params: {}, 12path: '', 13protocol: 'https', 14query: {}, 15route: {}, 16secure: true, 17signedCookies: {}, 18stale: false, 19subdomains: [], 20xhr: true, 21accepts: jest.fn(), 22acceptsCharsets: jest.fn(), 23acceptsEncodings: jest.fn(), 24acceptsLanguages: jest.fn(), 25get: jest.fn(), // (*) 26header: jest.fn(), // (*) 27is: jest.fn(), 28range: jest.fn(),
(*) The header()
method is an alias for the get()
method, as provided in Express.
Note that this invariant is not enforced, so if you override either of the header()
or get()
methods, this alias will not work as expected.
mockResponse()
The mockResponse()
function creates a mock Response
object. The options
parameter is an optional object with any keys you want to override or add to the created object. For instance:
1const res1 = mockResponse({ awesome: 'value' }); 2console.log(res1.awesome); 3// => 'value' 4 5const res2 = mockResponse({ locals: { name: 'John Smith' } }); 6console.log(res2.locals.name); 7// => 'John Smith'
The default mock contains the following members:
1 app: {}, 2 headersSent: false, 3 locals: {}, 4 append: jest.fn().mockReturnThis(), 5 attachment: jest.fn().mockReturnThis(), 6 clearCookie: jest.fn().mockReturnThis(), 7 contentType: jest.fn().mockReturnThis(), // (*) 8 cookie: jest.fn().mockReturnThis(), 9 download: jest.fn(), 10 end: jest.fn(), 11 format: jest.fn().mockReturnThis(), 12 get: jest.fn(), 13 header: jest.fn().mockReturnThis(), // (**) 14 json: jest.fn().mockReturnThis(), 15 jsonp: jest.fn().mockReturnThis(), 16 links: jest.fn().mockReturnThis(), 17 location: jest.fn().mockReturnThis(), 18 redirect: jest.fn(), 19 render: jest.fn(), 20 send: jest.fn().mockReturnThis(), 21 sendFile: jest.fn(), 22 sendStatus: jest.fn().mockReturnThis(), 23 set: jest.fn().mockReturnThis(), // (**) 24 status: jest.fn().mockReturnThis(), 25 type: jest.fn().mockReturnThis(), // (*) 26 vary: jest.fn().mockReturnThis(),
(*) The contentType()
method behaves as an alias to the type()
method, as that is the actual Express behavior, though it's not mentioned in the API docs.
(**) The header()
method is an alias for the set()
method, as provided in Express.
Note that these two invariants are not enforced, so if you override any of the contentType()
, header()
, set()
or type()
methods, the aliases will not work as expected.

No vulnerabilities found.
Reason
no binaries found in the repo
Reason
license file detected
Details
- Info: project has a license file: LICENSE:0
- Info: FSF or OSI recognized license: MIT License: LICENSE:0
Reason
0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
Reason
Found 0/16 approved changesets -- score normalized to 0
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
security policy file not detected
Details
- Warn: no security policy file detected
- Warn: no security file to analyze
- Warn: no security file to analyze
- Warn: no security file to analyze
Reason
project is not fuzzed
Details
- Warn: no fuzzer integrations found
Reason
branch protection not enabled on development/release branches
Details
- Warn: branch protection not enabled for branch 'master'
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
- Warn: 0 commits out of 4 are checked with a SAST tool
Reason
45 existing vulnerabilities detected
Details
- Warn: Project is vulnerable to: GHSA-67hx-6x53-jw92
- Warn: Project is vulnerable to: GHSA-93q8-gq69-wqmw
- Warn: Project is vulnerable to: GHSA-grv7-fg5c-xmjg
- Warn: Project is vulnerable to: GHSA-w8qv-6jwh-64r5
- Warn: Project is vulnerable to: GHSA-257v-vj4p-3w2h
- Warn: Project is vulnerable to: GHSA-3xgq-45jj-v275
- Warn: Project is vulnerable to: GHSA-gxpj-cx7g-858c
- Warn: Project is vulnerable to: GHSA-w573-4hg7-7wgq
- Warn: Project is vulnerable to: GHSA-ff7x-qrg7-qggm
- Warn: Project is vulnerable to: GHSA-ww39-953v-wcq6
- Warn: Project is vulnerable to: GHSA-43f8-2h32-f4cj
- Warn: Project is vulnerable to: GHSA-7r28-3m3f-r2pr
- Warn: Project is vulnerable to: GHSA-r8j5-h5cx-65gg
- Warn: Project is vulnerable to: GHSA-896r-f27r-55mw
- Warn: Project is vulnerable to: GHSA-9c47-m6qq-7p4h
- Warn: Project is vulnerable to: GHSA-76p3-8jx3-jpfq
- Warn: Project is vulnerable to: GHSA-3rfm-jhwj-7488
- Warn: Project is vulnerable to: GHSA-hhq3-ff78-jv3g
- Warn: Project is vulnerable to: GHSA-29mw-wpgm-hmr9
- Warn: Project is vulnerable to: GHSA-35jh-r3h4-6jhm
- Warn: Project is vulnerable to: GHSA-952p-6rrq-rcjv
- Warn: Project is vulnerable to: GHSA-f8q6-p94x-37v3
- Warn: Project is vulnerable to: GHSA-xvch-5gv4-984h
- Warn: Project is vulnerable to: GHSA-5fw9-fq32-wv5p
- Warn: Project is vulnerable to: GHSA-rp65-9cf3-cjxr
- Warn: Project is vulnerable to: GHSA-hj48-42vr-x3v9
- Warn: Project is vulnerable to: GHSA-566m-qj78-rww5
- Warn: Project is vulnerable to: GHSA-7fh5-64p2-3v2j
- Warn: Project is vulnerable to: GHSA-hwj9-h5mp-3pm3
- Warn: Project is vulnerable to: GHSA-hrpp-h998-j3pp
- Warn: Project is vulnerable to: GHSA-p8p7-x288-28g6
- Warn: Project is vulnerable to: GHSA-gcx4-mw62-g8wm
- Warn: Project is vulnerable to: GHSA-c2qf-rxjj-qqgw
- Warn: Project is vulnerable to: GHSA-44c6-4v22-4mhx
- Warn: Project is vulnerable to: GHSA-4x5v-gmq8-25ch
- Warn: Project is vulnerable to: GHSA-hxcc-f52p-wc94
- Warn: Project is vulnerable to: GHSA-4wf5-vphf-c2xc
- Warn: Project is vulnerable to: GHSA-jgrx-mgxx-jf9v
- Warn: Project is vulnerable to: GHSA-72xf-g2v4-qvf3
- Warn: Project is vulnerable to: GHSA-7p7h-4mm5-852v
- Warn: Project is vulnerable to: GHSA-38fc-wpqx-33j7
- Warn: Project is vulnerable to: GHSA-j8xg-fqg3-53r7
- Warn: Project is vulnerable to: GHSA-6fc8-4gx4-v693
- Warn: Project is vulnerable to: GHSA-3h5v-q93c-6h6q
- Warn: Project is vulnerable to: GHSA-p9pc-299p-vxgp
Score
1.7
/10
Last Scanned on 2025-02-10
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More