如何在单元测试中解决承诺?

时间:2018-03-21 10:21:27

标签: javascript reactjs enzyme jest

我在我的componentDidMount中添加了Promise后,测试不再通过。如何解决componentDidMount中的promise?我需要像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);
});

1 个答案:

答案 0 :(得分:1)

也许任何人派上用场。如果你在componentDidMount中使用promise,那么在test中你需要等待mount将结束

var {google} = require('googleapis');