美文网首页
[Jest] lodash debounce 方法

[Jest] lodash debounce 方法

作者: 小黄人get徐先生 | 来源:发表于2019-11-18 12:02 被阅读0次

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

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

根据参考资料里面大神的意见,修改 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(
      <WrappedCreateOrEditPatchForm isCreate={false} {...data1} />
    );
    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 方法

      本文链接:https://www.haomeiwen.com/subject/tlivictx.html