expectThrow.js 970 B

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