foundry icon indicating copy to clipboard operation
foundry copied to clipboard

bug(`cheatcodes`): expectEmit behaves weirdly

Open MathisGD opened this issue 7 months ago • 2 comments

Component

Forge

Have you ensured that all of these are up to date?

  • [x] Foundry
  • [ ] Foundryup

What version of Foundry are you on?

1.1.0-stable

What version of Foundryup are you on?

0.3.3

What command(s) is the bug in?

forge t

Operating System

None

Describe the bug

in a call following a expectEmit, if a reverts happens before the event emission the test will fail with log != expected log. It seems way more natural to fail with EvmError: Revert for two reasons: nobody should be testing even emission on failing paths (events are not emitted if the call reverted), and reverts are "more important" than wrong log. Also the inconsistent behaviour if the event is emitted before the revert is weird (the event will not be emitted if the call reverts).

minimal example:

    event Foo();

    function revertingBefore() external {
        revert();
        emit Foo();
    }

    function revertingAfter() external {
        emit Foo();
        revert();
    }

    // this test fails with `log != expected log`
    function testBefore() public {
        vm.expectEmit();
        emit Foo();
        this.revertingBefore(); // reverting call
    }

    // this test fails with `EvmError: Revert`
    function testAfter() public {
        vm.expectEmit();
        emit Foo();
        this.revertingAfter(); // reverting call
    }

https://github.com/MathisGD/forge-expect-emit-bug/

MathisGD avatar May 08 '25 09:05 MathisGD

Hey @MathisGD, thanks for the report - I agree, it would be expected that this reverts with EvmError: Revert

Related section:

https://github.com/foundry-rs/foundry/blob/292d248db6f7a1fd0279e9892e566ef2674332ae/crates/cheatcodes/src/inspector.rs#L1645-L1665

zerosnacks avatar May 08 '25 10:05 zerosnacks