msw: Error: Error: connect ECONNREFUSED 127.0.0.1:80
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)
About this issue
- Original URL
- State: closed
- Created 4 years ago
- Comments: 24 (10 by maintainers)
Hi @slim-hmidi your configuration seams right 😃 , Could I ask you to replace
with
and check then your console? Thanks, let me know
You’re right @kettanaito !
added
setupFiles: ['dotenv/config'],
in jest.config.ts and I was good to go.Thanks!
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.
Just a question, do you have a single instance of
setupServer
? or you have more test modules with different servers?You need to make it being taken into account manually. The URL you provide to the handler contains variables that resolve to
undefined
when your tests/site runs:Log out those variables and track why they are undefined. Perhaps you are not loading the environmental variables when running tests? Not a library issue.
I asked you that because we have an issue with this approach. https://github.com/mswjs/msw/issues/474
Monkey patched modules are shared between tests, so if one test
close
the server instance it could be possible that another running test will have the problem that you’ve described. Sorry for that 😔@karuppiah7890 it would be great if you can share a reproduction. Because to me, it seems like everything in the shared snippet is setup correctly.
Ah okay. It’s weird yarn upgrade didn’t upgrade it. I thought that would have done it.
And the logs mention about a connection and about node request interceptor. But yeah, I’ll dig in and get back
Makes sense. I’ll try to do that too with a demo setup 👍
Yes we can close this and use the other issue. Thanks and sorry for your trouble 😞
@karuppiah7890 In your situation, I would have updated
MSW
as well asnode
, could you try that 👍?Also, this does not seem to be related to
MSW
, or at least from the screenshots, if I am not mistaken 🤔