[Jest] lodash debounce 方法

代码里面用到了 lodash 的 debounce 方法,在测试的时候报了这样的错误:

Ran 100000 timers, and there are still more! Assuming we've hit an infinite recursion and bailing out...

[Jest] lodash debounce 方法_第1张图片

根据参考资料里面大神的意见,修改 runAllTimersrunOnlyPendingTimers,上面的错误确实不报了,但却引入了新莫名其妙的问题。 expect 语句老报错。

修改代码如下(加上 setTimeout ):

jest.useFakeTimers();

describe(
  it("should update patch failed", async () => {
    const mockUpdatePatch = PatchApi.updateWinPatch as jest.Mock;
    mockUpdatePatch.mockRejectedValueOnce(new Error("http error"));
    const spyOnHandleHttpError = jest.spyOn(kit, "handleHttpError");

    const wrapper = mount(
      
    );
    wrapper.find(Form).simulate("submit");
    await jest.runOnlyPendingTimers();
    setTimeout(async () => {
      await (global as any).flushPromises();
      wrapper.update();
      expect(spyOnHandleHttpError).toBeCalledWith(new Error("http error"));
    });
  });
)

参考资料:
https://github.com/facebook/jest/issues/3465

你可能感兴趣的:([Jest] lodash debounce 方法)