openzeppelin-test-helpers icon indicating copy to clipboard operation
openzeppelin-test-helpers copied to clipboard

ExpectRevert is unable to correctly identify revert messages

Open EuanHay opened this issue 5 years ago • 2 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?

EuanHay avatar Aug 06 '20 15:08 EuanHay

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

> [email protected] 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! [email protected] test: `mocha --exit --recursive test`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the [email protected] 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

abcoathup avatar Aug 07 '20 02:08 abcoathup

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

Silur avatar May 26 '22 09:05 Silur