Waffle icon indicating copy to clipboard operation
Waffle copied to clipboard

Add a way to capture events values to be able to write tests requiring them

Open bhuisgen opened this issue 3 years ago • 2 comments

As the issue #581, I need an easy way to capture values from event to reuse them.

For the moment and thanks to @wminshew I did this:

  it('should burn tokens if asked', async () => {
    const response = await (
      await contract.safeMint(await owner.getAddress())
    ).wait();

    expect(response.events[0].event).to.equals('Transfer');
    const [, , tokenId] = response.events.[0].args;

    await expect(await contract.burn(tokenId)).to.emit(contract, 'Transfer');
  });

Note the boring stuff about response / await / await / wait and the required expect on the event name. Maybe something like that could be interesting:

const args = await expect(await contract.safeMint(await owner.getAddress())).to.emit(contract, 'Transfer').args();
const tokenId = args[2];

bhuisgen avatar May 16 '22 15:05 bhuisgen

Interesting, wondering what we would like to return in the case of chaining the matcher, ie:

await expect(tx)
  .to.emit(contract, 'Transfer')
  .to.emit(contract, 'Mint')
  .args()

Just the arguments of the last one, Mint? Have you thought about this, what would make sense to you in this case?

rzadp avatar May 17 '22 09:05 rzadp

Yes, it would be perfect ie args() returning the values of the event from the last emit().

bhuisgen avatar May 17 '22 10:05 bhuisgen