Gathering detailed insights and metrics for @jest-mock/express
Gathering detailed insights and metrics for @jest-mock/express
Gathering detailed insights and metrics for @jest-mock/express
Gathering detailed insights and metrics for @jest-mock/express
npm install @jest-mock/express
Typescript
Module System
Node Version
NPM Version
TypeScript (99.31%)
JavaScript (0.69%)
Total Downloads
0
Last Day
0
Last Week
0
Last Month
0
Last Year
0
MIT License
52 Stars
302 Commits
11 Forks
3 Watchers
8 Branches
5 Contributors
Updated on Feb 03, 2025
Latest Version
3.0.0
Package Id
@jest-mock/express@3.0.0
Unpacked Size
684.03 kB
Size
52.23 kB
File Count
139
NPM Version
10.9.2
Node Version
22.13.1
Published on
Feb 03, 2025
Cumulative downloads
Total Downloads
Last Day
0%
NaN
Compared to previous day
Last Week
0%
NaN
Compared to previous week
Last Month
0%
NaN
Compared to previous month
Last Year
0%
NaN
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
all changesets reviewed
Reason
no binaries found in the repo
Reason
no dangerous workflow patterns detected
Reason
license file detected
Details
Reason
packaging workflow detected
Details
Reason
4 existing vulnerabilities detected
Details
Reason
dependency not pinned by hash detected -- score normalized to 0
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
no effort to earn an OpenSSF best practices badge detected
Reason
security policy file not detected
Details
Reason
project is not fuzzed
Details
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
Score
Last Scanned on 2025-07-07
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