I have created 2 TESTS —
In 2nd TEST, I have enclosed owner, addr1, addr2 in []
as per the Official Hardhat Documentation, like this const [owner,addr1,addr2] = await ethers.getSigners();
,
But the problem is when I use []
bracket, it shows me the error TypeError: Cannot read properties of undefined (reading 'equal')
and the test also failed,
Here is the Code —>
const { expect } = require('chai'); // const { ethers } = require('hardhat'); describe('Token contract', function () { //1st TEST it('Deployment should assign the total supply of the tokens to the owner', async function () { const [owner] = await ethers.getSigners(); const Token = await ethers.getContractFactory('Token'); const hardhatToken = await Token.deploy(); const ownerBalance = await hardhatToken.balanceOf(owner.address); expect(await hardhatToken.totalSupply()).to.equal(ownerBalance); }); //2nd TEST it('Should Transfer Tokens between accounts', async function () { const [owner,addr1,addr2] = await ethers.getSigners(); const Token = await ethers.getContractFactory('Token'); const hardhatToken = await Token.deploy(); //Transfer 10 tokens from Owner to addr1 await hardhatToken.transfer(addr1.address,10); expect(await hardhatToken.balanceOf(addr1.address).to.equal(10)); //Transfer 5 tokens from addr1 to addr2 await hardhatToken.connect(addr1).transfer(addr2.address,5); expect(await hardhatToken.balanceOf(addr2.address).to.equal(5)) }); });
But if U see in the 1st TEST, I haven’t used []
, for owner, so the test passed.
Below is the Official Hardhat documentation if U want to check the code —>
https://hardhat.org/tutorial/testing-contracts.html
Please help me to solve this Problem Thanks
Advertisement
Answer
You didn’t close the parenthesis around the expect
calls in the second test correctly. You’re accessing .to
on the number returned by .balanceOf
.
Replace with:
expect(await hardhatToken.balanceOf(addr1.address)).to.equal(10); // ... expect(await hardhatToken.balanceOf(addr2.address)).to.equal(5);