Gathering detailed insights and metrics for jest-mock-req-res
Gathering detailed insights and metrics for jest-mock-req-res
Gathering detailed insights and metrics for jest-mock-req-res
Gathering detailed insights and metrics for jest-mock-req-res
npm install jest-mock-req-res
Typescript
Module System
Node Version
NPM Version
55.6
Supply Chain
47.5
Quality
70.5
Maintenance
100
Vulnerability
97.3
License
TypeScript (100%)
Love this project? Help keep it running — sponsor us today! 🚀
Total Downloads
1,230,131
Last Day
2,403
Last Week
11,505
Last Month
47,284
Last Year
587,278
MIT License
3 Stars
20 Commits
2 Watchers
15 Branches
1 Contributors
Updated on Dec 02, 2024
Minified
Minified + Gzipped
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
Cumulative downloads
Total Downloads
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
Jest mocks for Express Request
and Response
objects. Inspired by mock-req-res.
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).
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.
Simply add this package as a development dependency to your project:
1npm i -D jest-mock-req-res
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
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
Reason
project is not fuzzed
Details
Reason
branch protection not enabled on development/release branches
Details
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
Reason
45 existing vulnerabilities detected
Details
Score
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