Skip to content

Commit

Permalink
Adds an assertion assertEOSErrorIncludesMessage that will assert th…
Browse files Browse the repository at this point in the history
…e error message for an expected `eosio_assert_message_exception` includes the given description.
  • Loading branch information
dallasjohnson committed Oct 11, 2019
1 parent dc1fe9a commit 382464a
Showing 1 changed file with 36 additions and 0 deletions.
36 changes: 36 additions & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,42 @@ export const assertEOSError = async (
assert.fail(`Expected ${description} but operation completed successfully.`);
};

/**
* Asserts EOS throws an error and validates the error output name matches the
* expected 'eosio_assert_message_exception' and the error message includes `description`
* @author Dallas Johnson <github.com/dallasjohnson>
* @param operation Operation promise
* @param description Output message description
*/
export const assertEOSErrorIncludesMessage = async (operation: Promise<any>, message: string) => {
const eosErrorName = 'eosio_assert_message_exception';
// Execute operation and handle exceptions
try {
await operation;
} catch (error) {
let errorMessage = error.json.error.details[0].message;
if (error.json && error.json.error && error.json.error.name && errorMessage) {
// Compare error and fail if the error doesn't match the expected
assert(
error.json.error.name === eosErrorName,
`Expected ${eosErrorName}, got ${error.json.error.name} instead.`
);
assert(
errorMessage.includes(message),
`Expected to include ${message}, got ${errorMessage} instead.`
);
return;
} else {
// Fail if error not thrown by EOS
assert.fail(
`Expected EOS error ${eosErrorName}, but got ${JSON.stringify(error, null, 4)} instead.`
);
}
}
// Fail if no exception thrown
assert.fail(`Expected ${eosErrorName} but operation completed successfully.`);
};

/**
* Asserts operation throws an `eosio_assert_message_exception` error
* @author Kevin Brown <github.com/thekevinbrown>
Expand Down

0 comments on commit 382464a

Please sign in to comment.