Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4791 pages
Node.js, PHP and APIs

Jest tests pass, then connect ECONNREFUSED 127.0.0.1:80 is printed

Your error

Error: connect ECONNREFUSED 127.0.0.1:80

Fix it fast

Most common cause, 1 of 2

The code under test sends a real request to a relative URL

Mock the HTTP module in the test so no request leaves the process, as Jest's own guide does with axios.

Fix
// users.test.js
import axios from 'axios';
import Users from './users';

jest.mock('axios');

test('loads users without a network call', () => {
  const users = [{ name: 'Bob' }];
  axios.get.mockResolvedValue({ data: users });
  return Users.all().then(data => expect(data).toEqual(users));
});

// jest.config.js, only for tests that need a live server
module.exports = {
  testEnvironment: 'jsdom',
  testEnvironmentOptions: { url: 'http://localhost:3000' },
};
Did this fix it?
Full steps for this cause: check, fix, verify

Short answer

Error: connect ECONNREFUSED 127.0.0.1:80 after passing Jest tests means code under test made a real HTTP request. Jest's jsdom environment uses http://localhost as the page URL, so a relative path like /api/users goes to port 80, where nothing listens. Mock the HTTP client with jest.mock or fix the MSW handler that should catch it. Applies to Jest 30 with jsdom.

  • Applies to Jest 30.5, jest-environment-jsdom 30.5 (jsdom 26.1), MSW 3.0, Node.js 22, 24 and 26
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Which cause do you have?

Run only the test file that prints the error, then check whether the project mocks HTTP at all. No jest.mock of the HTTP client and no MSW server is cause 1. An MSW server that is set up, with an MSW warning about an unhandled request in the output, is cause 2.

Run this first
npx jest src/users.test.js
grep -rnE "jest\.mock\(['\"]axios|setupServer" src jest.setup.* 2>/dev/null

Then find the line you see in this list and open its fix:

  1. 1The code under test sends a real request to a relative URLThe project has no HTTP mock, the code calls a path such as /users.json without a host, and port 80 appears in the error
  2. 2MSW is running, but no handler matches the URL the request really usessetupServer is in the setup file, MSW prints a warning about an unhandled request, and the ECONNREFUSED line follows it

What does the error look like?

  • Error: connect ECONNREFUSED 127.0.0.1:80

Cause 1 of 2

The code under test sends a real request to a relative URL

You seeThe project has no HTTP mock, the code calls a path such as /users.json without a host, and port 80 appears in the error

Check

Jest's jsdom environment sets the page URL to http://localhost unless testEnvironmentOptions.url says otherwise, and that URL is the base for relative requests. An XMLHttpRequest to /users.json therefore goes to http://localhost:80, and with nothing listening the connection is refused. jsdom reports the failed request on its virtual console, and Jest forwards that to console.error instead of throwing it inside the test, so assertions that already ran still count as a pass and the error shows up near the end.

Check
grep -rnE "axios\.(get|post)\(['\"]/" src

Fix

Mock the HTTP module in the test so no request leaves the process, as Jest's own guide does with axios. For a deliberate integration test that needs a running server, point jsdom's URL at that server instead, so relative paths resolve to it.

Fix
// users.test.js
import axios from 'axios';
import Users from './users';

jest.mock('axios');

test('loads users without a network call', () => {
  const users = [{ name: 'Bob' }];
  axios.get.mockResolvedValue({ data: users });
  return Users.all().then(data => expect(data).toEqual(users));
});

// jest.config.js, only for tests that need a live server
module.exports = {
  testEnvironment: 'jsdom',
  testEnvironmentOptions: { url: 'http://localhost:3000' },
};

Verify

The same test file runs green with no ECONNREFUSED line in the output.

Verify
npx jest src/users.test.js 2>&1 | grep -c ECONNREFUSED   # prints 0

Confirmed by jestjs.io

Did this fix it?

Cause 2 of 2

MSW is running, but no handler matches the URL the request really uses

You seesetupServer is in the setup file, MSW prints a warning about an unhandled request, and the ECONNREFUSED line follows it

Check

MSW only answers requests that a handler matches. Its default strategy for anything else is warn, which prints a warning and then performs the request as is, so it reaches the real network and fails. A handler written for https://api.example.com/users.json never matches code that calls the relative /users.json, because under jsdom that path becomes http://localhost/users.json. MSW 3 calls the strategy option onUnhandledFrame; MSW 2 called it onUnhandledRequest.

Check
grep -rnE "http\.(get|post)\(" src/mocks jest.setup.* 2>/dev/null

Fix

Write the handler for the URL as jsdom resolves it, and make unmatched requests fail loudly so a missing handler shows up as a test failure instead of a stray network error.

Fix
// jest.setup.js (MSW 3)
import { setupServer } from 'msw/node';
import { http, HttpResponse } from 'msw/http';

const server = setupServer(
  http.get('http://localhost/users.json', () => HttpResponse.json([{ name: 'Bob' }])),
);

beforeAll(() => server.listen({ onUnhandledFrame: 'error' }));
afterEach(() => server.resetHandlers());
afterAll(() => server.close());

Verify

The test passes with no MSW warning and no ECONNREFUSED. Removing the handler on purpose now makes MSW print an error for that request.

Verify
npx jest src/users.test.js

Confirmed by mswjs.io

Did this fix it?

How do I stop it coming back?

  • Start every MSW server with the error strategy (onUnhandledFrame in MSW 3), so a request without a handler is reported as an error, not quietly sent to the network.
  • Keep one API base path in a config module that tests import, so handlers and code build URLs the same way.
  • Treat console.error output in CI as a failure for test suites that mock HTTP, so a leaked request cannot hide behind a green run.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine on the same stack: TaxiDex, Workforce.

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.