Skip to content

expectRevert should be able to check for custom errorsΒ #180

@Rav3nPL

Description

@Rav3nPL

Since 0.8.4 we have use of new and cheap custom errors https://blog.soliditylang.org/2021/04/21/custom-errors/

Using them in contract code, but in test I need to use expectRevert.uspecified to handle them in any way.

Reproduce:

pragma solidity ^0.8.4;

contract Test {
    uint256 public num;
    error CustomErrorMessage();

    function willThrowOnZero(uint256 param) external {
        if (param == 0) revert CustomErrorMessage();
        num = param;
    }
}
const { contract } = require('@openzeppelin/test-environment');

const { expectRevert } = require('@openzeppelin/test-helpers');

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

describe('Custom test', function () {
    let test;
    before(async function () {
        test = await Test.new();
    })
    describe('Test check', function () {
        it('throws on zero', async function () {
            await expectRevert.unspecified(test.willThrowOnZero('0'))
        })
        it('passes on non-zero', async function () {
            await test.willThrowOnZero('1');
        })
        it('can not catch error name', async function () {
            await expectRevert(test.willThrowOnZero('0'), "CustomErrorMessage")
        })
    })
})

Test running throws:
image

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions