Search code examples
javascriptsoliditychaiassertionhardhat

Hardhat how to use revertedWith


I'm writing tests for an ERC-20 contract and I'm testing a scenario that should fail with the (OpenZeppelin library-provided) error:

revert ERC20: burn amount exceeds balance

In my terminal output I see:

ProviderError: VM Exception while processing transaction: revert ERC20: burn amount exceeds balance

However when I try the code I can't get test to succeed:

 it("Address #1 can't burn 100M tokens", async function () {
    await expect(contract.connect(signer1)
      .burn(parseEther((100_000_000).toString())))
      .revertedWith('revert ERC20: burn amount exceeds balance')
        });

I've also tried replacing the string argument to revertedWith with "burn amount exceeds balance", "VM Exception while processing transaction: revert ERC20: burn amount exceeds balance", "ProviderError: VM Exception while processing transaction: revert ERC20: burn amount exceeds balance", and "burn amount exceeds balance" yet all of them are failing.

What exact error reason should I be using with revertedWith assertion while testing hardhat with chai? (I'm using standard OpenZeppelin 4 ERC-20 contracts)


Solution

  • I believe that Hardhat is using the same matchers as Waffle. And if you follow the link, you can see that the proper syntax would be

     it("Address #1 can't burn 100M tokens", async function () {
        await expect(contract.connect(signer1)
          .burn(token_amount)
          .to.be.revertedWith('revert ERC20: burn amount exceeds balance')
      });