mswjs / msw

Industry standard API mocking for JavaScript.
https://mswjs.io
MIT License
15.97k stars 519 forks source link

Document various URL patterns (colon, asterisk etc.) #2203

Closed silvenon closed 4 months ago

silvenon commented 4 months ago

In #2202 it took me a while to figure out how to use MSW with SuperTest. It's making requests to random ports, which MSW is reporting as warnings/errors because they hadn't been intercepted. The goal is to tell MSW to ignore them, because those are requests to our own app.

It's clear that MSW has a parameter component of the URL preceded by the colon, but it seems to also supports an asterisk as a catch-all, which is what I was looking for in this case. However, I seem to have unfortunately combined both of them by preceding the asterisk with the colon and achieved a result that I didn't understand 😬

Luckily, after removing the colon everything worked as expected. I believe URL patterns should be documented in a way that makes it clear to people how to deal with situations like SuperTest.

Btw, did I stumble upon a bug? I guess URL matching can't really recognize ports vs. params when the URL is not finished.

kettanaito commented 4 months ago

Hi, @silvenon. Thanks for raising this!

I believe we do mention the documentation for it. MSW uses a package called path-to-regexp, which has become the standard for path matching in JavaScript (used in Express, lies in the foundation of the upcoming URLPattern standard API).

Screenshot 2024-07-09 at 16 35 33

Please read that package's README to learn about the supported special tokens when defining paths. Thanks!

kettanaito commented 4 months ago

Btw, did I stumble upon a bug? I guess URL matching can't really recognize ports vs. params when the URL is not finished.

This does sound like a bug though. Can you please open a new issue for this?

silvenon commented 4 months ago

Ah, indeed, there it is, I missed it! 🙂

I raised the wildcard bug separately in #2204 if anyone wants to follow.