Replies: 1 comment 1 reply
-
Hey, @la55u. Thanks for raising this. In terms of path matching, MSW is no different from ExpressJS. When there are ambiguous paths like You can account for this by structuring the handlers so that more specific handlers come first, more general handlers come last:
This is also how any tooling that generates MSW handlers should structure them.
No. Whenever a request happens, MSW goes through each request handler in sequence and uses the first handler that matches the request to produce a mocked response. In this regard, the library is extremely predictable and I wouldn't change that by introducing configurations or implicit behaviors.
I wouldn't recommend doing this. Right now, the only thing that controls path matching is the How would you handle this in Express/other JavaScript backend framework? Perhaps we can get some inspiration from that. |
Beta Was this translation helpful? Give feedback.
-
Scope
Improves an existing behavior
Compatibility
Feature description
I love MSW but recently stumbled upon a behavior that I believed was a bug and spent half a day tracing it down.
As demonstrated here #814 and here #742 the order of the handlers have a significant role since the first one that matches will be used. I believe this is not very intuitive as it may cause a lot of headache if you have to sort hundreds of handlers for a large codebase manually.
For example I have two handlers with these path patterns in the following order.
/institutes/:id
/institutes/changes
When I try to make a request to
/institutes/changes
it will give me the response defined in the handler for/institutes/:id
. Technically it's working because the path matches the pattern but if I want the correct path to match I have to reverse the order which gets very tedious to maintain very quickly especially if the mock handlers are auto generated with another tool. Tools like orval — and people as seen above — assume that MSW will just pick up the correct handler without worrying about their order.So my question is, is it possible currently to force MSW to pick up exact path matches first regardless of the handler's order and then fall back to paths with parameters? If not would it be possible to add a flag to do so?
Thank you
Beta Was this translation helpful? Give feedback.
All reactions