Comments (9)
from msw.
Hey, @hauptrolle. Could you please open a new issue regarding this server-side behavior in NextJS? I'd kindly ask you to provide a minimal reproduction repository so we can look into it and solve it. Looking forward to that!
from msw.
@kettanaito Thank you for your fast response. I was able to create a reproducible demo and opened an issue :)
#361 Feel free to have a look!
from msw.
Hello, @skipdavis1117. I'm sorry for such experience, there's clearly something off. I'll put down a test for the response patching feature and investigate the possible cause of the issue you're experiencing. Please stay tuned for updates.
from msw.
@skipdavis1117 Hello, can you post the mock code and the startup configuration of the program or keep screenshots of the browser or more detailed information?
from msw.
The main cause for this issue is that the req
argument that response resolver provides is not 1-1 compatible with the RequestInit
type that window.fetch()
expects. This may lead to all sort of quirks, potentially including this issue.
There is also one detail to keep in mind: any requests performed from the client-side are processed via MSW. That includes the window.fetch(req)
you may want to perform as a part of your response resolver to get an original response. In case you are mocking the same URL you would like to hit as a part of response resolver, it would throw MSW into an infinite loop. See:
composeMocks(
rest.get('https://api.github.com/users/:username', async (req, res) => {
const original = await fetch(req.url, req)
})
)
Requesting
https://api.github.com/users/octocat
matches the request handler in the schema, so MSW attempts to mock the response. As a part of mocking you perform an actualfetch()
to the same URI. That request is again matched against the mocked schema, and MSW attempts to mock it.
In order to prevent this loop I suggest two things:
- In order to perform a request bypassed by MSW you should use a newly introduced
ctx.fetch()
utility. This way such requests are evident and allow MSW to distinguish between mock/non-mock scenario. - Properly set a custom flag on the dispatched request using
ctx.fetch()
under the hood, so thatmockServiceWorker.js
would check for the flag and bypass such requests.
At the moment I'm thinking of what such flag can be and how to set it on the request object to preserve request semantics. I've tried using a custom header, but then it may result into preflight request violations from CORS policy. I'd appreciate any feedback on this.
from msw.
The implementation is released in 0.9.0
.
Resolution
Please see the updated Response patching recipe in the documentation.
In order to use response patching please perform a request to the actual resource via ctx.fetch()
utility function. Here's a tested example of response patching in action:
Why ctx.fetch()
over window.fetch()
?
Using ctx.fetch()
implicitly sets a special header on the request that, whenever present, tells MSW to bypass this request and actually perform it. You can still use regular fetch()
, but requests performed with it will be subjected to mocking.
@skipdavis1117, could you please update to 0.9.0
and let me know if following the example in the docs works for you? Thanks.
from msw.
@skipdavis1117, warms my heart to hear that! Thank you and hope that MSW keeps up with your team's expectations. Looking forward to hear some feedback post usage.
from msw.
Thank you so much for that example! I already implemented MSW in our Nextjs project.
But I have one question: When adding response patching, I always get a Request with GET/HEAD method cannot have body
error. But only on the server-side of Nextjs. Any ideas why this is happening? For client-side requests, everything works fine...
from msw.
Related Issues (20)
- 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 5
- "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 HOT 1
- Unable to use HttpResponse.error() with TypeScript HOT 3
- How do I implement MSW in a NextJS backend?
- Missing `await` in the getting started example section for fetch call HOT 4
- 'Cannot read properties of undefined (reading 'url')' when multiple browser tabs are open
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.