NomicFoundation / hardhat

Hardhat is a development environment to compile, deploy, test, and debug your Ethereum software.
https://hardhat.org
Other
7.27k stars 1.4k forks source link

Support Chai `members` matcher #4206

Open ItsNickBarry opened 1 year ago

ItsNickBarry commented 1 year ago

Describe the feature

When using Ethers v6, the Chai matchers plugin does not seem to support checking the contents of arrays using the members matcher.

This fails:

expect(
  await instance.balanceOfBatch.staticCall([holder.address], [0n]),
).to.have.deep.members([0n]);

This works:

expect(
  Array.from(await instance.balanceOfBatch.staticCall([holder.address], [0n])),
).to.have.deep.members([0n]);

Reproducible here: https://github.com/solidstate-network/solidstate-solidity/blob/494b278e75561da8b5b9010838bf517d2443ee66/spec/token/ERC1155/ERC1155Base.behavior.ts#L87-L92

Search terms

chai members matchers

schaable commented 1 year ago

hi @ItsNickBarry! This is currently not supported. We'll take it as a feature request. Reproduction example:

// contract

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

import "@openzeppelin/contracts/token/ERC1155/ERC1155.sol";

contract MyERC1155 is ERC1155 {
    constructor() ERC1155("") {
        // Mint some tokens for testing
        uint256 tokenId = 0;
        uint256 initialSupply = 0;
        _mint(msg.sender, tokenId, initialSupply, "");
    }
}

// test file

import { expect } from "chai";
import { ethers } from "hardhat";

describe("MyERC1155", function () {
  async function deployMyERC1155() {
    const [holder] = await ethers.getSigners();

    const MyERC1155 = await ethers.getContractFactory("MyERC1155");
    const myERC1155 = await MyERC1155.deploy();

    return { holder, instance: myERC1155 };
  }

  it("returns the balances of given tokens held by given addresses", async function () {
    const { holder, instance } = await deployMyERC1155();
    const balances = await instance.balanceOfBatch.staticCall(
      [holder.address],
      [0n]
    );
    // doesn't work
    expect(balances).to.have.deep.members([0n]);
    // works
    // expect(Array.from(balances)).to.have.deep.members([0n]);
  });
});