Skip to content
This repository has been archived by the owner on Feb 26, 2024. It is now read-only.

truffle test fails to decode custom errors #5753

Open
advra opened this issue Dec 3, 2022 · 5 comments
Open

truffle test fails to decode custom errors #5753

advra opened this issue Dec 3, 2022 · 5 comments

Comments

@advra
Copy link

advra commented Dec 3, 2022

Issue

image

I have a test which passes using string messages. Ive changed the contract to use custom error messages and get the error "VM Exception revert -- Reason given: Custom error (could not decode)".

Steps to Reproduce

Compile contract
Run test
Case should be able to compare error message

Here is a portion of the test_contract.js

it("should validate for 721", async function () {
    // code for account[0] to mint nft
    await expectRevert(
        registry.stakeNFT(nft721Contract, 1, {from: accounts[2], value: listingFee}),
        "Not owner of nft 721"
    );
}

The solidity code which will pass using error strings:
function someFunction(address nftContract, unint256 tokenId) {
// some logic here
require(IERC721(nftContract).ownerOf(tokenId) == msg.sender, "Not owner of nft 721");
// some more logic
}

The new solidity code which does not pass because it cannot decode the custom error message

// newcontract.sol
error NotOwnerOfNFT();

function someFunction(address nftContract, unint256 tokenId) {
    // some logic here
    if(IERC721(nftContract).ownerOf(tokenId) != msg.sender) revert NotOwnerOfNFT();

    // some more logic
}

When running newcontract.sol against the test it will provide the exception error. If I recall correctly, truffle now supports reading custom error codes. But it does not?

Expected Behavior

Custom error message should be read and returned in the test as a pass or fail. Instead it says cannot decode.

Actual Results

What actually happened. Please give examples and support it with screenshots, copied output or error messages.

Environment

  • Operating System: OSX
  • Ethereum client: local test
  • Truffle version (truffle version): 5.6.7
  • node version (node --version): 14.18.1
  • npm version (npm --version): 8.13.2
@advra
Copy link
Author

advra commented Dec 3, 2022

I see this that I may try out: OpenZeppelin/openzeppelin-test-helpers#180

Nevermind. This wont help. My issue is the custom message is not decoded Before hitting the asserts within the javascript test.

@cds-amal
Copy link
Member

cds-amal commented Dec 3, 2022

Thanks for opening this issue @advra, we have #4123 in our backlog, and I'll make a case to bump its priority during our next ticket grooming meeting.

@gnidan gnidan changed the title VM Exception revert -- Reason given: Custom error (could not decode) in Javascript Tests truffle test fails to decode custom errors Dec 8, 2022
@gnidan
Copy link
Contributor

gnidan commented Dec 8, 2022

We'll leave this open as an issue related to supporting custom error decoding directly in truffle test, and then leave #4123 for handling these in @truffle/contract more specifically.

Thanks again!

@TomiOhl
Copy link

TomiOhl commented Dec 27, 2022

Note to truffle devs: TomiOhl/custom-error-test-helper does basically what you want to do with ganache (doesn't work with hardhat network etc, but for that you could look at their official chai matchers).
I appreciate you're working on this, this is really the missing piece when testing with truffle.

@seaona
Copy link

seaona commented Feb 10, 2023

Looking forward this too #4123 💥

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

5 participants