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

improve expectRevert output #59

Merged
merged 5 commits into from Jul 26, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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