Search code examples
unit-testingethereumsoliditysmartcontractshardhat

TypeError: Cannot read properties of undefined (reading 'address')


I'm getting this error while testing the contract. TypeError: Cannot read properties of undefined (reading 'address')

const{expect} = require("chai");
const hre = require("hardhat");


 describe("Token Contract", function(){

it("Deployment should assign the totalSupply of tokens to the owner", async function()
{
    const signers = await ethers.getSigner();
    const owner = signers[0];

    console.log("Signers Object:", owner);

    const Token = await ethers.getContractFactory("Token");

    const hardhatToken = await Token.deploy();

    const ownerBalance = await hardhatToken.balanceOf(owner.address);

    expect(await hardhatToken.totalSupply()).to.be.equal(ownerBalance);
});

});


Solution

  • You have a typo.

    // this is wrong
    const signers = await ethers.getSigner();
    // this is correct
    const signers = await ethers.getSigners();
    

    getSigner is still a valid command, but it doesn't return an array. That's why signers[0] returned undefined.