返回

javascript-我是否有一个无用的act调用,还是这是一个React 18问题

发布时间:2022-08-16 07:52:04 262
# ios

我目前在测试中收到一个错误:

12 |     const response = await axios.get('http://mock-api-call/weather/get-weather');
      13 |
    > 14 |     setWeather(response.data.result.weather);
         |     ^
      15 |   };
      16 |
      17 |   useEffect(() => {

错误引用了我的api调入组件。最初,我从React收到一条错误消息,将我的测试包装在一个act函数中,我遵循了它的提示,但不知道我是否正确地遵循了它,尽管所有测试都在工作,我想知道我是否错误地使用了act,这是否是React 18问题。以下是一些测试示例:

jest.mock('axios');
const mockedAxios = axios as jest.Mocked;

describe('Return Weather Component Data', () => {
  beforeEach(() => {
    mockedAxios.get.mockResolvedValue({
      data: {
        result: {
          weather: {
            forcast: 'Sunny',
            max: 28,
            min: 17,
            description: 'Clear skys all day with a warm summber breaze ariving in the afternoon',
          },
        },
      },
    });
  });

  test('should return description', async () => {
    await act(async () => {
      const { getByText } = render();
      waitFor(() => {
        expect(
          getByText('Clear skys all day with a warm summber breaze ariving in the afternoon'),
        ).toBeInTheDocument();
      });
    });
  });

  test('should return forcast', async () => {
    await act(async () => {
      const { getByText } = render();
      waitFor(() => {
        expect(getByText(/Sunny/i)).toBeInTheDocument();
      });
    });
  });
  test('should return temp', async () => {
    await act(async () => {
      const { getByText } = render();
      waitFor(() => {
        expect(getByText(/Sunny with a low of 17 and a high of 28/i)).toBeInTheDocument();
      });
    });
  });
});

错误消息:

   Warning: The current testing environment is not configured to support act(...)
特别声明:以上内容(图片及文字)均为互联网收集或者用户上传发布,本站仅提供信息存储服务!如有侵权或有涉及法律问题请联系我们。
举报
评论区(0)
按点赞数排序
用户头像
下一篇
autodesk-FORGE:创建项目模板 2022-08-16 05:34:23