Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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

This comment has been minimized.

Copy link
Owner Author

raineorshine commented Mar 16, 2019

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
You can’t perform that action at this time.