The assertion for the following test fails:
it('should access MAX_COUNT', async () => {
const maxCount = await myContract.functions.MAX_COUNT();
expect(maxCount).to.equal(64);
});
With the following error:
AssertionError: expected [ BigNumber { value: "64" } ] to equal 64
This is the solidity code for (the concise version of) the smart contract being tested:
// SPDX-License-Identifier: GPL-3.0
pragma solidity 0.8.17;
contract MyContract
{
uint256 public constant MAX_COUNT = 64;
}
Why is the return value [ BigNumber { value: "64" } ] instead of just BigNumber { value: "64" }?
For context, this question was originally created while trying to figure out this one:
How to correctly import @nomicfoundation/hardhat-chai-matchers into hardhat project? ...
However turns out to be completely unrelated.
This test should fail:
However, this should pass:
And this test should pass too:
The clue is in the documentation, specifically the difference between the return types when called via
Contract.functions.XYZ()vsContract.XYZ():docs for
Contract.functionscallIn summary
Contract.XYZ()returns eitherPromise< any >orPromise< Result >; whereasContract.functions.XYZ()always returnsPromise< Result >docs for
Resultreturn typeThe
Resultobject is designed for functions that can return multiple values etc, so that's why it it needs to be array-destructured.