简体   繁体   English

将 msw 与 react-testing-library (NextJS + SWR) 结合使用时连接 ECONNREFUSED

[英]connect ECONNREFUSED when using msw with react-testing-library (NextJS + SWR)

I'm struggling mocking the SWR fetch api call with MSW.我正在努力 mocking SWR 获取 api 呼叫与 MSW。

You can reproduce the issue with this repo: https://github.com/charitha95/msw-test您可以使用此 repo重现该问题https://github.com/charitha95/msw-test

Error that I'm facing when using MSW:我在使用 MSW 时遇到的错误:

Error: connect ECONNREFUSED 127.0.0.1:80 at Object.dispatchError

在此处输入图像描述

My test file:我的测试文件:

import "@testing-library/jest-dom";
import {
  render,
  screen,
  waitForElementToBeRemoved,
} from "@testing-library/react";
import { rest } from "msw";
import { setupServer } from "msw/node";
import Home from "../pages/index";

const server = setupServer(
  rest.get("/api/colors", (req, res, ctx) => {
    return res(
      ctx.delay(100),
      ctx.json([
        {
          color: "red",
          value: "#f00",
        },
        {
          color: "green",
          value: "#0f0",
        },
      ])
    );
  })
);

beforeAll(() => server.listen());
afterAll(() => server.close());
afterEach(() => server.resetHandlers());

describe("Home", () => {
  render(<Home />);
  it("renders list of colors", async () => {
    await waitForElementToBeRemoved(screen.getByText("loading..."));

    const colors = screen.getByTestId("color-list");
    expect(colors).toBeInTheDocument();
    expect(screen.getByText("BMW")).toBeInTheDocument();
  }, 1500);
});

Things I looked at, but no luck:我看过的东西,但没有运气:
github github
stackoverflow 堆栈溢出

Jest executes all describe handlers in a test file before it executes any of the actual tests. Jest 在执行任何实际测试之前执行测试文件中的所有描述处理程序。 This is another reason to do setup and teardown inside before* and after* handlers rather than inside the describe blocks.这是在 before* 和 after* 处理程序内部而不是在 describe 块内部进行设置和拆卸的另一个原因。 Once the describe blocks are complete, by default Jest runs all the tests serially in the order they were encountered in the collection phase, waiting for each to finish and be tidied up before moving on.一旦描述块完成,默认情况下,Jest 按照它们在收集阶段遇到的顺序依次运行所有测试,等待每个测试完成并在继续之前进行整理。

You should not be doing any setup in describe blocks.您不应该在描述块中进行任何设置。 The reason this is failing is because your server.listen hasn't even been called when you render the component.失败的原因是您的 server.listen 在渲染组件时甚至没有被调用。

It's showing me the same issue but in my case when I keep the below in jest-setup.ts file它向我展示了同样的问题,但就我而言,当我将以下内容保留在 jest-setup.ts 文件中时

beforeAll(() => server.listen());
afterAll(() => server.close());
afterEach(() => server.resetHandlers());

it's throwing as below: TypeError: this.log.extend is not a function export const server = setupServer(...handlers);它的抛出如下: TypeError: this.log.extend is not a function export const server = setupServer(...handlers);

and when I created setUptests.ts and kept the same code, it's throwing error connection issue as below:当我创建 setUptests.ts 并保留相同的代码时,它会引发错误连接问题,如下所示:

Error: Error: connect ECONNREFUSED 127.0.0.1:80

Did you ever face this issue like this having jest-setUp and setUpTests files togther?你有没有遇到过像这样将 jest-setUp 和 setUpTests 文件放在一起的问题?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM