kshsri21 / Hardhat

12 stars 8 forks source link

Big Number error in test/token.js #1

Open Naveen-2021ucp1387 opened 3 months ago

Naveen-2021ucp1387 commented 3 months ago

hello bhaiya in test folder the file token.js is showing error while running

ERROR !!! Should assign the total supply of tokens to the owner:

  AssertionError: expected BigNumber{ _hex: '0x2710', …(1) } to equal BigNumber{ _hex: '0x2710', …(1) }
  + expected - actual
Naveen-2021ucp1387 commented 3 months ago

Bhaiya I have resolved this issue by using the String. for comparing the Big number or we can also use chai - Big Number to resolve this . i am sharing my code please look into it

Naveen-2021ucp1387 commented 3 months ago

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

describe("Token Contract", function () { let Token; let hardhatToken; let owner; let addr1; let addr2; let addrs;

beforeEach(async function () { Token = await ethers.getContractFactory("Token"); [owner, addr1, addr2, ...addrs] = await ethers.getSigners(); hardhatToken = await Token.deploy(); });

describe("Deployment", function () { it("Should set the right owner", async function () { expect(await hardhatToken.owner()).to.equal(owner.address); }); it("Should assign the total supply of tokens to the owner", async function () { const ownerBalance = await hardhatToken.balanceOf(owner.address); expect(await hardhatToken.totalSupply()).to.equal(ownerBalance); }); });

describe("Transactions", function () { it("Should trasfer tokens between accounts", async function () { //owner account to addr1.address await hardhatToken.transfer(addr1.address, 5); const addr1Balance = await hardhatToken.balanceOf(addr1.address); expect(addr1Balance).to.equal(5);

  await hardhatToken.connect(addr1).transfer(addr2.address, 5);
  const addr2Balance = await hardhatToken.balanceOf(addr2.address);
  expect(addr2Balance).to.equal(5);
});

it("Should fail if sender does not have enough tokens", async function () {
  const initialOwnerBalance = await hardhatToken.balanceOf(owner.address); //10000
  await expect(
    hardhatToken.connect(addr1).transfer(owner.address, 1) //initially - 0 tokens addr1
  ).to.be.revertedWith("Not enough tokens");
  expect(await hardhatToken.balanceOf(owner.address)).to.equal(
    initialOwnerBalance
  );
});

it("Should update balances after transfers", async function () {
  const initialOwnerBalance = await hardhatToken.balanceOf(owner.address);
  await hardhatToken.transfer(addr1.address, 5);
  await hardhatToken.transfer(addr2.address, 10);

  const finalOwnerBalance = await hardhatToken.balanceOf(owner.address);
  expect(finalOwnerBalance).to.equal(initialOwnerBalance - 15);

  const addr1Balance = await hardhatToken.balanceOf(addr1.address);
  expect(addr1Balance).to.equal(5);
  const addr2Balance = await hardhatToken.balanceOf(addr2.address);
  expect(addr2Balance).to.equal(10);
});

}); });

this code is working fine