gpt4 book ai didi

javascript - 如何解决单元测试中的 promise ?

转载 作者:行者123 更新时间:2023-11-30 20:39:48 26 4
gpt4 key购买 nike

在我的 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);
});

最佳答案

也许对任何人都能派上用场。如果你在 componentDidMount 中使用 promise,在测试中你需要 await while mount while mount will end

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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com