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.

connect ECONNREFUSED when i use msw with react-testing-library

See original GitHub issue

Environment

Name Version
msw ^0.21.2
node v12.18.3
OS W10

Request handlers

const server = setupServer(
  rest.post("http://localhost:3100/users", (req, res, ctx) => {
    return res(
      ctx.json({ message: 'Usuário registrado com sucesso!' }),
      ctx.status(200)
    );
  })
);
beforeAll(() => server.listen())
afterEach(() => server.resetHandlers())
afterAll(() => server.close())

My test call one function that make a http request:

test("should register user successfully when all fields is correctly filled", async () => {
   `fireEvent.click(registerButton);`
})

That calls:

const registerUser = async (
  user: IRegisterUser
): Promise<AxiosResponse<IRequestResponse>> => {
const api = axios.create({
  baseURL: "http://localhost:3100",
});
  return await api.post("/users", user);
};

I’m receiving this error:

Error: Error: connect ECONNREFUSED 127.0.0.1:3100

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:6 (4 by maintainers)

github_iconTop GitHub Comments

4reactions
marcosvega91commented, Sep 24, 2020

Hi @veronesecoms thanks for raising this 😄

Have you tried to use onUnhandledRequest option to understand if you have configured your handler well?

You can add the option in this way

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

If this is not useful for your case, could I ask you to create a reproduction repo ? I can help you better.

Thank you so much

0reactions
charitha95commented, Aug 27, 2022

Im still getting this error. This is a repo which re produces the error. If anyone could help me, that would be awesome ❤️ https://github.com/charitha95/msw-test

Read more comments on GitHub >

github_iconTop Results From Across the Web

connect ECONNREFUSED when using msw with react-testing ...
Jest executes all describe handlers in a test file before it executes any of the actual tests. This is another reason to do...
Read more >
connect ECONNREFUSED when integration testing a Node.js ...
In an express.js app that I was integration testing using Mocha the tests suddenly started acting weird ... "before all" hook: Error: connect...
Read more >
Frequently asked questions - Mock Service Worker Docs
However, you can use Mock Service Worker in your React Native project for unit and integration tests that run in Node.
Read more >
connect econnrefused 127.0.0.1:80 - You.com | The AI Search ...
React : JSDOM 13 Error: Error: connect ECONNREFUSED 127.0.0.1:80 ... import '@testing-library/jest-dom/extend-expect'; // configure msw import server from '.
Read more >
React-Testing-Library - What do you guys use to mock HTTP ...
I tried: axios-mock-adapter Axios test fails when you use a proxy in your package.json. Error: connect ECONNREFUSED 127.0.0.1:80 Other then ...
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