代码里面用到了 lodash 的 debounce 方法,在测试的时候报了这样的错误:
Ran 100000 timers, and there are still more! Assuming we've hit an infinite recursion and bailing out...
根据参考资料里面大神的意见,修改
runAllTimers
为 runOnlyPendingTimers
,上面的错误确实不报了,但却引入了新莫名其妙的问题。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"));
});
});
)