Ambiguous URL not prioritized with Use
See original GitHub issuePrerequisites
- I confirm my issue is not in the opened issues
- I confirm the Frequently Asked Questions didn’t contain the answer to my issue
Environment check
- I’m using the latest
msw
version - I’m using Node.js version 14 or higher
Browsers
Chromium (Chrome, Brave, etc.)
Reproduction repository
https://github.com/bgantzler/examples/tree/handler-order-with-use
Reproduction steps
cd examples/with-jest yarn yarn test
The test that fails is the one that should succeed, the one the succeeds is the one that should fail.
Current behavior
I am running into the same issue as https://github.com/mswjs/msw/issues/742. I do have the routes in the correct order. The difference is I am using use
. Is use
acting differently than when the routes are first defined. This is a simple example to get the point across. For me, no routes are being defined when the worker is created. They are defined ad hoc later, so I am using a use
for each.
let worker = setupWorker();
worker.use(
rest.get('/account/referrer/stats', (req, res, ctx) => res(ctx.text('one')))
);
worker.use(
rest.get('/account/referrer/:code', (req, res, ctx) => res(ctx.text('two')))
);
In this case, if I switch the order, it appears as if the last one defined is the always the one that is used.
Expected behavior
To be able to define routes that overlap, but allow for a prioritization of explicit routes over segmented routes when using use
In the example repo, the test that defines the specific route /login
before the segment /:id
is the one that fails. Since the specific was defined first (see reference issue https://github.com/mswjs/msw/issues/742), I would have expected it to succeed
The next test that defines the specific route second, I would have expected that to fail, but it succeeds. I am assuming that the one that is defined last is the one that is always being used.
Expected behaviour would be that the defining the handlers with use
, the same prioritization would be respected as when defining the handlers with setupWorker
.
Issue Analytics
- State:
- Created a year ago
- Comments:7 (4 by maintainers)
Top GitHub Comments
Will have to land the other stuff first, then see what I can do here.
Ok after reading through this I understand the problem. Missed the pre-append part (it is in your docs) which means I need to do the opposite of what was in the referenced issue.
I have gotten farther. Now on to the next issue.
For your FYI, I am converting an large set of tests from pretender to msw. In pretender, then server is created, then all the handlers are created. The code I have does this in different spots, so use makes similar drop in replacement, rather than re-designing the whole test suite to create the handlers first, then create the worker.