When using package jest-express
to mock Requests in a Typescript project, what is the correct way to ensure that req.body
is an object and not a string?
When a requests's body is parsed to JSON with express.json()
, the value of req.body
is replaced with the parsed Javascript object, but the Request
type from jest-express
has string
as the type of the body property.
Is there some Typescript way of 'changing' the type?
How to get the Request
type from jest-express
to have a body
property of type any
or object
?
Bonus question:
The Request
type from jest-express
does not seem to be a direct replacement for Request
from express
. I have to do something like this:
middlewareFn(<express.Request> <unknown> request, <express.Response> <unknown> response)
to cast from the jest-express
types to the express
types when using the jest-express
mocks as arguments in the tested functions.
How to use jest-express
types as direct replacements for express
types?
Aucun commentaire:
Enregistrer un commentaire