foundry: `vm.expectEmit` fails

Component

Forge

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

  • Foundry
  • Foundryup

What version of Foundry are you on?

forge 0.2.0 (d93312b 2023-07-30T00:26:55.660686000Z)

What command(s) is the bug in?

forge test

Operating System

macOS (Apple Silicon)

Describe the bug

After running foundryup for the first time in a while, all of my tests containing vm.expectEmit are suddenly failing. I cannot create a new test to reproduce the issue, as the behavior in the new test is as expected. I have read through #5117 and other similar issues, but none seem to apply to my use case. Here is my test:

    // should fail performing an admin action while still pending
    function testCallingAdminFunctionAsPendingAdminFails(uint256 eoaUint) public {
        address eoa = _generateRandomEoa(eoaUint);

        _deployDummyIntegration();
        address newAdmin = makeAddr("newAdmin");
        vm.startPrank(integrationDeployer);
        vm.expectEmit(true, true, false, true);
        emit SecurityAdminTransferStarted(integrationDeployer,newAdmin);
        dummyIntegration.transferSecurityAdministration(newAdmin);
        vm.stopPrank();

        assertEq(newAdmin, dummyIntegration.pendingSecurityAdmin(), "SA02: not pending admin");

        vm.startPrank(newAdmin);
        vm.expectRevert(bytes("SA01: security admin only"));
        dummyIntegration.transferSecurityAdministration(eoa);
        vm.stopPrank();
    }

I have confirmed that both events, defined in my contract, and my test file, are identical.

    event SecurityAdminTransferStarted(address indexed oldAdmin, address indexed pendingAdmin);

I tried moving the event definition into the test file, and still no dice. From the console output, we can see that the event is fired inside the first call being made, and the args match exactly.

    ├─ [0] VM::addr(<pk>) [staticcall]
    │   └─ ← newAdmin: [0x67ED0Ac45b537A79406E01656d90659325455585]
    ├─ [0] VM::label(newAdmin: [0x67ED0Ac45b537A79406E01656d90659325455585], newAdmin)
    │   └─ ← ()
    ├─ [0] VM::startPrank(0xA4B9D41Efb6b3d2C684f3909bdA5356f4F58a899)
    │   └─ ← ()
    ├─ [0] VM::expectEmit(true, true, false, true)
    │   └─ ← ()
    ├─ emit SecurityAdminTransferStarted(oldAdmin: 0xA4B9D41Efb6b3d2C684f3909bdA5356f4F58a899, pendingAdmin: newAdmin: [0x67ED0Ac45b537A79406E01656d90659325455585])
    ├─ [22386] DummyIntegration::transferSecurityAdministration(newAdmin: [0x67ED0Ac45b537A79406E01656d90659325455585])
    │   ├─ emit SecurityAdminTransferStarted(oldAdmin: 0xA4B9D41Efb6b3d2C684f3909bdA5356f4F58a899, pendingAdmin: newAdmin: [0x67ED0Ac45b537A79406E01656d90659325455585])
    │   └─ ← ()
    └─ ← "Log != expected log"

Test result: FAILED. 0 passed; 1 failed; 0 skipped; finished in 109.19ms
Ran 1 test suites: 0 tests passed, 1 failed, 0 skipped (1 total tests)

Failing tests:
Encountered 1 failing test in test/foundry/MyTest.t.sol:MyTestTest
[FAIL. Reason: Expected an emit, but the call reverted instead. Ensure you're testing the happy path when using the `expectEmit` cheatcode. Counterexample: calldata=0x5710534d0000000000000000000000000000000000000000000000000000000000000000, args=[0]] testCallingAdminFunctionAsPendingAdminFails(uint256) (runs: 0, μ: 0, ~: 0)

The reason seems to indicate that the issue has to do with the call reverting, but that is expected behaviour with the expectRevert included. As a sanity check, the test still fails when the expectRevert portion of the test is commented out.

Attempting to reproduce the error was unsuccessful, as the behavior is as expected and the test passes:

pragma solidity 0.8.19;

import "forge-std/Test.sol";

contract EmitContract {
     event TestEvent(address indexed sender, uint256 indexed value);

     function shouldEmit(uint256 val) public {
       emit TestEvent(msg.sender, val);
     }
}

interface IEvents {
     event TestEvent(address indexed sender, uint256 indexed value);
}

contract EmitTest is Test, IEvents {


    EmitContract emitContract;

    function setUp() public {
      emitContract = new EmitContract();
    }
    function testEvent(uint256 val) public {

       address eoa = makeAddr("eoa");
       vm.startPrank(eoa);
       vm.expectEmit(true, true, false, true);
       emit TestEvent(eoa, val);
       emitContract.shouldEmit(val);
       vm.stopPrank();
    }
}

I have tried expecting the emission with all values set to false, and without any args, and the result is the same.

About this issue

  • Original URL
  • State: closed
  • Created 3 years ago
  • Reactions: 3
  • Comments: 15 (4 by maintainers)

Most upvoted comments

Even I’m facing a similar issue. I’ve defined it here👇🏾 Discussion Page

Awaiting for help on this. Thanks!

This was fixed! The issue was that I had not followed the sequence of calls for a successful vm.expectEmit(...) to catch the emitted event. You will be able to view the answer on the Discussion page itself.

Hey all - was running into similar issue. Figured out I needed to redefine the events in my test contract (they’re not inherited, as they are NOT types like enums/structs/etc.).

Hope this saves someone some time.