Lesson 9: fulfillRandomWords unit testing time out #3401
-
Hello, I've been struggling to understand why the event "WinnerPicked" is never triggered when testing in hardhat. I've tried some debugging and seems like await vrfCoordinatorV2Mock.fulfillRandomWords(
txReceipt.events[1].args.requestId,
raffle.address
) I've tried forcing the event myself but it only works if I do it before I'd really appreciate some help on this. Section from Raffle.test.js below and my repo is @0xJayPi/hardhat-smartcontract-lottery const { assert, expect } = require("chai")
const { getNamedAccounts, deployments, ethers, network } = require("hardhat")
const { developmentChains, networkConfig } = require("../../helper-hardhat-config")
!developmentChains.includes(network.name)
? describe.skip
: describe("Raffle Unit Test", function () {
let raffle, vrfCoordinatorV2Mock, raffleEntranceFee, deployer, interval
const chainId = network.config.chainId
beforeEach(async function () {
await deployments.fixture(["all"])
deployer = (await getNamedAccounts()).deployer
raffle = await ethers.getContract("Raffle", deployer)
vrfCoordinatorV2Mock = await ethers.getContract("VRFCoordinatorV2Mock")
raffleEntranceFee = await raffle.getEntranceFee()
interval = await raffle.getInterval()
})
describe("fulfillRandomWords", function () {
beforeEach(async function () {
await raffle.enterRaffle({ value: raffleEntranceFee })
await network.provider.send("evm_increaseTime", [interval.toNumber() + 1])
await network.provider.send("evm_mine", [])
})
it("Can only be called after perfomrUpkeep", async function () {
await expect(
vrfCoordinatorV2Mock.fulfillRandomWords(0, raffle.address)
).to.be.revertedWith("nonexistent request")
await expect(
vrfCoordinatorV2Mock.fulfillRandomWords(1, raffle.address)
).to.be.revertedWith("nonexistent request")
})
it("Picks a winner, resets the lottery, and sends the money", async function () {
const additionalEntrances = 3
const startingAccountIndex = 1 // deployer = 0
const accounts = await ethers.getSigners()
for (
let i = startingAccountIndex;
i < startingAccountIndex + additionalEntrances;
i++
) {
const accountConnectedRaffle = raffle.connect(accounts[i])
await accountConnectedRaffle.enterRaffle({ value: raffleEntranceFee })
}
const startingTimeStamp = await raffle.getLatestTimeStamp()
await new Promise(async (resolve, reject) => {
/* Events Listener */
raffle.once("WinnerPicked", async () => {
console.log("Found the event!")
try {
const recentWinner = await raffle.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 raffleState = await raffle.getRaffleState()
const endingTimeStamp = await raffle.getLatestTimeStamp()
const numPlayers = await raffle.getNumberOfPlayers()
assert.equal(raffleState.toString(), "0")
assert.equal(numPlayers.toString(), "0")
assert(endingTimeStamp > startingTimeStamp)
resolve()
} catch (e) {
reject(e)
}
})
const txResponse = await raffle.performUpkeep([])
const txReceipt = await txResponse.wait(1)
await vrfCoordinatorV2Mock.fulfillRandomWords(
txReceipt.events[1].args.requestId,
raffle.address
)
raffle.emit("WinnerPicked") // this is a test to force the event
})
})
})
})
|
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 14 replies
-
@0xJayPi console log this |
Beta Was this translation helpful? Give feedback.
@0xJayPi console log this
txReceipt
and show me.