Skip to content

Commit

Permalink
feat: enable RegExp test for revertedWith matcher (#633)
Browse files Browse the repository at this point in the history
  • Loading branch information
glinda93 committed Mar 28, 2022
1 parent 4cd1574 commit c4871e6
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 7 deletions.
7 changes: 7 additions & 0 deletions docs/source/matchers.rst
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,13 @@ Testing if transaction was reverted with certain message:
await expect(token.transfer(walletTo.address, 1007))
.to.be.revertedWith('Insufficient funds');
You can also test if revert message matches to a regular expression:

.. code-block:: ts
await expect(token.checkRole('ADMIN'))
.to.be.revertedWith(/AccessControl: account .* is missing role .*/);
Change ether balance
--------------------
Testing whether the transaction changes the balance of the account:
Expand Down
14 changes: 9 additions & 5 deletions waffle-chai/src/matchers/revertedWith.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {decodeRevertString} from '@ethereum-waffle/provider';

export function supportRevertedWith(Assertion: Chai.AssertionStatic) {
Assertion.addMethod('revertedWith', function (this: any, revertReason: string) {
Assertion.addMethod('revertedWith', function (this: any, revertReason: string | RegExp) {
const promise = this._obj;

const onSuccess = (value: any) => {
Expand All @@ -22,11 +22,14 @@ export function supportRevertedWith(Assertion: Chai.AssertionStatic) {
const onError = (error: any) => {
const revertString = error?.receipt?.revertString ?? decodeRevertString(error);
if (revertString !== undefined) {
const isReverted = revertReason instanceof RegExp
? revertReason.test(revertString)
: revertString === revertReason;
this.assert(
revertString === revertReason,
isReverted,
`Expected transaction to be reverted with "${revertReason}", but other reason was found: "${revertString}"`,
`Expected transaction NOT to be reverted with "${revertReason}"`,
`Transaction reverted with "${revertReason}".`,
`Transaction reverted with "${revertReason}"`,
error
);
return error;
Expand All @@ -45,8 +48,9 @@ export function supportRevertedWith(Assertion: Chai.AssertionStatic) {
const reasonsList = error.results && Object.values(error.results).map((o: any) => o.reason);
const message = (error instanceof Object && 'message' in error) ? error.message : JSON.stringify(error);
const isReverted = reasonsList
? reasonsList.some((r: string) => r === revertReason)
: message.includes('revert') && message.includes(revertReason);
? reasonsList.some((r: string) => revertReason instanceof RegExp ? revertReason.test(r) : r === revertReason)
: message.includes('revert') &&
(revertReason instanceof RegExp ? revertReason.test(message) : message.includes(revertReason));
const isThrown = message.search('invalid opcode') >= 0 && revertReason === '';

this.assert(
Expand Down
11 changes: 11 additions & 0 deletions waffle-chai/test/matchers/reverted.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -140,12 +140,23 @@ describe('INTEGRATION: Matchers: revertedWith', () => {
.to.be.revertedWith('Revert cause (with complex reason)');
});

it('Revert: success when message matches to the pattern', async () => {
await expect(matchers.doRevertWithComplexReason())
.to.be.revertedWith(/complex reason/);
});

it('Revert: fail when different message was thrown', async () => {
await expect(
expect(matchers.doRevert()).to.be.revertedWith('Different message')
).to.be.eventually.rejected;
});

it('Revert: fail when message does not match to the pattern', async () => {
await expect(
expect(matchers.doRevert()).to.be.revertedWith(/Different message/)
).to.be.eventually.rejected;
});

it('Revert: fail no exception', async () => {
await expect(
expect(matchers.doNothing()).to.be.revertedWith('')
Expand Down
5 changes: 3 additions & 2 deletions waffle-jest/src/matchers/toBeRevertedWith.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import {decodeRevertString} from '@ethereum-waffle/provider';

export async function toBeRevertedWith(
promise: Promise<any>,
revertReason: string
revertReason: string | RegExp
) {
try {
const tx = await promise;
Expand All @@ -23,7 +23,8 @@ export async function toBeRevertedWith(

const message = error instanceof Object && 'message' in error ? (error as any).message : JSON.stringify(error);

const isReverted = message.search('revert') >= 0 && message.search(revertReason) >= 0;
const isReverted = message.search('revert') >= 0 &&
(revertReason instanceof RegExp ? revertReason.test(message) : message.search(revertReason) >= 0);
const isThrown = message.search('invalid opcode') >= 0 && revertReason === '';
const isError = message.search('code=') >= 0;

Expand Down

0 comments on commit c4871e6

Please sign in to comment.