@beamnetwork/chai-beam
Advanced tools
Comparing version 0.3.3 to 0.3.4
16
index.js
@@ -26,2 +26,17 @@ /* eslint-env mocha */ | ||
} | ||
/** | ||
* Asserts that the promise will throw a revert error when executing and that the thrown message | ||
* is the one we expect | ||
*/ | ||
async function assertRevertMessage(promise, revertMessage) { | ||
try { | ||
await promise | ||
expect.fail(new Error('The promise did not revert')) | ||
} catch (error) { | ||
let msg = error.message || '' | ||
msg = msg.toString() | ||
assert(msg.includes('VM Exception while processing transaction: revert', `Expected revert, Error: ${msg}`)) | ||
assert(msg.includes(revertMessage), `Expected revert to contain message: ${revertMessage} \n Actual Message: ${msg}`) | ||
} | ||
} | ||
@@ -115,2 +130,3 @@ /** | ||
assertRevert, | ||
assertRevertMessage, | ||
assertJump, | ||
@@ -117,0 +133,0 @@ ignoreRevert, |
{ | ||
"name": "@beamnetwork/chai-beam", | ||
"version": "0.3.3", | ||
"version": "0.3.4", | ||
"description": "Beam plugins for chai", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -8,3 +8,3 @@ /* eslint-env mocha */ | ||
// eslint-disable-next-line | ||
assertRevert, assertJump, ignoreRevert, ignoreJump, expectEvent, expectNotEvent | ||
assertRevert, assertRevertMessage, assertJump, ignoreRevert, ignoreJump, expectEvent, expectNotEvent | ||
} = require('../index.js') | ||
@@ -38,2 +38,3 @@ | ||
const message = 'a seriously descriptive message' | ||
let threw = false | ||
try { | ||
@@ -43,6 +44,57 @@ await assertRevert(contract.normal(), message) | ||
assert(e.message.toString().includes(message), 'error did not contain the given message') | ||
threw = true | ||
} | ||
if (!threw) { | ||
expect.fail('assertRevert failed to throw on reverting call') | ||
} | ||
}) | ||
}) | ||
describe('assertRevertMessage', async () => { | ||
it('should fail when revert occurs but called with no revert message', async () => { | ||
let threw = false | ||
try { | ||
await assertRevertMessage(contract.revertWithMessage('revert AAA')) | ||
} catch (e) { | ||
assert(e.message.toString().includes('Expected revert to contain message: undefined'), 'revert did not contain the correct message') | ||
threw = true | ||
} | ||
if (!threw) { | ||
expect.fail('assertRevertMessage failed to throw on reverting call') | ||
} | ||
}) | ||
it('should fail when revert with wrong message thrown', async () => { | ||
let threw = false | ||
try { | ||
await assertRevertMessage(contract.revertWithMessage('revert AAA'), 'revert BBB') | ||
} catch (e) { | ||
assert(e.message.toString().includes('Expected revert to contain message'), 'revert did not contain the correct message') | ||
threw = true | ||
} | ||
if (!threw) { | ||
expect.fail('assertRevertMessage failed to throw on reverting call') | ||
} | ||
}) | ||
it('should fail when revert not thrown', async () => { | ||
let threw = false | ||
try { | ||
await assertRevertMessage(contract.normal(), 'some message') | ||
} catch (e) { | ||
threw = true | ||
} | ||
if (!threw) { | ||
expect.fail('assertRevertMessage failed to throw on non-reverting call') | ||
} | ||
}) | ||
it('should pass when revert with the right message is thrown', async () => { | ||
const coolRevertMessage = 'satishi too low' | ||
await assertRevertMessage(contract.revertWithMessage(coolRevertMessage), coolRevertMessage) | ||
}) | ||
}) | ||
describe('assertJump', async () => { | ||
@@ -67,2 +119,3 @@ it('should pass when jump thrown', async () => { | ||
const message = 'a seriously descriptive message' | ||
let threw = false | ||
try { | ||
@@ -72,3 +125,8 @@ await assertJump(contract.normal(), message) | ||
assert(e.message.toString().includes(message), 'error did not contain the given message') | ||
threw = true | ||
} | ||
if (!threw) { | ||
expect.fail('assertJump failed to throw on jump call') | ||
} | ||
}) | ||
@@ -75,0 +133,0 @@ }) |
Sorry, the diff of this file is not supported yet
20566
427