【发布时间】:2023-03-27 19:27:01
【问题描述】:
在我的 componentDidMount 中添加 Promise 后,测试不再通过。如何解决 componentDidMount 中的承诺?我需要类似 runOnlyPendingTimers 之类的东西,但为了 Promise。
我的测试是:
it("should clear canvas on each frame draw", () => {
mount(<CoinsAndStars stars={true} coins={true} DeviceSupport={DeviceSupport} />);
ctxMock.clearRect = jest.fn();
jest.runOnlyPendingTimers();
expect(ctxMock.clearRect).toHaveBeenCalledTimes(1);
jest.runOnlyPendingTimers();
expect(ctxMock.clearRect).toHaveBeenCalledTimes(2);
jest.runOnlyPendingTimers();
expect(ctxMock.clearRect).toHaveBeenCalledTimes(3);
});
【问题讨论】:
-
可能欺骗here
-
您可以将其包装在 Async/await 中,只需将其放入级联 .then 块中即可。
-
你是什么意思?我有 .then 在我的 componentDidMount
-
@Shammoo 是这样的吗?
(async function() { mount(<CoinsAndStars stars={true} coins={true} DeviceSupport={DeviceSupport} />); ctxMock.clearRect = jest.fn(); jest.runOnlyPendingTimers(); expect(ctxMock.clearRect).toHaveBeenCalledTimes(1); jest.runOnlyPendingTimers(); expect(ctxMock.clearRect).toHaveBeenCalledTimes(2); jest.runOnlyPendingTimers(); expect(ctxMock.clearRect).toHaveBeenCalledTimes(3); })(); -
前一个不会改变行为。你能分享你的
cdm函数的摘录吗?你想测试什么?
标签: javascript reactjs enzyme jestjs