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.

Error: Error: connect ECONNREFUSED 127.0.0.1:80

See original GitHub issue

Environment

Name Version
msw 0.21.3
node 12.19.0
OS MacOS

Request handlers

import { rest } from "msw";
import check1result from "./check1result";
import check2result from "./check2result";

function check1(queryParams: URLSearchParams): boolean {
  return true;
}

function check2(queryParams: URLSearchParams): boolean {
  return true;
}

const requestHandler = [
  rest.get("/api/resources", (req, res, ctx) => {
    let json;
    const queryParams = req.url.searchParams;

    if (check1(queryParams)) {
      json = ctx.json(check1result);
    } else if (check2(queryParams)) {
      json = ctx.json(check2result);
    }
    {
      json = ctx.json([]);
    }

    return res(ctx.delay(0), ctx.status(200), json);
  }),
];

const orderReturnMockServer = setupServer(...requestHandler);

// mws api server mock initialization
beforeAll(() =>
  orderReturnMockServer.listen({
    onUnhandledRequest: "error",
  })
);
afterAll(() => orderReturnMockServer.close());

describe('describe', () => {
    test('test1', () => {
        networkCall()
    })
    test('test2', () => {
        networkCall()
    })
})

Actual request

// Example of making a request. Provide your code here.
fetch('???').then((res) => res.json())

Current behavior

A lot of Error: Error: connect ECONNREFUSED 127.0.0.1:80 in the console logs when running my tests. But the tests pass most of the time. Sometimes there’s failures - for example timeouts. Not sure if it’s related to this though, just saying.

Expected behavior

No errors about connection refused in the console logs - as it looks like something’s wrong, but I don’t know what’s wrong and the tests pass too. 😕

Screenshots

I’ll paste some logs instead

console.error node_modules/@testing-library/react/dist/act-compat.js:52
      Error: Error: connect ECONNREFUSED 127.0.0.1:80
          at Object.dispatchError (/Users/karuppiahn/projects/deda-group/S-OMS/s-oms-ui/node_modules/jsdom/lib/jsdom/living/xhr/xhr-utils.js:62:19)
          at Request.<anonymous> (/Users/karuppiahn/projects/deda-group/S-OMS/s-oms-ui/node_modules/jsdom/lib/jsdom/living/xhr/XMLHttpRequest-impl.js:654:18)
          at Request.emit (events.js:326:22)
          at Request.onRequestError (/Users/karuppiahn/projects/deda-group/S-OMS/s-oms-ui/node_modules/request/request.js:877:8)
          at ClientRequestOverride.emit (events.js:314:20)
          at ClientRequest.<anonymous> (/Users/karuppiahn/projects/deda-group/S-OMS/s-oms-ui/node_modules/node-request-interceptor/src/interceptors/ClientRequest/ClientRequestOverride.ts:292:14)
          at ClientRequest.emit (events.js:314:20)
          at Socket.socketErrorListener (_http_client.js:428:9)
          at Socket.emit (events.js:314:20)
          at emitErrorNT (internal/streams/destroy.js:92:8) undefined


console.error node_modules/jsdom/lib/jsdom/virtual-console.js:29
      Error: Error: connect ECONNREFUSED 127.0.0.1:80
          at Object.dispatchError (/Users/karuppiahn/projects/deda-group/S-OMS/s-oms-ui/node_modules/jsdom/lib/jsdom/living/xhr/xhr-utils.js:62:19)
          at Request.<anonymous> (/Users/karuppiahn/projects/deda-group/S-OMS/s-oms-ui/node_modules/jsdom/lib/jsdom/living/xhr/XMLHttpRequest-impl.js:654:18)
          at Request.emit (events.js:326:22)
          at Request.onRequestError (/Users/karuppiahn/projects/deda-group/S-OMS/s-oms-ui/node_modules/request/request.js:877:8)
          at ClientRequestOverride.emit (events.js:314:20)
          at ClientRequest.<anonymous> (/Users/karuppiahn/projects/deda-group/S-OMS/s-oms-ui/node_modules/node-request-interceptor/src/interceptors/ClientRequest/ClientRequestOverride.ts:292:14)
          at ClientRequest.emit (events.js:314:20)
          at Socket.socketErrorListener (_http_client.js:428:9)
          at Socket.emit (events.js:314:20)
          at emitErrorNT (internal/streams/destroy.js:92:8) undefined


console.error node_modules/jsdom/lib/jsdom/virtual-console.js:29
      Error: Error: connect ECONNREFUSED 127.0.0.1:80
          at Object.dispatchError (/Users/karuppiahn/projects/deda-group/S-OMS/s-oms-ui/node_modules/jsdom/lib/jsdom/living/xhr/xhr-utils.js:62:19)
          at Request.<anonymous> (/Users/karuppiahn/projects/deda-group/S-OMS/s-oms-ui/node_modules/jsdom/lib/jsdom/living/xhr/XMLHttpRequest-impl.js:654:18)
          at Request.emit (events.js:326:22)
          at Request.onRequestError (/Users/karuppiahn/projects/deda-group/S-OMS/s-oms-ui/node_modules/request/request.js:877:8)
          at ClientRequestOverride.emit (events.js:314:20)
          at ClientRequest.<anonymous> (/Users/karuppiahn/projects/deda-group/S-OMS/s-oms-ui/node_modules/node-request-interceptor/src/interceptors/ClientRequest/ClientRequestOverride.ts:292:14)
          at ClientRequest.emit (events.js:314:20)
          at Socket.socketErrorListener (_http_client.js:428:9)
          at Socket.emit (events.js:314:20)
          at emitErrorNT (internal/streams/destroy.js:92:8) undefined

Something to add is, we do have some tests that get rid of components abruptly when they are in the middle of a fetch call (my assumption), which leads to a lot of the below logs

console.error node_modules/@testing-library/react/dist/act-compat.js:52
      Warning: Can't perform a React state update on an unmounted component. This is a no-op, but it indicates a memory leak in your application. To fix, cancel all subscriptions and asynchronous tasks in a useEffect cleanup function.
          in Orders (at Orders.test.tsx:121)
          in ThemeProvider (at Orders.test.tsx:120)
          in IntlProvider (at Orders.test.tsx:119)
          in Router (at Orders.test.tsx:118)
          in KeycloakProvider (at keycloak_mock_provider.tsx:46)
          in MockedKeycloakProvider (at Orders.test.tsx:117)

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:24 (10 by maintainers)

github_iconTop GitHub Comments

7reactions
slim-hmidicommented, Jan 19, 2021

you are right. I added the different handlers mentioned by the warn and it works well now. thanks a lot for your time and your help.

7reactions
marcosvega91commented, Jan 19, 2021

Hi @slim-hmidi your configuration seams right 😃 , Could I ask you to replace

beforeAll(() => {
  server.listen();
});

with

beforeAll(() => {
  server.listen({
    onUnhandledRequest: 'warn',
  })
});

and check then your console? Thanks, let me know

Read more comments on GitHub >

github_iconTop Results From Across the Web

Error: connect ECONNREFUSED 127.0.0.1:80 - Postman
It looks like you're sending a request to localhost:3000 , but your Proxy settings seem to be set to 127.0.0.1: 8080 Is your...
Read more >
api - Postman connect ECONNREFUSED 127.0.0.1:8080
ECONNREFUSED (Connection refused): No connection could be made because the target machine actively refused it. This usually results from trying ...
Read more >
Error: connect ECONNREFUSED 127.0.0.1:8080 | POSTMAN
Error : connect ECONNREFUSED 127.0.0.1:8080 | POSTMAN. 6.8K views 5 months ago. Knowledge Flavours Technical. Knowledge Flavours Technical.
Read more >
Axios returns ECONNREFUSED 127.0.0.1:80 but port is set to ...
This error happened while generating the page. Any console logs will be displayed in the terminal window. Call Stack error: connect ECONNREFUSED ......
Read more >
Follow up of Econnrefused ::1:27017 - MongoDB
When connecting to mongodb with MongoClient i get MongoServerSelectionError: connect ECONNREFUSED ::1:27017 when connecting to ...
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