Closed michielbdejong closed 4 years ago
Hi @michielbdejong, could you please give an example how the method
of the IncomingMessage
might be undefined? How are you even able to send such a request?
Ha, you're right, https://nodejs.org/api/http.html#http_class_http_incomingmessage says it's always a string
, so it should not be necessary, but the following happens:
const method: string = request.method;
then TypeScript will complain something to the effect of 'cannot assign <string | undefined>
to <string>
.const method: string = request.method || 'GET';
So sounds to me like the ?
in https://github.com/DefinitelyTyped/DefinitelyTyped/blob/master/types/node/http.d.ts#L284 is essentially incorrect here? Maybe I misunderstood, I'm pretty new to branch coverage maximisation for TypeScript code.
Ah I guess the comment above that line explains why the ?
is there.
Indeed, that comment explains.
But this module is meant to be used only for testing http.Server
requests.
In this context - method
cannot be undefined
.
I suggest a workaround for your case:
const req = httpMocks.createRequest();
req.method = undefined;
We want to test how our code responds to a http IncomingMessage where the method is undefined.