question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Path matching not working as expected in browser environment

See original GitHub issue

Environment

Name Version
msw 0.21.2
browser Firefox 82.0b7
OS MacOS 10.15.6

Issue

Hello! This might be more of a documentation issue (but would love to see it becoming a feature request as well). I recently started using MSW in a CRA application to mock Axios calls.

Specifying only the paths (not the complete URL) to MSW handlers doesn’t work. Example:

 rest.get("/people/3/", (_req, res, ctx) => {
  ...
}

This could be a trip up on my behalf since I found the Path with parameters section example under the Path section (and coming from using axios-mock-adapter, which lets me specify paths to intercept requests). Writing up this repro + issue report has clarified the behaviour a bit, but took me a couple of hours of scratching my head to get here.

If you can confirm that the behavior below is as expected, would love to see the documentation updated to use consistent examples in the Path section (i.e. just API paths or complete URLs with params, rather than both, since the notation is new for people like me coming from a non-Node/Express background). Or to have the feature supported as well (#406 might solve for this, I guess). I can raise a PR for the former if needed, based on your confirmation.

For now, I work around this issue in my project by specifying the path in my matcher as (I maintain all endpoints in one file, hence the variable):

rest.get(`*${endpoints.FETCH_C3PO}`, (_req, res, ctx) => {
  ...
}

I created a quick, dummy example with the issue: ghostwriternr/msw-pathmatch-example

Request handlers

Endpoints:

const FETCH_C3PO = `/people/2/`;
const FETCH_R2D2 = `/people/3/`;

export const endpoints = {
  FETCH_C3PO,
  FETCH_R2D2,
};

Handlers:

export const handlers = [
  rest.get(endpoints.FETCH_C3PO, (_req, res, ctx) => {
    return res(ctx.status(200), ctx.json(mockedResponses.c3poData));
  }),
  rest.get("https://swapi.dev/api/people/3/", (_req, res, ctx) => {
    return res(ctx.status(200), ctx.json(mockedResponses.r2d2Data));
  }),
];

Actual request

const starWarsAxiosInstance = Axios.create({
  baseURL: "https://swapi.dev/api",
});

const fetchBot = (bot: "c3po" | "r2d2") => {
  return starWarsAxiosInstance.get<BotData>(
    bot === "c3po" ? endpoints.FETCH_C3PO : endpoints.FETCH_R2D2
  );
};

Current behavior

Only /people/3/ endpoint, whose handler specifies the whole URL instead of just the path, is mocked by MSW.

Expected behavior

Both endpoints to be mocked by MSW.

Screenshots

Screenshot 2020-10-13 at 5 01 51 AM

Credits

I have recently started using MSW and can’t thank the maintainers enough for how brilliant and simultaneously easy this is to use. Definitely loved the step-by-step getting started documentation as well. Thanks a ton!

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:5 (4 by maintainers)

github_iconTop GitHub Comments

2reactions
kettanaitocommented, Oct 14, 2020

My pleasure, @ghostwriternr! Documentation is something we constantly iterate on as people share their struggle and ask questions. I’m super happy to get a chance to improve that page. Don’t hesitate to reach out with questions. Thanks!

1reaction
ghostwriternrcommented, Oct 13, 2020

@kettanaito that was super fast! Thank you so much for your patience and detailed explanations. The documentation update is super helpful, along with the REPL, and I’m able to understand & write the MSW handlers in our project properly. Your help is much much appreciated 😁 🎉

Read more comments on GitHub >

github_iconTop Results From Across the Web

"'chromedriver' executable needs to be available in the path ...
Error message: "'chromedriver' executable needs to be available in the path" · 52. Try copying the chromedriver.exe in the same directory as your...
Read more >
Fix site display issues with Compatibility View in Internet ...
Learn how to fix website display problems using Compatibility View in Internet Explorer.
Read more >
Handling common HTML and CSS problems - MDN Web Docs
In the Search packages text field, type "lint" and press Enter/Return to search for linting-related packages. You should see a package called ...
Read more >
URLPattern brings routing to the web platform
At its heart, routing involves taking a URL, applying some pattern matching or other app-specific logic to it, and then, usually, displaying web ......
Read more >
4 Reasons Why Your Source Maps are Broken - Sentry Blog
But generating a source map isn't worth diddly if the browser can't find it. To do that, browser agents expect your bundled JavaScript...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found