First of all, thanks a lot for MSW, it's really cool 馃憤
When using HTTP error code in a response, statusText is set to null. It's in node environment.
msw: 0.19.5nodejs: 12.18.0npm: 6.14.5Please also provide your browser version.
Steps to reproduce the behavior:
return res(
ctx.status(401),
ctx.json({
message: 'Mocked response JSON body',
}),
);
statusText to format an error message, you can see null value:{
"statusCode": 401,
"message": "null: Mocked response JSON body"
}
Note, same happens if you provide custom statusText like so ctx.status(401, 'Test')
Response should contain correct default statusText. Also it should be replaced by a custom one, if provided:
{
"statusCode": 401,
"message": "Unauthorized: Mocked response JSON body"
}
None
Hi @m0t0r thanks for opening the issue :),
it should work as you expected. Can you create a repo with a reproduction case?, in this way I can take a look at the code to understand why it's not working.
Thanks, @m0t0r for reporting this, and @marcosvega91 for taking over!
I'll share all the technical details I know about this, maybe this helps.
ctx.status utility inherits a status message respective to the given status code:
https://github.com/mswjs/msw/blob/be0173d6cd4efc9e06c9e4e6f3ff771e77c56708/src/context/status.ts#L10-L11
That statusText is set on the mocked response object, but that doesn't necessarily means it propagates to the client. Mocked response object is mapped to the response instance manually. For example, this is how it's mapped to a Response on the Service Worker's side:
clientMessage.payloadcontains the mocked response object with thestatuText: stringproperty set.
In NodeJS request interception here's how the mocked response object is mapped to the instance that node-request-interceptor (NRI) library would understand:
response.statusTextis present here, so we should be fine.
Once NRI received a mocked response object, it converts it to the proper response based on how the request has been issued (via http, or XMLHttpRequest). Here's the relevant parts:
ClientRequest mapping of response.status:Notice how there is no
response.statusTextmapping at all. I believe that is the issue.
XMLHttpRequest mapping of this.status and this.statusText:Starting with a test is always a way to go. This doesn't look like an issue on the MSW side, rather on the NRI side. Let me know if you have any questions. Thanks.
@marcosvega91 @kettanaito please check this repo with reproduction of the issue: https://github.com/m0t0r/msw-statusText-issue-repro
ok I have found the issue, I'll make a PR on node-request-interceptor.
The problem is that statusText from mockedResponse is not assigned to statusMessage of response object
Awesome, @marcosvega91! Looking forward to the pull request. Will help with the code review.
done, let me know :)
@marcosvega91 thanks a lot for the fix!
The scenario has been tested and the fix has been released in [email protected] (see Release notes).
@marcosvega91, please, do you mind carrying over the update of the dependency in MSW? Feel free to use pull requests like this as an inspiration.
Most helpful comment
The scenario has been tested and the fix has been released in
[email protected](see Release notes).@marcosvega91, please, do you mind carrying over the update of the dependency in MSW? Feel free to use pull requests like this as an inspiration.