Test passed but the error message "Error: connect ECONNREFUSED 127.0.0.1:80" shows up

Test passed but the error message "Error: connect ECONNREFUSED 127.0.0.1:80" shows up

我正在使用 Jest 和 React 测试库编写测试代码。

代码通过,但显示错误“错误:连接 ECONNREFUSED 127.0.0.1:80”。

这是代码。


describe('Purchase button', () => {
  test('It should be working and move to complete page', async () => {

:

    // Push 'Submit' button
    act(() => {
      fireEvent.click(getByText(/Submit/));
    })
    expect(spyAPI).toBeCalledWith(`${id}`);
    // Check moving to complete page.
    await waitFor(() => {
      expect(getByText(/Thank you, you have purchased/)).toBeInTheDocument();
    })
:


如果最后一行像下面这样被注释掉,则不会出现错误信息

/*
await waitFor(() => {
  expect(getByText(/Thank you, you have purchased/)).toBeInTheDocument();
})
*/

EConnectionRefused 表示仍有一个 API 调用正在执行。我不知道你是如何监视你的。当您注释掉 waitFor 时它不存在的原因是因为您的测试在您执行它之前就退出了。如果您在调用后设置状态,那么在这种情况下您可能会收到行为警告。