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.

When specifying the endpoint, not matching any mocked endpoint

See original GitHub issue

Environment

Name Version
msw 0.19.5
node 12.18.1
OS Windows 10 Pro

Request handlers

import { rest } from 'msw';
import { setupServer } from 'msw/node';

//Also tried http://localhost:*, http://localhost:5000/todo*, http://localhost:*/todo* 
const handlers = [
  rest.get('/todo', (req, res, ctx) => {
    return Promise.resolve(res(ctx.json({
      currentPage: 1,
      pageSize: 1,
      pageCount: 1,
      totalRowCount: 0,
      result: [],
    })));
  })]

const server = setupServer(...handlers);

beforeAll(() => server.listen());
afterEach(() => server.resetHandlers());
afterAll(() => server.close());

Actual request

// Example of making a request. Provide your code here.
fetch("http://localhost:5000/api/todo").then(result => {
      console.log('LOOK AT ME', result);
      return result;
    });

Current behavior

The request URL is coming in empty. Also, the request not mapping to the correct path, only works when I use wildcard “*”.

Expected behavior

Intercepting the call and routing to the correct mocked endpoint.

Issue Analytics

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

github_iconTop GitHub Comments

1reaction
steve-taylorcommented, Jun 1, 2021

Also, please, take a moment to read about absolute request URLs in NodeJS, it’s useful if you are running your test outside of a DOM-like environment (i.e. outside of jsdom).

Even with jsdom, msw doesn’t match requests unless the absolute URL is specified. Seems like a bug, either in the implementation or the docs.

1reaction
risaaccommented, Jul 27, 2020

I see, thank you!

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to allow non mocked endpoints when using responses ...
E.g. Actually access the 'http://google.com' endpoint to run the function, then mock other data from another endpoint and test my function.
Read more >
Request matching - Basics - Mock Service Worker Docs
Requests with no corresponding request handlers are performed as-is (bypassed ). Request matching substantially differs based on the request ...
Read more >
Understanding example matching | Postman Learning Center
Using Postman's mock servers requires a collection with requests and saved request examples. You can save as many examples to a collection ...
Read more >
AdviceWith - Apache Camel
Mocks all endpoints in the route that matches the patterns. You can use wildcards and regular expressions in the given pattern to match...
Read more >
Returning stubbed HTTP responses to specific requests
A core feature of WireMock API mocking is the ability to return canned ... You can specify ANY if you want the stub...
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