Research
Security News
Malicious npm Package Targets Solana Developers and Hijacks Funds
A malicious npm package targets Solana developers, rerouting funds in 2% of transactions to a hardcoded address.
@jest-mock/express
Advanced tools
A lightweight Jest mock for unit testing Express
Installation:
yarn add --dev @jest-mock/express
npm install --save-dev @jest-mock/express
Importing:
import { getMockReq, getMockRes } from '@jest-mock/express'
getMockReq
getMockReq
is intended to mock the req
object as easily as possible. In its simplest form you can call it with no arguments to return a standard req
with no values.
const req = getMockReq()
To create mock requests with values, you can simply provide them to the function in any order with all being optional. The advantage of this is that it ensures all of the other properties are not undefined.
// an example GET request to retrieve an entity
const req = getMockReq({ params: { id: '123' } })
// an example PUT request to update a person
const req = getMockReq({
params: { id: 564 },
body: { firstname: 'James', lastname: 'Smith', age: 34 },
})
getMockRes
getMockRes
will provide a res
object with Jest mock functions. Chaining has been implemented for the applicable functions.
const { 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
.
const { res, next, clearMockRes } = getMockRes()
beforeEach(() => {
clearMockRes()
})
It will also provide a mock next
function for convenience. That will also be cleared as part of the call to mockClear
/clearMockRes
.
A full example could be:
const { res, next } = getMockRes()
test('will respond with the entity from the service', async () => {
// generate a mock request
const req = getMockReq({ params: { id: 'abc-def' } })
// provide the mock req, res, and next to check against
await myController.getEntity(req, res, next)
expect(res.json).toHaveBeenCalledWith(
expect.objectContaining({
id: 'abc-def',
}),
)
expect(next).toBeCalled()
})
FAQs
A lightweight Jest mock for unit testing Express
The npm package @jest-mock/express receives a total of 148,820 weekly downloads. As such, @jest-mock/express popularity was classified as popular.
We found that @jest-mock/express demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 2 open source maintainers 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.
Research
Security News
A malicious npm package targets Solana developers, rerouting funds in 2% of transactions to a hardcoded address.
Security News
Research
Socket researchers have discovered malicious npm packages targeting crypto developers, stealing credentials and wallet data using spyware delivered through typosquats of popular cryptographic libraries.
Security News
Socket's package search now displays weekly downloads for npm packages, helping developers quickly assess popularity and make more informed decisions.