A lightweight Jest mock for unit testing Express
yarn add --dev @jest-mock/express
npm install --save-dev @jest-mock/express
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.
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
// an example PUT request to update a person
getMockRes will provide a
res object with Jest mock functions. Chaining has been implemented for the applicable functions.
All of the returned mock functions can be cleared with a single call to
mockClear. An alias is also provided called
It will also provide a mock
next function for convenience. That will also be cleared as part of the call to
A full example could be:
test'will respond with the entity from the service',