rkalis / truffle-assertions

🛠 Assertions and utilities for testing Ethereum smart contracts with Truffle unit tests
https://kalis.me/check-events-solidity-smart-contract-test-truffle/
MIT License
154 stars 20 forks source link

Does not work well with solidity-coverage #31

Closed PaulRBerg closed 5 years ago

PaulRBerg commented 5 years ago

Describe the bug

solidity-coverage is known to polyfill the contracts with loads of custom events used to measure test coverage. It seems that something done by them makes truffle-assertions not see the event.

Here's the error output:

{ AssertionError: Event filter for PayInterest returned no results
  Events emitted in tx 0xd88f11f66a6e2dde445150891914b4b6b4d5200a719acc0585e9d4626b0fbcb8:
  ----------------------------------------------------------------------------------------
  WithdrawFromStream(0: 1, 1: 0x6Ecc55fF73ca55367c29298F23813efa54cbAc94, 2: 499725001, __length__: 3, streamId: 1, recipient: 0x6Ecc55fF73ca55367c29298F23813efa54cbAc94, amount: 499725001)
  WithdrawFromStream(0: 1, 1: 0x6Ecc55fF73ca55367c29298F23813efa54cbAc94, 2: 499725001, __length__: 3, streamId: 1, recipient: 0x6Ecc55fF73ca55367c29298F23813efa54cbAc94, amount: 499725001)
  PayInterest(0: 1, 1: 225000, 2: 225000, 3: 49999, __length__: 4, streamId: 1, senderInterest: 225000, recipientInterest: 225000, sablierInterest: 49999)
  PayInterest(0: 1, 1: 225000, 2: 225000, 3: 49999, __length__: 4, streamId: 1, senderInterest: 225000, recipientInterest: 225000, sablierInterest: 49999)

Yeah, it's weird, there are two PayInterest events logged but they are somehow neglected. I suspect that truffle-assertions expects the event to be the second after WithdrawFromStream? Not sure.

Example test code

The issue occurred in the sablier monorepo. Sorry, as of creating this issue, I have not yet pushed my local work on solidity-coverage. I'll come back here and make an update when I push to remote.

Here's how the test looks:

it("pays the interest to the sender of the stream", async function() {
  const balance = await this.cToken.balanceOf(sender);
  const result = await this.sablier.withdrawFromStream(streamId, amount, opts);

  try {
    truffleAssert.eventEmitted(result, "PayInterest", (event) => {
      console.log("event", require("util").inspect(event, false, null, true /* enable colors */));
    });
  } catch (err) {
    console.log({ err });
  }

  const senderInterest = result.logs[1].args.senderInterest;
  const newBalance = await this.cToken.balanceOf(sender);
  balance.should.be.bignumber.equal(newBalance.minus(senderInterest));
});

Expected behavior

The event should be filtered by truffle-assertions because it's definitely emitted.

Environment Information

Truffle version: 5.0.35 Web3 version: 1.2.1 truffle-assertions version: 0.8.2 ganache-cli version: 6.4.1 from https://github.com/frangio/ganache-cli (using this fork because OpenZeppelin uses it)

rkalis commented 5 years ago

eventEmitted takes a filter function that returns true if the event should be included in the filtered output or false if it shouldn't. In the test you provided, it never returns true so no events are included in the filtered output.

Does the bug still persist if you change the filter function to return a boolean?

PaulRBerg commented 5 years ago

Oh, I see! so eventEmitted in and of itself is the test assertion.

I confirm that by returning true, the error disappeared. Thanks!

rkalis commented 5 years ago

Exactly. Good to hear that it works for you now :)