Skip to content

Instantly share code, notes, and snippets.

@raineorshine
Last active March 16, 2019 18:37
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save raineorshine/a179b3eea3466fb1db5d8213e047903a to your computer and use it in GitHub Desktop.
Save raineorshine/a179b3eea3466fb1db5d8213e047903a to your computer and use it in GitHub Desktop.
Asserting solidity throws in truffle tests.
it('should throw an error', cb => {
const result = aMethodThatRejects()
result.then(x => { cb('Expected error. Instead got ' + x) }))
result.catch(() => cb())
})
@raineorshine
Copy link
Author

In newer versions of node, the originally suggested approach will result in unhandled rejections. They can be suppressed, but a cleaner approach is one of the following:

Promise:

return aMethodThatRejects().then(
  () => Promise.reject(new Error('Expected method to reject.')),
  err => assert.instanceOf(err, Error)
)

With Chai:

aMethodThatRejects().should.eventually.be.rejectedWith('ERROR');

Reference: https://gist.github.com/haroldtreen/5f1055eee5fcf01da3e0e15b8ec86bf6

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment