OpenZeppelin / openzeppelin-test-helpers

Assertion library for Ethereum smart contract testing

Home Page:https://docs.openzeppelin.com/test-helpers

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

ExpectRevert is unable to correctly identify revert messages

EuanHay opened this issue · comments

In the expectRevert function, the error message I provided is identical to the error message expected by the expectRevert function. expectRevert is unable to see the error given is in fact correct.

Error: Returned error: VM Exception while processing transaction: revert Item does not exist, cannot update Item -- Reason given: Item does not exist, cannot update Item.

Any ideas on how to resolve this?

Hi @EuanHay! I’m sorry that you had this issue. We would need more information so that we can reproduce it.

Can you provide how you are testing (Truffle or OpenZeppelin Test Environment), and a snippet of your test? Any other details that might help us reproduce it would be appreciated!

I only get the error using OpenZeppelin Test Environment when I don't try to catch the revert using expectRevert, see my test below.

For usage, please see the API documentation: https://docs.openzeppelin.com/test-helpers/0.5/api#expect-revert

MyContract.sol

// contracts/MyContract.sol
// SPDX-License-Identifier: MIT
pragma solidity ^0.6.0;

contract MyContract {
    uint256 private value;

    function doStuff() public {
        require(false, "Item does not exist, cannot update Item");
    }
}

MyContract.test.js

const { accounts, contract } = require('@openzeppelin/test-environment');
const { expect } = require('chai');

// Import utilities from Test Helpers
const { BN, expectEvent, expectRevert } = require('@openzeppelin/test-helpers');

const MyContract = contract.fromArtifact('MyContract');

describe('MyContract', function () {
  const [ owner, other ] = accounts;

  beforeEach(async function () {
    this.contract = await MyContract.new({ from: owner });
  });

  it('fails to catch revert', async function () {
    // Test a transaction reverts
    await this.contract.doStuff({ from: other })
  });

  it('expect revert', async function () {
    // Test a transaction reverts
    await expectRevert(
      this.contract.doStuff({ from: other }),
      'Item does not exist, cannot update Item'
    );
  });
});

Testing

$ npm run test

> box@1.0.0 test /home/abcoathup/projects/forum/box
> mocha --exit --recursive test



  MyContract
    1) fails to catch revert
    ✓ expect revert (85ms)


  1 passing (998ms)
  1 failing

  1) MyContract
       fails to catch revert:
     Error: Returned error: VM Exception while processing transaction: revert Item does not exist, cannot update Item -- Reason given: Item does not exist, cannot update Item.
      at Context.<anonymous> (test/MyContract.test.js:18:25)
      at process._tickCallback (internal/process/next_tick.js:68:7)



npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! box@1.0.0 test: `mocha --exit --recursive test`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the box@1.0.0 test script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

npm ERR! A complete log of this run can be found in:
npm ERR!     /home/abcoathup/.npm/_logs/2020-08-07T01_55_35_090Z-debug.log
commented

the example you provided indeed results in expectRevert not indentifying the revert message and fail. Tested with solc 0.8.11+commit.d7f03943.Emscripten.clang, node v16.14.2 on an x86_64 arch linux environment