Lesson 9: Unit Test - Timeout 500s Error. (on localhost) #2102
-
Hey, Guys!
Lottery.test.jsdescribe("fulfillRandomWords", () => {
beforeEach(async () => {
await lottery.enterLottery({ value: lotteryEntranceFee })
await network.provider.send("evm_increaseTime", [interval.toNumber() + 1])
await network.provider.send("evm_mine", [])
})
it("picks a winner, resets the lottery, and sends money", async () => {
const accounts = await ethers.getSigners()
const startingAccountIndex = 1 // since deployer is 0. New accounts will starts from index 1
const additionalEntrances = 3
for (let i = startingAccountIndex; i <= additionalEntrances; i++) {
const accountConnectedLottery = lottery.connect(accounts[i])
await accountConnectedLottery.enterLottery({ value: lotteryEntranceFee })
}
const startingTimeStamp = await lottery.getLastTimeStamp()
await new Promise(async (resolve, reject) => {
lottery.once("WinnerPicked", async () => {
console.log("Found the events")
try {
const recentWinner = await lottery.getRecentwinner()
console.log(recentWinner)
console.log(accounts[0].address)
console.log(accounts[1].address)
console.log(accounts[2].address)
console.log(accounts[3].address)
const lotteryState = await lottery.getLotteryState()
const playersCount = await lottery.getNumberOfPlayers()
const endingTimeStamp = await lottery.getLastTimeStamp()
assert.equal(playersCount.toString(), "0")
assert.equal(lotteryState.toString(), "0")
assert(endingTimeStamp > startingTimeStamp)
} catch (e) {
reject(e)
}
resolve()
})
const tx = await lottery.performUpkeep([])
const txReceipt = tx.wait(1)
await vrfCoordinatorV2Mock.fulfillRandomWords(
txReceipt.events[1].args.requestId,
lottery.address
)
})
})
}) fulfillRandomWords Functionfunction fulfillRandomWords(
uint256, /*requestId*/
uint256[] memory randomWords
) internal override {
uint256 indexOfWinner = randomWords[0] % s_players.length;
address payable recentWinner = s_players[indexOfWinner];
s_recentWinner = recentWinner;
s_lotteryState = LotteryState.OPEN;
s_players = new address payable[](0);
s_lastTimeStamp = block.timestamp;
(bool success, ) = recentWinner.call{value: address(this).balance}("");
if (!success) {
revert Lottery__TransferFailed();
}
emit WinnerPicked(recentWinner);
} If you need more code then here is my repo link Thanks In Advance! |
Beta Was this translation helpful? Give feedback.
Replies: 4 comments 11 replies
-
Hey @RohitKS7 you Lotter.test.js file doesn't have the code above. Please push all the updates and notify me. |
Beta Was this translation helpful? Give feedback.
-
Hey, put this code: const tx = await lottery.performUpkeep([])
const txReceipt = tx.wait(1)
await vrfCoordinatorV2Mock.fulfillRandomWords(
txReceipt.events[1].args.requestId,
lottery.address
) In a try and catch block, Like this try{
const tx = await lottery.performUpkeep([])
const txReceipt = tx.wait(1)
await vrfCoordinatorV2Mock.fulfillRandomWords(
txReceipt.events[1].args.requestId,
lottery.address
)
} catch (error) {
console.log(error);
} This will help you to debug accordingly. |
Beta Was this translation helpful? Give feedback.
-
@RohitKS7 is this repo updated? |
Beta Was this translation helpful? Give feedback.
-
Hey @RohitKS7 checkout the PR i've made, that should fix your issue. |
Beta Was this translation helpful? Give feedback.
Hey @RohitKS7 checkout the PR i've made, that should fix your issue.
There was an uncommented
NOTE
on line 146 that was weirdly resulting in the promise not being resolved. I also added theawait tx.wait(1)
as @adityabhattad2021 suggested.