Open silvenon opened 4 months ago
Thanks for raising this, @silvenon.
The issue occurs because of the way MSW coerces the *
into a regular expression. We are using path-to-regexp
for path matching, and the version we depend on doesn't understand wildcards.
Some later versions do but they ship their own breaking changes, some of which are still incompatible with the path matching in MSW.
In attempts to resolve this, I've tried the standard URLPattern
API (#2209). It does resolve your issue! But it has one new of its own (tracked in the PR). I will wait for the folks behind the spec to comment before I continue with this.
Prerequisites
Environment check
msw
versionNode.js version
v20.15.1
Reproduction repository
https://github.com/silvenon/repro/tree/msw-wildcard-port
Reproduction steps
The reproduction branch contains a project with two test, both of them are passing, but the the first one is set to fail using
test.fails
.In a situation where you have to match an unknown port, atttempt to match it in MSW with something like this:
Then attempt to fetch something from
localhost
endpoints with random ports:Current behavior
Currently the library for matching the endpoint,
path-to-regexp
, seems to detect this as the parameter:If I remove the colon it acts as expected.
Expected behavior
Regardless of whether the wildcard is used with or without colon it should achieve a similar result:
localhost:*
localhost*
I.e. for this use case it should work. Except that in the latter pattern I would expect the hostname to not only match
localhost
, but alsolocalhostable
etc., so I consider it to be a workaround rather than a solution.