Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Lesson 9: TypeError: Cannot read properties of undefined (reading 'enterRaffle') #5347

Unanswered
devcaii asked this question in Q&A
Discussion options

this code gives me the following error:

const { getNamedAccounts, deployments, ethers, network } = require("hardhat");
const { assert, expect } = require("chai");
const { developmentChains, networkConfig } = require("../../helper-hardhat-config");
const { beforeEach } = require("node:test");
!developmentChains.includes(network.name)
	? describe.skip
	: describe("Raffle", async () => {
			let raffle, vrfCoordinatorV2Mock, raffleEntranceFee, deployer, interval;
			const chainId = network.config.chainId;
			beforeEach(async () => {
				deployer = (await getNamedAccounts()).deployer;
				await deployments.fixture(["all"]);
				raffle = await ethers.getContract("Raffle", deployer);
				vrfCoordinatorV2Mock = await ethers.getContract("VRFCoordinatorV2Mock", deployer);
				raffleEntranceFee = await raffle.getEntranceFee();
				interval = await raffle.getInterval();
			});
			describe("constructor", async () => {
				it("initializes raffle correctly", async () => {
					const raffleState = await raffle.getRaffleState();
					const interval = await raffle.getInterval();
					assert.equal(interval.toString(), networkConfig[chainId]["interval"]);
					assert.equal(raffleState.toString(), "0");
				});
			});
			describe("enter raffle", async () => {
				it("reverts when dont pay enough", async () => {
					await expect(raffle.enterRaffle()).to.be.revertedWith(
						"Raffle__NotEnoughETHEntered"
					);
				});
				it("records players when they enter", async () => {
					await raffle.enterRaffle({ value: raffleEntranceFee });
					const playerFromContract = await raffle.getPlayer(0);
					assert.equal(playerFromContract, deployer);
				});
				it("emits event on enter", async () => {
					await expect(raffle.enterRaffle({ value: raffleEntranceFee })).to.emit(
						raffle,
						"RaffleEnter"
					);
				});
				it("doesnt allow entrance when raffle is calculating", async () => {
					await raffle.enterRaffle({ value: raffleEntranceFee });
					await network.provider.send("evm_increaseTime", [interval.toNumber() + 1]);
					await network.provider.send("evm_mine", []);
					await raffle.performUpkeep([]);
					await expect(
						raffle.enterRaffle({ value: raffleEntranceFee })
					).to.be.revertedWith("Raffle__NotOpen");
				});
			});
			describe("checkUpkeep", async () => {
				it("turns false if ppl havent sent any eth", async () => {
					await network.provider.send("evm_increaseTime", [interval.toNumber() + 1]);
					await network.provider.send("evm_mine", []);
					const { upkeepNeeded } = await raffle.callStatic.checkUpkeep([]);
					console.log(upkeepNeeded);
					assert(!upkeepNeeded);
				});
				it("returns false if raffle isnt open", async () => {
					await raffle.enterRaffle({ value: raffleEntranceFee });
					await network.provider.send("evm_increaseTime", [interval.toNumber() + 1]);
					await network.provider.send("evm_mine", []);
					await raffle.performUpkeep("0x");
					const raffleState = await raffle.getRaffleState();
					const { upkeepNeeded } = await raffle.callStatic.checkUpkeep([]);
					assert.equal(raffleState.toString(), "1");
					assert.equal(upkeepNeeded, false);
				});
				it("returns false if enough time hasn't passed", async () => {
					await raffle.enterRaffle({ value: raffleEntranceFee });
					await network.provider.send("evm_increaseTime", [interval.toNumber() - 5]); // use a higher number here if this test fails
					await network.provider.request({ method: "evm_mine", params: [] });
					const { upkeepNeeded } = await raffle.callStatic.checkUpkeep("0x"); // upkeepNeeded = (timePassed && isOpen && hasBalance && hasPlayers)
					assert(!upkeepNeeded);
				});
				it("returns true if enough time has passed, has players, eth, and is open", async () => {
					await raffle.enterRaffle({ value: raffleEntranceFee });
					await network.provider.send("evm_increaseTime", [interval.toNumber() + 1]);
					await network.provider.request({ method: "evm_mine", params: [] });
					const { upkeepNeeded } = await raffle.callStatic.checkUpkeep("0x"); // upkeepNeeded = (timePassed && isOpen && hasBalance && hasPlayers)
					assert(upkeepNeeded);
				});
			});
			describe("performUpkeep", () => {
				it("can only perform if checkupkeep is true", async () => {
					await raffle.enterRaffle({ value: raffleEntranceFee });
					await network.provider.send("evm_increaseTime", [interval.toNumber() + 1]);
					await network.provider.send("evm_mine", []);
					const tx = await raffle.performUpkeep([]);
					assert(tx);
				});
				it("reverts when checkupkeep is false", async () => {
					await expect(raffle.performUpkeep([])).to.be.revertedWith(
						"Raffle__UpkeepNotNeeded"
					);
				});
				it("updates the raffle state, emits an event and calls the vrf coordinator", async () => {
					await raffle.enterRaffle({ value: raffleEntranceFee });
					await network.provider.send("evm_increaseTime", [interval.toNumber() + 1]);
					await network.provider.send("evm_mine", []);
					const txResponse = await raffle.performUpkeep("0x");
					const txReciept = await txResponse.wait(1);
					const requestId = txReciept.events[1].args.requestId;
					const raffleState = await raffle.getRaffleState();
					assert(requestId.toNumber() > 0);
					assert(raffleState == 1);
				});
			});
			describe("fulfillRandomWords", function () {
				it("can only be called after performupkeep", async () => {
					await raffle.enterRaffle({ value: raffleEntranceFee });
					await network.provider.send("evm_increaseTime", [interval.toNumber() + 1]);
					await network.provider.request({ method: "evm_mine", params: [] });
					await expect(
						vrfCoordinatorV2Mock.fulfillRandomWords(0, raffle.address) // reverts if not fulfilled
					).to.be.revertedWith("nonexistent request");
					await expect(
						vrfCoordinatorV2Mock.fulfillRandomWords(1, raffle.address) // reverts if not fulfilled
					).to.be.revertedWith("nonexistent request");
				});
			});
	 });

ERROR:


 constructor
 1) initializes raffle correctly
 enter raffle
 2) reverts when dont pay enough
 3) records players when they enter
 4) emits event on enter
 5) doesnt allow entrance when raffle is calculating
 checkUpkeep
 6) turns false if ppl havent sent any eth
 7) returns false if raffle isnt open
 8) returns false if enough time hasn't passed
 9) returns true if enough time has passed, has players, eth, and is open
 performUpkeep
 10) can only perform if checkupkeep is true
 11) reverts when checkupkeep is false
 12) updates the raffle state, emits an event and calls the vrf coordinator
 fulfillRandomWords
 13) can only be called after performupkeep
 0 passing (142ms)
 13 failing
 1) Raffle
 constructor
 initializes raffle correctly:
 TypeError: Cannot read properties of undefined (reading 'getRaffleState')
 at Context.<anonymous> (test/unit/raffle.test.js:24:39)
 at processImmediate (node:internal/timers:471:21)
 2) Raffle
 enter raffle
 reverts when dont pay enough:
 TypeError: Cannot read properties of undefined (reading 'enterRaffle')
 at Context.<anonymous> (test/unit/raffle.test.js:32:26)
 at processImmediate (node:internal/timers:471:21)
 3) Raffle
 enter raffle
 records players when they enter:
 TypeError: Cannot read properties of undefined (reading 'enterRaffle')
 at Context.<anonymous> (test/unit/raffle.test.js:37:19)
 at processImmediate (node:internal/timers:471:21)
 4) Raffle
 enter raffle
 emits event on enter:
 TypeError: Cannot read properties of undefined (reading 'enterRaffle')
 at Context.<anonymous> (test/unit/raffle.test.js:42:26)
 at processImmediate (node:internal/timers:471:21)
 5) Raffle
 enter raffle
 doesnt allow entrance when raffle is calculating:
 TypeError: Cannot read properties of undefined (reading 'enterRaffle')
 at Context.<anonymous> (test/unit/raffle.test.js:48:19)
 at processImmediate (node:internal/timers:471:21)
 6) Raffle
 checkUpkeep
 turns false if ppl havent sent any eth:
 TypeError: Cannot read properties of undefined (reading 'toNumber')
 at Context.<anonymous> (test/unit/raffle.test.js:59:64)
 at processImmediate (node:internal/timers:471:21)
 7) Raffle
 checkUpkeep
 returns false if raffle isnt open:
 TypeError: Cannot read properties of undefined (reading 'enterRaffle')
 at Context.<anonymous> (test/unit/raffle.test.js:66:19)
 at processImmediate (node:internal/timers:471:21)
 8) Raffle
 checkUpkeep
 returns false if enough time hasn't passed:
 TypeError: Cannot read properties of undefined (reading 'enterRaffle')
 at Context.<anonymous> (test/unit/raffle.test.js:76:19)
 at processImmediate (node:internal/timers:471:21)
 9) Raffle
 checkUpkeep
 returns true if enough time has passed, has players, eth, and is open:
 TypeError: Cannot read properties of undefined (reading 'enterRaffle')
 at Context.<anonymous> (test/unit/raffle.test.js:83:19)
 at processImmediate (node:internal/timers:471:21)
 10) Raffle
 performUpkeep
 can only perform if checkupkeep is true:
 TypeError: Cannot read properties of undefined (reading 'enterRaffle')
 at Context.<anonymous> (test/unit/raffle.test.js:92:19)
 at processImmediate (node:internal/timers:471:21)
 11) Raffle
 performUpkeep
 reverts when checkupkeep is false:
 TypeError: Cannot read properties of undefined (reading 'performUpkeep')
 at Context.<anonymous> (test/unit/raffle.test.js:99:26)
 at processImmediate (node:internal/timers:471:21)
 12) Raffle
 performUpkeep
 updates the raffle state, emits an event and calls the vrf coordinator:
 TypeError: Cannot read properties of undefined (reading 'enterRaffle')
 at Context.<anonymous> (test/unit/raffle.test.js:104:19)
 at processImmediate (node:internal/timers:471:21)
 13) Raffle
 fulfillRandomWords
 can only be called after performupkeep:
 TypeError: Cannot read properties of undefined (reading 'enterRaffle')
 at Context.<anonymous> (test/unit/raffle.test.js:118:19)
 at processImmediate (node:internal/timers:471:21)

Obviously the raffle for some reason is not defined, but however when i run the code without the last describe block, it did once work fine, however now when i try to replicate what i did earlier in removing the block of code, its now not fixing it. Any help or insight into why this is happening, would be greatly appreciated, as ive tried quite extensively to fix it myself, may just be a silly issue. But i dont believe theres any issue with the packages im using, because when i copied the example code in the repository, it worked fine, albeit with a few errors because of different names in my solidity functions.

You must be logged in to vote

Replies: 4 comments

Comment options

Heres my package.json just incase anyone does think its a packages issue

{
	"name": "back-end",
	"version": "1.0.0",
	"main": "index.js",
	"license": "MIT",
	"devDependencies": {
		"@chainlink/contracts": "0.4.1",
		"@nomiclabs/hardhat-ethers": "npm:hardhat-deploy-ethers@^0.3.0-beta.13",
		"@nomiclabs/hardhat-etherscan": "^3.1.7",
		"@nomiclabs/hardhat-waffle": "^2.0.5",
		"chai": "^4.3.7",
		"dotenv": "^16.0.3",
		"ethereum-waffle": "^4.0.10",
		"ethers": "^5.5.1",
		"hardhat": "^2.14.0",
		"hardhat-contract-sizer": "^2.8.0",
		"hardhat-deploy": "^0.11.26",
		"hardhat-deploy-ethers": "npm:hardhat-deploy-ethers@^0.3.0-beta.13",
		"hardhat-gas-reporter": "^1.0.9",
		"prettier": "^2.8.7",
		"prettier-plugin-solidity": "^1.1.3",
		"solhint": "^3.4.1",
		"solidity-coverage": "^0.8.2"
	}
}
You must be logged in to vote
0 replies
Comment options

@devcaii Please leave your repository link so I can test it locally.

You must be logged in to vote
0 replies
Comment options

Hey, I had the same issue, then i realised vs code inserted for me on top of my "test" file:
const { it } = require("node:test")

Once I deleted this line it solved my issue.
I see in your file you have something quite similar:
const { beforeEach } = require("node:test")

I think you should get rid of this line

You must be logged in to vote
0 replies
Comment options

so i had the same issue and i removed callstatic from the line
const { upkeepNeeded } = await raffle.callStatic.checkUpkeep("0x");
to
const { upkeepNeeded } = await raffle.checkUpkeep("0x");

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

AltStyle によって変換されたページ (->オリジナル) /