1

I'm currently writing some tests for my contract to make sure the custom errors are being reverted in the contract. Example:

function fund(address sender, uint256 amount) public {
      if (amount <= 0) {
           revert FundMe__FundAmountMustBeAboveZero();
      }
      doOtherStuff();
}

So in my test I have the following code:

it("fails when you fund 0 tokens", async function () {
     await expect(fundMe.fund(deployer.address, 0)).to.be.revertedWith(
          "FundAmountMustBeAboveZero"
     )
})

Clearly given the code this should be reverted but in the test it keeps telling me that the transaction was not reverted.

AssertionError: Expected transaction to be reverted
      + expected - actual

      -Transaction NOT reverted.
      +Transaction reverted.
      

Even if you look at the transaction on etherscan it says it reverted within the contract execution... https://goerli.etherscan.io/tx/0x6f757bcd1aa1aa336f1fc5a71dd05ac249e175e8ee55f179c038bd4f75e2f32a

Not really sure what to do about this, if this is an error in my code or some weird bug with chai?

3 Answers 3

0

Seems weird, but please use Hardhat console.log and check what is in amount. Beside - you need to use (...).to.be.revertedWithCustomError(fundMe, "FundMe__FundAmountMustBeAboveZero");

13
  • The strangest thing is that it works completely fine on the local hardhat network but when running on the live testnet I get the issue described above
    – l1nkm4rine
    Commented Sep 6, 2022 at 1:51
  • Try to console.log and get back to us with it please :)
    – Cezar
    Commented Sep 6, 2022 at 5:16
  • I did try console.log and it gave me the expected result of zero because it is working on the local hardhat network but since console.log doesn't work on the testnet it doesn't really add any insight :/
    – l1nkm4rine
    Commented Sep 6, 2022 at 14:25
  • So if you do function fund(address sender, uint256 amount) public { console.log("amount: ", amount); if (amount <= 0) { console.log("I'm here!"); revert FundMe__FundAmountMustBeAboveZero(); } doOtherStuff(); } What do you get in console? It is weird that it doesn't go inside if when value is 0. On the other hand you don't need amount <= 0, because amount is a uint type which can't be negative.
    – Cezar
    Commented Sep 6, 2022 at 16:48
  • I get "amount: 0 I'm here!" as expected but like I said the issues are only happening on the testnet and not on the local hardhat network.
    – l1nkm4rine
    Commented Sep 6, 2022 at 17:51
0

to.be.revertedWith with not match with the name of a custom error.

try changing this

 await expect(fundMe.fund(deployer.address, 0)).to.be.revertedWith(
      "FundAmountMustBeAboveZero"
 )

to

 await expect(fundMe.fund(deployer.address, 0)).to.be.revertedWithCustomError (
          "FundMe__FundAmountMustBeAboveZero"
     )

https://hardhat.org/hardhat-chai-matchers/docs/reference

1
  • Tried that, now I'm just getting a similar error: AssertionError: Expected transaction to be reverted with custom error 'FundMe__FundAmountMustBeAboveZero', but it didn't revert
    – l1nkm4rine
    Commented Sep 5, 2022 at 19:16
0

Had the same issue with ethereum-waffle 3.4.4
Update the package version to a recent one.
In my case it worked well with 4.0.7

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.