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

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

我创建了 2 个测试 --

在第二次测试中,我根据官方 Hardhat 文档将所有者、addr1、addr2 包含在 [] 中,如下所示 const [owner,addr1,addr2] = await ethers.getSigners();, 但问题是当我使用 [] 括号时,它显示错误 TypeError: Cannot read properties of undefined (reading 'equal') 并且测试也失败了,

这是代码--->

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))
    });
});

但是如果你在第一次测试中看到,我没有使用 [],作为所有者,所以测试通过了。 如果你想检查代码,下面是官方 Hardhat 文档 --->

https://hardhat.org/tutorial/testing-contracts.html

请帮我解决这个问题 谢谢

enter image description here

您没有正确关闭第二个测试中 expect 调用周围的括号。您正在通过 .balanceOf.

返回的号码访问 .to

替换为:

expect(await hardhatToken.balanceOf(addr1.address)).to.equal(10);
// ...
expect(await hardhatToken.balanceOf(addr2.address)).to.equal(5);