Jest tests pass, then connect ECONNREFUSED 127.0.0.1:80 is printed
Your error
Error: connect ECONNREFUSED 127.0.0.1:80Fix it fast
Most common cause, 1 of 2The 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.
// 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' },
};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.
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.
npx jest src/users.test.js
grep -rnE "jest\.mock\(['\"]axios|setupServer" src jest.setup.* 2>/dev/nullThen find the line you see in this list and open its fix:
- 1The code under test sends a real request to a relative URL
The project has no HTTP mock, the code calls a path such as /users.json without a host, and port 80 appears in the error - 2MSW is running, but no handler matches the URL the request really uses
setupServer 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.
grep -rnE "axios\.(get|post)\(['\"]/" srcFix
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.
// 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.
npx jest src/users.test.js 2>&1 | grep -c ECONNREFUSED # prints 0Confirmed by jestjs.io
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.
grep -rnE "http\.(get|post)\(" src/mocks jest.setup.* 2>/dev/nullFix
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.
// 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.
npx jest src/users.test.jsConfirmed by mswjs.io
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
- Jest: configuration (testEnvironment, testEnvironmentOptions url)
- Jest: mock functions (mocking modules with axios)
- Jest source: jsdom environment (virtual console, default URL)
- jsdom 26.1.0 source: XHR network errors
- MSW: server.listen() and unhandled requests
- MSW: setupServer
- MSW: migrating from 2.x to 3.x
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.