msw

Stop msw fron showing warnings about unhandled request?


Is there any way to configure msw to only intercept requests to paths with certain pattern?

For example only paths start with "/api".

Currently it shows warning every time I navigate to some page of my React app.


Solution

  • This should do it:

    worker.start({
      onUnhandledRequest(req, print) {
        if (req.url.pathname.startsWith('/api/') {
          print.warning()
        }
    
        return;
      }
    })
    

    If you want to turn it off completely use:

    worker.start({
      onUnhandledRequest: 'bypass',
    })