0

For example, testing for a transaction that reverted or an out of gas error etc.

What is the best practice for writing such tests? Specifically, I am using truffle test framework (mocha/chai).

Dylan Kerler
  • 416
  • 1
  • 3
  • 16
  • You might find this answer useful (disclosure: I wrote it). – goodvibration Sep 07 '20 at 21:35
  • Actually I did come across that. However I was hoping that something more robust existed in the form of a library that has been vetted and tested by a significant amount of the community. Copying and pasting code like this seems a bit risky even if only for a tiny nuanced error; When money is at stake then it has to be near-perfect. I guess I'll use this if nothing else exists though. – Dylan Kerler Sep 07 '20 at 21:39
  • 1
    Waffle has Chai matchers for events, reverts, etc. You can use it in conjunction with Truflfe. – Morten Sep 07 '20 at 21:44
  • @Morten This is great thanks! Why not post it as an answer? – Dylan Kerler Sep 07 '20 at 21:48
  • Risky? That argument is relevant when dealing with on-chain code (Solidity), not off-chain code. – goodvibration Sep 08 '20 at 06:27
  • @goodvibration If there is an error in the testing library then it can make you think the on-chain code is working fine even though it might be failing. – Dylan Kerler Sep 08 '20 at 07:53
  • @DylanKerler: Of course, but being that the testing library is simple and straitfoward (and written in Javascript, AND probably have been tested by the community here for more than two years), I would imagine that it is pretty safe by now. BTW, you can also use const { expectRevert } = require('@openzeppelin/test-helpers');. Although it is more recent than my answer, it has probably been verified by a rather large community (being that it is a part of OZ package). – goodvibration Sep 08 '20 at 09:33

1 Answers1

2

You can use Waffle's built-in Chai matchers to test for events, reverts, etc. It works with Truffle and Mocha out of the box, and supports any network provider with history support (like Ganache, BuidlerVM). You can find documentation here:

There are also experimental Jest matchers if you're looking to use Jest instead of Mocha and Chai.

Morten
  • 6,017
  • 2
  • 12
  • 26