Skip to content
This repository has been archived by the owner on Jan 4, 2024. It is now read-only.

Commit

Permalink
Add better error handling if invalid tx result is passed in
Browse files Browse the repository at this point in the history
  • Loading branch information
rkalis committed May 29, 2019
1 parent 4c54074 commit 512df9e
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 1 deletion.
16 changes: 16 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,17 @@ const isEqual = require('lodash.isequal');

/* global web3 */

class InvalidTxResultError extends Error {}

const validateResult = (result) => {
if (!result.logs) {
throw new InvalidTxResultError(
'First argument is not a transaction result. Did you accidentally pass a contract instance or transaction receipt?\n'
+ 'If that is the case, check out truffleAssert.createTransactionResult in the documentation.',
);
}
};

/* Creates a new assertion message, containing the passedAssertionMessage and
* the defaultAssertion message when passedAssertionMessage exists, otherwise
* just the default.
Expand Down Expand Up @@ -57,6 +68,8 @@ const getPrettyEmittedEventsString = (result, indentationSize) => {
};

const assertEventEmittedFromTxResult = (result, eventType, filter, message) => {
validateResult(result);

/* Filter correct event types */
const events = result.logs.filter(entry => entry.event === eventType);

Expand All @@ -76,6 +89,8 @@ const assertEventEmittedFromTxResult = (result, eventType, filter, message) => {
};

const assertEventNotEmittedFromTxResult = (result, eventType, filter, message) => {
validateResult(result);

/* Filter correct event types */
const events = result.logs.filter(entry => entry.event === eventType);

Expand Down Expand Up @@ -194,4 +209,5 @@ module.exports = {
fails(asyncFn, ErrorType.REVERT, reason, message)
),
ErrorType,
InvalidTxResultError,
};
14 changes: 13 additions & 1 deletion test/events.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,17 @@ describe('eventEmitted', () => {
});
});

it('should pass when event is emitted with correct paritial arguments (number) given as an objec', () => {
it('should pass when event is emitted with correct paritial arguments (number) given as an object', () => {
truffleAssert.eventEmitted(truffleV4winResult, 'Play', {
betNumber: '0',
});
});

it('should throw an error when contract instance is passed instead of tx result', () => {
assert.throws(() => (
truffleAssert.eventEmitted({}, 'Play', ev => ev.betNumber === ev.winningNumber)
), truffleAssert.InvalidTxResultError);
});
});

describe('eventNotEmitted', () => {
Expand Down Expand Up @@ -108,6 +114,12 @@ describe('eventNotEmitted', () => {
betNumber: '2',
});
});

it('should throw an error when contract instance is passed instead of tx result', () => {
assert.throws(() => (
truffleAssert.eventNotEmitted({}, 'Play')
), truffleAssert.InvalidTxResultError);
});
});

// TODO: Truffle v5 -> don't print { 0: xxx, 1: xxx }
Expand Down

0 comments on commit 512df9e

Please sign in to comment.