在我的componentDidMount中添加Promise之后,测试不再通过。如何解决componentDidMount中的诺言?我需要类似runOnlyPendingTimers的东西,但是要保证。
我的测试是:
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);
});
最佳答案
也许对任何人派上用场。如果在componentDidMount中使用promise,则在测试中需要等待,而挂载将结束
it("should clear canvas on each frame draw", async () => {
await 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);
});
关于javascript - 如何在单元测试中解决 promise ?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/49403702/