3

Usually, using truffle I can check for events like this:

    let { logs } = await myContract.doSomethingImportant( xxx );

    // 1500 tokens are expected to get
    expectEvent.inLogs(logs, 'myImportantEvent', {
      arg1: 111,
      arg2: 'and so on'
    });

However, logs contains only the events that was fired directly inside of the contract function withdrawVestedTokensByTimestamp:

function doSomethingImportant(uint256 x) public returns(uint256) {
    // ... do something else here ...

    emit myImportantEvent(x, amount);

    return amount;
}

However, if in this function a token transfer will happen or another function is called, which would fire events itself, they don't appear in the logs.

Running tests with truffle test --show-events will list all of the expected events, but how can I catch them inside truffle test?

2 Answers 2

4

Use openzeppelin-test-helpers

Specifically use expectEvent.inTransaction

I created an example in the community forum for how to use: https://forum.zeppelin.solutions/t/how-to-check-for-events-that-was-dispatched-in-a-nested-operation/955/2

1
  • 1
    This is indeed an amazing library, but it comes with a caveat. It breaks compatibility with BigNumber.js, due to several imports that use BN.js Aug 6, 2019 at 18:20
1

The openzeppelin-test-helpers library that you are using can actually retrieve all events from a transaction. expectEvent.inLogs(logs, ...); actually scans logs for each event that was emitted during the transaction and will look for the event name (and values) that you specified.

In your case, you simply need to add another expectEvent.inLogs(logs, ...); to your test case that has the expected values from your other event. This will catch it and do exactly as you expect.

Additionally, since this expectEvent searches through all of the events in a transaction, you can actually have multiple of the same events get emitted in a single transaction. For example, if you had the line emit myImportantEvent(x + 1, amount); immediately after the first myImportantEvent, you could write a test and it would be able to pick up both of them.

2
  • But in my example, logs contains only the event of my own contract, not those events, that was dispatched internally thru calls to other contracts and their transfer-methods
    – delete
    Jun 14, 2019 at 6:59
  • Use expectEvent.inTransaction from openzeppelin-test-helpers
    – abcoathup
    Jul 3, 2019 at 4:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.