Skip to content

Commit

Permalink
improve expectRevert output (#59)
Browse files Browse the repository at this point in the history
* improve expectRevert output

* use AssertionError instead assert.fail, add message confirmation to assertFailure

* make expectRevert show nicer error message

* add basic test for expectRevert error message

* lint
  • Loading branch information
dotrungkien authored and frangio committed Jul 26, 2019
1 parent a7c81ef commit 2522e0b
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 5 deletions.
6 changes: 4 additions & 2 deletions src/expectRevert.js
@@ -1,4 +1,5 @@
const { web3 } = require('./setup');
const { expect } = require('chai');

const colors = require('ansi-colors');
const semver = require('semver');
Expand All @@ -8,12 +9,13 @@ async function expectException (promise, expectedError) {
await promise;
} catch (error) {
if (error.message.indexOf(expectedError) === -1) {
throw Error(`Wrong failure type, expected '${expectedError}' and got '${error.message}'`);
const actualError = error.message.replace('Returned error: VM Exception while processing transaction: ', '');
expect.fail(actualError, expectedError, 'Wrong kind of exception received');
}
return;
}

throw Error('Expected failure not received');
expect.fail('Expected an exception but none was received');
}

const expectRevert = async function (promise, expectedError) {
Expand Down
2 changes: 1 addition & 1 deletion test/helpers/assertFailure.js
Expand Up @@ -4,7 +4,7 @@ async function assertFailure (promise) {
try {
await promise;
} catch (error) {
return;
return error;
}
expect.fail();
}
Expand Down
12 changes: 10 additions & 2 deletions test/src/expectRevert.test.js
@@ -1,3 +1,5 @@
const { expect } = require('chai');

const assertFailure = require('../helpers/assertFailure');
const expectRevert = require('../../src/expectRevert');

Expand All @@ -10,7 +12,10 @@ describe('expectRevert', function () {

describe('expectRevert', function () {
it('rejects if no revert occurs', async function () {
await assertFailure(expectRevert(this.reverter.dontRevert()));
const assertion =
await assertFailure(expectRevert(this.reverter.dontRevert(), 'reason'));

expect(assertion.message).to.equal('Expected an exception but none was received');
});

it('rejects a revert', async function () {
Expand All @@ -22,7 +27,10 @@ describe('expectRevert', function () {
});

it('rejects a revert with incorrect expected reason', async function () {
await assertFailure(expectRevert(this.reverter.revertFromRevertWithReason(), 'Wrong reason'));
const assertion =
await assertFailure(expectRevert(this.reverter.revertFromRevertWithReason(), 'Wrong reason'));

expect(assertion.message).to.equal('Wrong kind of exception received');
});

it('accepts a revert with correct expected reason', async function () {
Expand Down

0 comments on commit 2522e0b

Please sign in to comment.