-
Notifications
You must be signed in to change notification settings - Fork 26
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Support for onUnhandledRequest #52
Comments
I'd also love to see this feature! |
Hey @jmbeach and @brandon-pereira, sorry for the delay. At this stage this configuration is unsupported by The reason for this is that Another way of putting this is that unlike the official
Interestingly, prior to version 2.0, the library did use I have two questions:
|
It sounds like this would be pretty involved and I only passively wanted it. I didn't get how playwright-msw worked, but I figured it just made calls to the official msw, in which case, you'd just expose the configuration property that allows users to do this. I wouldn't want to add it if it could potentially give other users of the library a worse experience (with more noise). To answer your question though, I turn the option on so that I don't forget to mock any requests. When I use playwright, it's typically to do more of an integration test instead of an end-to-end test so I'm not wanting to use real api calls. |
For me it's very similar to @jmbeach, we are running these as integration tests in our CI server. Sometimes, some of the requests appear to not be mocked (haven't debugged why, probably query params or headers mismatch). Currently, these misses result in going out to our real server, and the response of that server will have different data than the mocked response. This then causes the integration test to fail because it's different result than we're expecting. The ideal scenario for me would be that these tests fail, so that we can fix the mock to correctly resolve (or flag that a new api is not mocked, as an example) rather than failing due to the DOM output not being as expected. I don't think this is absolutely necessary either, but some ability to potentially have a "debug" mode where its very verbose and allows us to debug this would be easier, currently, there is no way (that I know of) to properly show which responses were cache hit vs cache miss other than looking at the network planel in the playwright windows dev tools. Please correct me if i'm wrong! By the way, I really appreciate the thorough answer and explanation, thank you! 🙏 |
Thanks for taking the time to explain the importance of this feature @jmbeach and @brandon-pereira. Having requests hit a real server and causing tests to be flaky is definitely undesirable. In my opinion, while there is some effort required to implement this feature, it is worthwhile to mitigate this issue. As such, I've started working on this feature already. I'll have more to share on this front in the near future :)
That's correct. There's no way to do this right now. I'm keen to get some input though, if one were to pass in
I'm currently leaning towards #2 as this is how I believe the official MSW behaves. If we want the test to fail, we can pass a callback to onUnhandledRequest and throw an error. Thoughts? |
I would agree with this! Please let me know if I can help out with implementing this feature :) |
I second @jmbeach and @brandon-pereira's case for mocking all XHR requests for functional (not e2e) tests — the main feature request here being able to tell which requests we forgot to mock. |
I'd also love to see this implemented to aid debugging, and would be happy to help out. |
I would like to see this as well, to avoid external URL requests that were forgotten to be mocked, I would like to mock all requests so that we are not including network requests in our tests to make them quicker |
For anyone stumbling across this, I solved this by adding a catch all handler like mentioned earlier in this thread, but instead of catching all requests it only catches request that are sent to the api: const catchAllHandler = http.all("yourapidomain.com/api/*", ({ request }) => {
console.warn(`Warning: ${request.url} endpoint using ${request.method} method is not mocked.`);
return Response.error(); // This will return a network error
}); adjust the api base url to fit your sites structure This should show a warning in your playwright console for any unhandled request to your api. This way you can catch unhandled requests when running the tests manually and paying attention to the console, but to catch them all automatically it would be nice to see this package updated. |
I'd like to use the msw configuration option
onUnhandledRequest: 'error'
. However, this is an option that gets passed to thelisten()
method and I don't know if this is supported by playwright-msw. Any advice?The text was updated successfully, but these errors were encountered: