Msw: Explicitly spot outgoing traffic in tests (unhandled routes)

Created on 31 Jul 2020  路  2Comments  路  Source: mswjs/msw

Is your feature request related to a problem? Please describe.
Greetings @kettanaito ! First of all, the library has been of great use so far, the team got in touch very quickly with the MITM approach proposed here, which is awesome 馃拵

One particular issue we were having is not being able to determine (or spot) actual ongoing traffic / requests that may occur within Jest tests. Most solutions on the internet suggest spying on axios / fetch which is the main thing we're trying to avoid.

Describe the solution you'd like
Not sure if there's a way to do so, but, what are your thoughts on having a fallback / wildcard route "*" (without specifying the verb, which I also don't know how to achieve) that catches any unhandled requests and throws a warning ("This route must be handled!")?
I'd love seeing jest complaining when the test runs and an actual request is fired without a mocked handler.

Thanks in advance!

Describe alternatives you've considered
Couldn't come up with any other alternatives so far

Additional context
Running on Windows 10 Pro
Jest 26.0.0
React 16.13.0(ejected) w/ Typescript 3.9
React testing library

feature

Most helpful comment

Hey, @tncho! Thank you for the kind words. It's the result of a collective work of multiple people that result into, hopefully, awesome experience you and your team are having!

I believe what you've describing has been added as a new feature in the latest 0.20.0 release. Take a look at these docs:

The documentation page above also contains a usage example that would warn you about any unhandled requests that have occurred during your test run. Sounds like what you want!

For posterity, I'll also include the usage example of the onUnhandledRequest option here:

const server = setupServer(
  rest.get('/books', (req, res, ctx) => {
    return res(ctx.json({ title: 'The Lord of the Rings' }))
  }),
)
server.listen({
  onUnhandledRequest: 'warn',
})

Please let me know if this covers your needs.

All 2 comments

Hey, @tncho! Thank you for the kind words. It's the result of a collective work of multiple people that result into, hopefully, awesome experience you and your team are having!

I believe what you've describing has been added as a new feature in the latest 0.20.0 release. Take a look at these docs:

The documentation page above also contains a usage example that would warn you about any unhandled requests that have occurred during your test run. Sounds like what you want!

For posterity, I'll also include the usage example of the onUnhandledRequest option here:

const server = setupServer(
  rest.get('/books', (req, res, ctx) => {
    return res(ctx.json({ title: 'The Lord of the Rings' }))
  }),
)
server.listen({
  onUnhandledRequest: 'warn',
})

Please let me know if this covers your needs.

It does indeed, works like a charm.

Thanks for such a quick and clear reply!

Was this page helpful?
0 / 5 - 0 ratings