smartcontractkit / full-blockchain-solidity-course-js

Learn Blockchain, Solidity, and Full Stack Web3 Development with Javascript
12.55k stars 3k forks source link

Lesson 9 : expect().to.be.revertedWith() isn't taking in my custom error #2034

Closed br0wnD3v closed 2 years ago

br0wnD3v commented 2 years ago

Hello there, In lesson 9's testing part, I want the transaction to fail with the custom error error Lottery__InsufficientFunds() and it IS failing with that custom error BUT when I do

describe("Enter Raffle.", async () => {
        it("Should revert if enough ETH not sent to enter the lottery.", async () => {
          await expect(lotteryContract.enterLottery()).to.be.revertedWith(
            "Lottery__InsufficientFunds"
          );
        });
});
$yarn hardhat test

I get an Assertion Error : AssertionError: Expected transaction to be reverted with reason 'Lottery__InsufficientFunds', but it reverted with a custom error

My Solidity code block for enterLottery() :

function enterLottery() public payable {
      if(s_state != LOTTERY_STATE.OPEN){
        revert Lottery__Closed();
      }
      if(msg.value != i_entranceFee){
        revert Lottery__InsufficientFunds();
      }
      s_participants.push(payable(msg.sender));
      emit LotteryEnter(msg.sender);  
    }

I defined the error under contract Lottery is VRFConsumerBaseV2, KeeperCompatibleInterface {} as
error Lottery__InsufficientFunds();

Any help is appreciated :) Stuck on it for quite some time.

NOTE : If I edit the expect() to

await expect(lotteryContract.enterLottery()).to.be.reverted;

It works like it intended to be and the test passes.

br0wnD3v commented 2 years ago

revertWithCustomError() did help me so I'll probably close the issue now.

await expect(
            lotteryContract.enterLottery()
          ).to.be.revertedWithCustomError(
            lotteryContract,
            "Lottery__InsufficientFunds"
          );