Gathering detailed insights and metrics for @jest-mock/express
The total size of the npm registry is estimated to be over 4 terabytes. This includes all the packages, versions, and metadata stored in the registry.
Gathering detailed insights and metrics for @jest-mock/express
The total size of the npm registry is estimated to be over 4 terabytes. This includes all the packages, versions, and metadata stored in the registry.
npm install @jest-mock/express
99.2
Supply Chain
99.6
Quality
79.3
Maintenance
100
Vulnerability
100
License
51 Stars
301 Commits
7 Forks
3 Watching
7 Branches
4 Contributors
Updated on 11 Oct 2024
TypeScript (99.32%)
JavaScript (0.68%)
Cumulative downloads
Total Downloads
Last day
-2.1%
5,167
Compared to previous day
Last week
17.2%
221,164
Compared to previous week
Last month
25.4%
803,044
Compared to previous month
Last year
31.4%
7,717,568
Compared to previous year
A lightweight Jest mock for unit testing Express
Installation:
yarn add --dev @jest-mock/express
npm install --save-dev @jest-mock/express
Importing:
1import { getMockReq, getMockRes } from '@jest-mock/express'
getMockReq
getMockReq
is intended to mock the req
object as quickly as possible. In its simplest form, you can call it with no arguments to return a standard req
object with mocked functions and default values for properties.
1const req = getMockReq()
To create a mock req
with provided values, you can pass them to the function in any order, with all being optional. The advantage of this is that it ensures the other properties are not undefined
. Loose type definitions for standard properties are provided, custom properties ([key: string]: any
) will be passed through to the returned req
object.
1// an example GET request to retrieve an entity 2const req = getMockReq({ params: { id: '123' } })
1// an example PUT request to update a person 2const req = getMockReq({ 3 params: { id: 564 }, 4 body: { firstname: 'James', lastname: 'Smith', age: 34 }, 5})
For use with extended Requests, getMockReq
supports generics.
1interface AuthenticatedRequest extends Request { 2 user: User 3} 4 5const req = getMockReq<AuthenticatedRequest>({ user: mockUser }) 6 7// req.user is typed 8expect(req.user).toBe(mockUser)
getMockRes
getMockRes
will return a mocked res
object with Jest mock functions. Chaining has been implemented for the applicable functions.
1const { res, next, clearMockRes } = getMockRes()
All of the returned mock functions can be cleared with a single call to mockClear
. An alias is also provided called clearMockRes
.
1const { res, next, mockClear } = getMockRes() 2 3beforeEach(() => { 4 mockClear() // can also use clearMockRes() 5})
It will also return a mock next
function for convenience. next
will also be cleared as part of the call to mockClear
/clearMockRes
.
To create mock responses with provided values, you can provide them to the function in any order, with all being optional. Loose type definitions for standard properties are provided, custom properties ([key: string]: any
) will be passed through to the returned res
object.
1const { res, next, clearMockRes } = getMockRes({ 2 locals: { 3 user: getLoggedInUser(), 4 }, 5})
For use with extended Responses, getMockRes
supports generics.
1interface CustomResponse extends Response { 2 locals: { 3 sessionId?: string 4 isPremiumUser?: boolean 5 } 6} 7 8const { res } = getMockRes<CustomResponse>({ 9 locals: { 10 sessionId: 'abcdef', 11 isPremiumUser: false, 12 }, 13}) 14 15// res.locals is typed 16expect(res.locals.sessionId).toBe('abcdef') 17expect(res.locals.isPremiumUser).toBe(false)
A full example to test a controller could be:
1// generate a mocked response and next function, with provided values 2const { res, next } = getMockRes({ 3 locals: { 4 isPremiumUser: true, 5 }, 6}) 7 8test('will respond with the entity from the service', async () => { 9 // generate a mock request with params 10 const req = getMockReq({ params: { id: 'abc-def' } }) 11 12 // provide the mock req, res, and next to assert 13 await myController.getEntity(req, res, next) 14 15 expect(res.json).toHaveBeenCalledWith( 16 expect.objectContaining({ 17 id: 'abc-def', 18 }), 19 ) 20 expect(next).toBeCalled() 21})
No vulnerabilities found.
Reason
no binaries found in the repo
Reason
all changesets reviewed
Reason
no dangerous workflow patterns detected
Reason
license file detected
Details
Reason
packaging workflow detected
Details
Reason
2 existing vulnerabilities detected
Details
Reason
0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
Reason
detected GitHub workflow tokens with excessive permissions
Details
Reason
dependency not pinned by hash detected -- score normalized to 0
Details
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
project is not fuzzed
Details
Reason
security policy file not detected
Details
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
Score
Last Scanned on 2024-11-18
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