更改startGame函数以返回承诺。还修复了其他人提到的setTimeout问题。应该是这样的
startGame = () => {
return this.buildDeck()
.then(this.shuffleDeck)
.then(this.dealToPlayer)
.then(() => setTimeout(this.dealToPlayer, 2000))
.then(() => setTimeout(this.dealToDealer, 4000))
}
这里有两种不同类型的异步;承诺和计时器。在做出断言之前,您需要确保承诺已经解决,计时器已经运行。您可以通过这样的测试来实现这一点(假设您使用的是Jest):
it('expects playersHand to have 2 cards once game started', async () => {
jest.useFakeTimers();
await wrapper.instance().startGame();
jest.runAllTimers();
expect(wrapper.state('playersHand').length).toEqual(2);
});