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.

TypeError: Network Request Failed

See original GitHub issue

Describe the bug

I received this error whenever there’s code error on the handler code.

Environment

This unit testing running on Node environment

  • msw: 0.35.0
  • nodejs: 16.6.0
  • npm: 7.24.1

To Reproduce

Take a look at the example of the handler code below:

import { rest } from 'msw'

export const handlers = [
  rest.post('/login', (req, res, ctx) => {
    const { username } = req.body
    i = 1 / 0
    return res(
      ctx.json({
        id: 'f79e82e8-c34a-4dc7-a49e-9fadc0979fda',
        username,
        firstName: 'John',
        lastName: 'Maverick',
      })
    )
  }),
]

Expected behavior

The error should appear like this when testing

    ReferenceError: i is not defined
        at resolver ([project path]\src\mocks\handlers.js:6:6)
        at [rest of stack traces]

Current behavior

But instead, TypeError appears

    TypeError: Network request failed

      at node_modules/whatwg-fetch/dist/fetch.umd.js:535:18
      at Timeout.task [as _onTimeout] (node_modules/jsdom/lib/jsdom/browser/Window.js:516:19)

It seems like when the handler code failed, there’s no response being sent back, resulting what it looks like timeout.

More info

I’m able to make the error shows up in the console by editing this line

https://github.com/mswjs/msw/blob/11bb2441e102666184188f9f576ac65e59b1be9d/src/handlers/RequestHandler.ts#L235

into this

      try {
        result = this.resolverGenerator || (await resolver(req, res, ctx))
      } catch (error) {
        console.error(error)
        return null
      }

but it’s kinda ugly? What’s best approach for this?

Issue Analytics

  • State:open
  • Created 2 years ago
  • Reactions:1
  • Comments:10 (4 by maintainers)

github_iconTop GitHub Comments

3reactions
Smankusorscommented, Nov 8, 2021

yeah it should return 500 response, but it doesn’t. The timeout returned instead.

I have tried console.log everywhere in the createRequestListener.ts, but not a single log is even appear on the console

Take a look of my modified createRequestListener

https://github.com/Smankusors/msw/blob/52ffe4117d89820c7c29152e860c4fa8ff15e2ee/src/utils/worker/createRequestListener.ts

2reactions
angeld287commented, Jun 29, 2022

In my case adding the full Url, it didn’t show the error.

const login = rest.post('http://localhost:3001/api/auth/login', (req, res, ctx) => {

Read more comments on GitHub >

github_iconTop Results From Across the Web

React Native fetch() Network Request Failed - Stack Overflow
When I create a brand new project using react-native init (RN version 0.29.1) and put a fetch in the render method to the...
Read more >
HTTP Fetch fails with "TypeError: Network request ... - GitHub
There are 83 issues opened and unanswered about network requests failing with this generic error. The main causes of the pain are: Not...
Read more >
Improve "TypeError: Network request failed" error message
Network request failed, this error occurs usually when api call failed or you have some internet issue. In android emulator, sometimes this error...
Read more >
React Test TypeError: Network request failed - technote.fyi
If you get this error message during a test, it's because jest isn't making the network call. You need to make a mock...
Read more >
Network request failed! : Help & Support
Network request failed! Print · Make sure the resource that you are trying to use is accessible to the outside world. Try checking...
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