From d431f1418d21d77f2591c7987dfc2a99c7396744 Mon Sep 17 00:00:00 2001 From: Do Trung Kien Date: Wed, 26 Jun 2019 18:23:37 +0700 Subject: [PATCH 1/5] improve expectRevert output --- src/expectRevert.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/expectRevert.js b/src/expectRevert.js index b1bd9ce..d33d0aa 100644 --- a/src/expectRevert.js +++ b/src/expectRevert.js @@ -2,13 +2,15 @@ const { web3 } = require('./setup'); const colors = require('ansi-colors'); const semver = require('semver'); +const assert = require('assert'); async function expectException (promise, expectedError) { try { 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: ', ''); + assert.fail(`Wrong failure type, expected '${expectedError}' and got '${actualError}'`); } return; } From 79661736cf87f12eb0fa4b687568e75d7a706360 Mon Sep 17 00:00:00 2001 From: Do Trung Kien Date: Fri, 28 Jun 2019 13:45:41 +0700 Subject: [PATCH 2/5] use AssertionError instead assert.fail, add message confirmation to assertFailure --- src/expectRevert.js | 8 ++++++-- test/helpers/assertFailure.js | 4 ++++ 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/src/expectRevert.js b/src/expectRevert.js index d33d0aa..5c7d33f 100644 --- a/src/expectRevert.js +++ b/src/expectRevert.js @@ -2,7 +2,7 @@ const { web3 } = require('./setup'); const colors = require('ansi-colors'); const semver = require('semver'); -const assert = require('assert'); +const AssertionError = require('assert').AssertionError; async function expectException (promise, expectedError) { try { @@ -10,7 +10,11 @@ async function expectException (promise, expectedError) { } catch (error) { if (error.message.indexOf(expectedError) === -1) { const actualError = error.message.replace('Returned error: VM Exception while processing transaction: ', ''); - assert.fail(`Wrong failure type, expected '${expectedError}' and got '${actualError}'`); + throw new AssertionError({ + message: 'Wrong failure type', + expected: expectedError, + actual: actualError, + }); } return; } diff --git a/test/helpers/assertFailure.js b/test/helpers/assertFailure.js index f5a63c4..b220954 100644 --- a/test/helpers/assertFailure.js +++ b/test/helpers/assertFailure.js @@ -1,9 +1,13 @@ const { expect } = require('chai'); +const AssertionError = require('assert').AssertionError; async function assertFailure (promise) { try { await promise; } catch (error) { + if (error instanceof AssertionError) { + expect(error.message).to.equal('Wrong failure type'); + } return; } expect.fail(); From c4077df8024b613f1693e1dfcdcc8b6a020023b3 Mon Sep 17 00:00:00 2001 From: Francisco Giordano Date: Fri, 26 Jul 2019 19:12:37 -0300 Subject: [PATCH 3/5] make expectRevert show nicer error message --- src/expectRevert.js | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/src/expectRevert.js b/src/expectRevert.js index 5c7d33f..d658435 100644 --- a/src/expectRevert.js +++ b/src/expectRevert.js @@ -1,4 +1,5 @@ const { web3 } = require('./setup'); +const { expect } = require('chai'); const colors = require('ansi-colors'); const semver = require('semver'); @@ -10,16 +11,12 @@ async function expectException (promise, expectedError) { } catch (error) { if (error.message.indexOf(expectedError) === -1) { const actualError = error.message.replace('Returned error: VM Exception while processing transaction: ', ''); - throw new AssertionError({ - message: 'Wrong failure type', - expected: expectedError, - actual: actualError, - }); + 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) { From be6647aab06dbc5b64ebc0c7fa08d86b825d263e Mon Sep 17 00:00:00 2001 From: Francisco Giordano Date: Fri, 26 Jul 2019 19:13:05 -0300 Subject: [PATCH 4/5] add basic test for expectRevert error message --- test/helpers/assertFailure.js | 5 +---- test/src/expectRevert.test.js | 12 ++++++++++-- 2 files changed, 11 insertions(+), 6 deletions(-) diff --git a/test/helpers/assertFailure.js b/test/helpers/assertFailure.js index b220954..1b6201f 100644 --- a/test/helpers/assertFailure.js +++ b/test/helpers/assertFailure.js @@ -5,10 +5,7 @@ async function assertFailure (promise) { try { await promise; } catch (error) { - if (error instanceof AssertionError) { - expect(error.message).to.equal('Wrong failure type'); - } - return; + return error; } expect.fail(); } diff --git a/test/src/expectRevert.test.js b/test/src/expectRevert.test.js index 92f39b7..992d406 100644 --- a/test/src/expectRevert.test.js +++ b/test/src/expectRevert.test.js @@ -1,3 +1,5 @@ +const { expect } = require('chai'); + const assertFailure = require('../helpers/assertFailure'); const expectRevert = require('../../src/expectRevert'); @@ -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 () { @@ -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 () { From a10e74008ce691d0df794d683a230ed77b0a00d7 Mon Sep 17 00:00:00 2001 From: Francisco Giordano Date: Fri, 26 Jul 2019 19:17:13 -0300 Subject: [PATCH 5/5] lint --- src/expectRevert.js | 1 - test/helpers/assertFailure.js | 1 - 2 files changed, 2 deletions(-) diff --git a/src/expectRevert.js b/src/expectRevert.js index d658435..e7ebc3f 100644 --- a/src/expectRevert.js +++ b/src/expectRevert.js @@ -3,7 +3,6 @@ const { expect } = require('chai'); const colors = require('ansi-colors'); const semver = require('semver'); -const AssertionError = require('assert').AssertionError; async function expectException (promise, expectedError) { try { diff --git a/test/helpers/assertFailure.js b/test/helpers/assertFailure.js index 1b6201f..3c33ee7 100644 --- a/test/helpers/assertFailure.js +++ b/test/helpers/assertFailure.js @@ -1,5 +1,4 @@ const { expect } = require('chai'); -const AssertionError = require('assert').AssertionError; async function assertFailure (promise) { try {