Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ExpectRevert is unable to correctly identify revert messages #128

Open
EuanHay opened this issue Aug 6, 2020 · 2 comments
Open

ExpectRevert is unable to correctly identify revert messages #128

EuanHay opened this issue Aug 6, 2020 · 2 comments

Comments

@EuanHay
Copy link

EuanHay commented Aug 6, 2020

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?

@abcoathup
Copy link
Contributor

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

@Silur
Copy link

Silur commented May 26, 2022

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

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

No branches or pull requests

3 participants