Skip to content

Support custom error selectors in revertedWithCustomError Chai matcher #6484

Open
@ItsNickBarry

Description

@ItsNickBarry

Describe the feature

What is the reasoning behind requiring an interface to be passed to revertedWithCustomError? The error name string itself should be enough to run the assertion.

In my case, the contract I'm testing takes a custom error selector as a parameter and reverts with that error using assembly. The errors do not exist in the contract ABI, so I have to pass in a fake interface to the matcher.

const customError = 'TestError';

const placeholder = {
  interface: new ethers.Interface([`error ${ customError }()`]),
};

const selector = placeholder.interface.getError(customError)?.selector!;

await expect(
  contract.revertWithCustomErrorSelector(selector),
).to.be.revertedWithCustomError(placeholder, customError);

If this process could be encapsulated within the matcher code, the matcher could be used like this:

await expect(...).to.be.revertedWithCustomError(customError);
// or
await expect(...).to.be.revertedWithCustomError(selector);

Maybe the withArgs extension needs the full interface to process its arguments. In that case, maybe require the full error signature?

await expect(...).to.be.revertedWithCustomError('TestError(uint256)`);

Search terms

revertedWithCustomError

Similar to #5818, but more specifically related to errors that do not exist on the contract being tested.

Metadata

Metadata

Labels

status:readyThis issue is ready to be worked on

Type

No type

Projects

Status

Backlog

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions