expectThrow.js 1.1 KB

12345678910111213141516171819202122232425262728293031323334
  1. async function expectThrow (promise, message) {
  2. try {
  3. await promise;
  4. } catch (error) {
  5. // Message is an optional parameter here
  6. if (message) {
  7. assert(
  8. error.message.search(message) >= 0,
  9. 'Expected \'' + message + '\', got \'' + error + '\' instead',
  10. );
  11. return;
  12. } else {
  13. // TODO: Check jump destination to destinguish between a throw
  14. // and an actual invalid jump.
  15. const invalidOpcode = error.message.search('invalid opcode') >= 0;
  16. // TODO: When we contract A calls contract B, and B throws, instead
  17. // of an 'invalid jump', we get an 'out of gas' error. How do
  18. // we distinguish this from an actual out of gas event? (The
  19. // ganache log actually show an 'invalid jump' event.)
  20. const outOfGas = error.message.search('out of gas') >= 0;
  21. const revert = error.message.search('revert') >= 0;
  22. assert(
  23. invalidOpcode || outOfGas || revert,
  24. 'Expected throw, got \'' + error + '\' instead',
  25. );
  26. return;
  27. }
  28. }
  29. assert.fail('Expected throw not received');
  30. }
  31. module.exports = {
  32. expectThrow,
  33. };