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

Adds an assertion assertEOSErrorIncludesMessage #97

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 63 additions & 6 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,16 +110,21 @@ export const assertRowCount = async (

/**
* Asserts EOS throws an error and validates the error output name matches the expected `eosErrorName`
* Also asserts that other aspects of the error as checked through an optional passed in function.
* @author Dallas Johnson <github.com/dallasjohnson>
* @author Kevin Brown <github.com/thekevinbrown>
* @author Mitch Pierias <github.com/MitchPierias>
* @param operation Operation promise
* @param eosErrorName Expected EOS error name
* @param description Output message description
* @param furtherErrorCheck function to run further assertions on a thrown error.
* @returns a Boolean of true if an error was thrown as is expected.
*/
export const assertEOSError = async (
const assertExpectedEOSError = async (
operation: Promise<any>,
eosErrorName: string,
description: string
) => {
furtherErrorCheck?: (err: any) => any
): Promise<boolean> => {
// Execute operation and handle exceptions
try {
await operation;
Expand All @@ -133,16 +138,68 @@ export const assertEOSError = async (
error.json.error.name === eosErrorName,
`Expected ${eosErrorName}, got ${error.json.error.name} instead.`
);
return;
if (furtherErrorCheck) {
furtherErrorCheck(error);
}
} else {
// Fail if error not thrown by EOS
assert.fail(
`Expected EOS error ${eosErrorName}, but got ${JSON.stringify(error, null, 4)} instead.`
);
}
return true;
}
return false;
};

/**
* Asserts EOS throws an error and validates the error output name matches the expected `eosErrorName`
* @author Kevin Brown <github.com/thekevinbrown>
* @param operation Operation promise
* @param eosErrorName Expected EOS error name
* @param description Output message description
*/
export const assertEOSError = async (
operation: Promise<any>,
eosErrorName: string,
description: string
) => {
if (assertExpectedEOSError(operation, eosErrorName)) {
// Execute operation and handle exceptions
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 message Output message expected to be included
*/
export const assertEOSErrorIncludesMessage = async (operation: Promise<any>, message: string) => {
const eosErrorName = 'eosio_assert_message_exception';
// Execute operation and handle exceptions
if (
!(await assertExpectedEOSError(operation, eosErrorName, error => {
let errorMessage = error.json.error.details[0].message;

if (!errorMessage) {
assert.fail(
`Expected EOS error ${eosErrorName}, but got ${JSON.stringify(error, null, 4)} instead.`
);
}
assert(
errorMessage.includes(message),
`Expected to include ${message}, got ${errorMessage} instead.`
);
}))
) {
// Fail if no exception thrown
assert.fail(
`Expected ${eosErrorName} with message to include ${message} but operation completed successfully.`
);
}
// Fail if no exception thrown
assert.fail(`Expected ${description} but operation completed successfully.`);
};

/**
Expand Down