Comments (11)
I think that having a default statusText that's the same as what the statusText would be for a given status code would be useful. I could always override it if I want to, but having a default to what the browser does would be sensible IMO.
from msw.
Confirmed. This worked. Thanks!
from msw.
@pandrodo, thanks for including that. The issue confirmed as reproducible, the fix pull request is issued. Regression test was added, so we don't miss it occurring again. Will be released in the next minor version of msw
.
from msw.
My guess is that since MSW constructs a new Response
it has statusText: 'OK'
by default. I presume you set the status code alone (i.e. via ctx.status(400)
), so it doesn't map it to any status text associated with that status code.
One way to solve it for now could be to provide an explicit status text alongside the status code:
ctx.status(400, 'Bad request')
I value the validity of requests and responses, but I wonder whether it's the library's responsibility to map status codes to texts implicitly. I'd expect this to be done by Request
constructor. Would have to look into this in more detail.
from msw.
That's fine. Thank you for helping me know what to do there.
from msw.
I was thinking of keeping a status code-status text map internally in MSW and mapping it by default, as you described. I guess there's little harm in that, but a big gain in developer experience.
from msw.
I went with the solution that reuses the statuses
package thats's used in Express to map status codes to status texts.
Starting from 0.9.3
MSW now implicitly maps given status codes to the associated status text, if such is present, and if no custom status text is provided.
@kentcdodds, could you please update to 0.9.3
and let me know it produced correct status texts for you? Thanks.
from msw.
Sorry for bumping that old issue, but when I using setup server with such handler:
rest.post('http://127.0.0.1:3000/getUserPoints', (req, res, ctx) => res(ctx.status(401))),
I receiving response with status text OK
. Its not only for 401, but for other statuses too.
from msw.
Hey, @pandrodo. That's perfectly fine.
Can you clarify a few things:
- Which version of
msw
are you using? - What version of NodeJS are you on?
- How do you make an actual request? (include the code)
from msw.
- Version of
msv
is 0.21.3. - Version of NodeJS is 14.15.0. Running server in my
jest
tests (version 24.9.0 shipped withreact-scripts
3.4.3). - Code is slightly simplified, but response still same with wrong statusText
from msw.
@kettanaito, thanks for so fast fix.
from msw.
Related Issues (20)
- Mocked data getting empty string HOT 8
- "InvalidStateError: The object is in invalid state" when mocking rest api
- Unable to use msw/node for testing solid-js due to `resolve.conditions` set to `browser` HOT 5
- drop CommonJS support HOT 2
- quiet: true should supress RESPONSE LISTENER logs HOT 4
- Narrowing the response body type in `HttpResponse.json` HOT 16
- Error: No known conditions for "./browser" specifier in "msw" package HOT 2
- "Cannot read properties of undefined (reading 'get')" when using generators/async generators as resolvers HOT 2
- `TypeError: crypto.randomUUID is not a function` with msw 2.2.12 HOT 3
- When running Cypress tests in Electron, MSW activation occurs after test completion, causing tests to fail
- Chrome unregisters MSW after 5 mins of inactivity / idle HOT 13
- First-class Server-Sent Events (SSE) API
- The currently registered Service Worker has been generated by a different version of MSW (undefined) HOT 1
- disable prettier as well HOT 1
- Issue with response resolver types after recent upgrade HOT 2
- "TypeError: response.body.getReader is not a function" for HTTPResponse of JSON type HOT 5
- Preserve the original request URL in "onUnhandledRequest" warnings HOT 3
- The cookies object recevied in the handlers varies depending on the placement of the handler within the `setupServer` function. HOT 5
- Calls to worker.use() remove handlers to same endpoint with different method
- Unable to use HttpResponse.error() with TypeScript HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from msw.