首页 > 解决方案 > 增加 jest.setTimeout() 是一个好习惯吗?

问题描述

我有一个案例,我正在像这样在 Jest 文件中模拟 API Put 请求。

    ....
    ....
    const mockSocialPostUpdate = jest
      .spyOn(api, "put")
      .mockImplementation(() => Promise.resolve());
    
    await waitFor(() => {
      expect(mockSocialPostUpdate).toHaveBeenCalledWith(
        expect.objectContaining({ path: "/api/v1/social-posts/:id" }),
        { id: postContent.id },
        {
          id: postContent.id,
          text: updatedDelta
        }
      );
    });

我不知道为什么,但只有在我得到的这个测试文件中

 Async callback was not invoked within the 5000 ms timeout specified by jest.setTimeout.Timeout - Async callback was not invoked within the 5000 ms timeout specified by jest.setTimeout.Error:

现在唯一可行的解​​决方案是 putjest.setTimeout(30000);但我不确定这是否是最佳实践。我如何调查为什么我会收到此测试的超时错误。我正在做 console.logs 并且 Put 请求正在发生,我正在达到await WaitFor()功能

标签: reactjsjestjs

解决方案


推荐阅读