[Ethereum] How to use BigNumber values in Hardhat tests

bignumberhardhattesting

I'm using Hardhat with TypeScript/Ethers/Chai. I can't find a way to test BigNumber values.

For example, expect(someBigNumber).to.be.equal.to.(otherBigNumber) will (understandably) throw an error like this one: AssertionError: expected 9999969797040000000000 to be a number or a date.

I installed chai-bn and followed the example here. I can't run the tests because Property 'bignumber' does not exist on type 'Assertion'. I can't figure out the issue and I can't find a working example either. chai-bignumber doesn't work either.

I'd appreciate if someone could point me in the right direction.

Best Answer

The ethereum-waffle package does this very well.

To use it add this to your hardhat.config.ts:

import chai from "chai";
import { solidity } from "ethereum-waffle";

chai.use(solidity);

Then in your tests you can directly compare ethers.js BigNumber values:

expect(await token.balanceOf(wallet.address)).to.equal(993);

See ethereum-waffle docs for more details.