TrueFiEng / Waffle

Library for writing and testing smart contracts.
https://getwaffle.io
MIT License
960 stars 161 forks source link

emitWithArgs(contract, eventName, {args}) and emitCount(n) #835

Open MantisClone opened 1 year ago

MantisClone commented 1 year ago

Is your feature request related to a problem? Please describe. My contract function emits the Transfer event twice with different arguments if certain conditions are met but only once otherwise. I want to write a test where I check that the first Transfer event was emitted but the 2nd Transfer event was NOT.

The emit(Transfer).withArgs(1st_transfer_args) chained matchers successfully match the first Transfer event, but chaining not.emit(Transfer).withArgs(2nd_transfer_args) fails to match because the Transfer event was, in fact, emitted so the withArgs(2nd_transfer_args) is never evaluated.

Describe the solution you'd like Some new matchers that would help:

  1. emitWithArgs(contract, eventName, { args }) - this combines the emit and withArgs matchers for more precision.
  2. emitCount(contract, eventName, n) - This counts the number of times an event was emitted.
  3. emitOnce - this is syntactic sugar for emittedCount(1)
  4. emitTwice
  5. emitThrice

Describe alternatives you've considered None.

Additional context Example usage:

// In this test, only the first Transfer event is emitted
await expect(contract.transferAtLeastOnceButSometimesTwice())
  .to.emitWithArgs(contract, 'Transfer', { "1st_transfer_args" })
  .to.not.emitWithArgs(contract, 'Transfer', { "2nd_transfer_args" })
  .to.emitCount(contract, 'Transfer', 1)
  .to.emitOnce(contract, 'Transfer');