Msw: Unmatched requests get logged to the console.

Created on 20 Mar 2020  路  6Comments  路  Source: mswjs/msw

The lib/index.js file is built via webpack and has a console.warn in it in the match function from node-match-path:

/**
 * Matches a given url against a path.
 */
const match = (path, url) => {
    const expression = path instanceof RegExp ? path : pathToRegExp(path);
    const match = expression.exec(url) || false;
    // Matches in strict mode: match string should equal to input (url)
    // Otherwise loose matches will be considered truthy:
    // match('/messages/:id', '/messages/123/users') // true
    const matches = !!match && match[0] === match.input;
    console.warn('nmp', { path, url, match, matches  })
    return {
        matches,
        params: match && matches ? match.groups || null : null,
    };
};

But no version of node-match-path has a console.warn in it. Could we get a rebuild of this and a publish to get rid of that warning? It's kind of distracting 馃槄

Thanks!

Most helpful comment

For anybody arriving here like me _wanting_ to log requests, you can use this technique:

rest.get('*', (req, res, ctx) => {
  console.log(req.url.href)
  // Notice no `return res()` statement
})

The lack of return means that other handlers can still serve the request after it is logged.

All 6 comments

Hello, @kentcdodds. Thanks for reporting this. My bad, I've published the library with some local version of node-match-path. Fixed in 0.9.1. Could you please update and let me know if it's fine? Sorry for any inconvenience this caused.

No worries. I'll take a look tomorrow. Thanks!

This is fixed. Thanks!

For anybody arriving here like me _wanting_ to log requests, you can use this technique:

rest.get('*', (req, res, ctx) => {
  console.log(req.url.href)
  // Notice no `return res()` statement
})

The lack of return means that other handlers can still serve the request after it is logged.

Hey, @bargar! Thanks for posting this for others.

Note that MSW logs out requests that were matched, meaning their responses were mocked. However, you can see those requests _and_ other requests in the "Network" tab of your browser. Sometimes extra logging to the console may not be necessary: just look into the "Network".

Thanks for posting this for others.

My pleasure.

you can see those requests _and_ other requests in the "Network" tab of your browser

An excellent point for msw in the browser!

I probably should have mentioned that I was coming from the context of debugging a react testing library test run at the command line. The request logging alerted me to an unexpected request that I had neglected to mock with msw.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

tomalexhughes picture tomalexhughes  路  3Comments

otaciliolacerda picture otaciliolacerda  路  3Comments

baker-travis picture baker-travis  路  3Comments

Afsoon picture Afsoon  路  3Comments

otaciliolacerda picture otaciliolacerda  路  3Comments