Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Testing related to events broken #6170

Closed
2 tasks done
Philogy opened this issue Oct 30, 2023 · 1 comment · Fixed by #6172
Closed
2 tasks done

Testing related to events broken #6170

Philogy opened this issue Oct 30, 2023 · 1 comment · Fixed by #6172
Labels
T-bug Type: bug

Comments

@Philogy
Copy link

Philogy commented Oct 30, 2023

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 (dee4181 2023-10-30T00:17:30.403957319Z)

What command(s) is the bug in?

forge test

Operating System

Linux

Describe the bug

The Example

// SPDX-License-Identifier: MIT
pragma solidity 0.8.21;

import {Test} from "forge-std/Test.sol";

contract Emitter {
    event Values(uint256 indexed a, uint256 indexed b);

    function plsEmit(uint256 a, uint256 b) external {
        emit Values(a, b);
    }
}

contract MyTest is Test {
    event Values(uint256 indexed a, uint256 b);

    Emitter e = new Emitter();

    function testCorrect() public {
        vm.expectEmit(true, true, false, true);
        emit Values(69, 420);
        e.plsEmit(69, 420);
    }
}

Description

When creating asserts about event logs with vm.expectRevert. However when the comparison fails the test gives insufficient partially incorrect feedback:

[FAIL. Reason: Expected an emit, but the call reverted instead. Ensure you're testing the happy path when using the `expectEmit` cheatcode.]

You get a hint at real the cause of the error (log mismatch) if you increase verbosity:

[FAIL. Reason: Expected an emit, but the call reverted instead. Ensure you're testing the happy path when using the `expectEmit` cheatcode.] testCorrect() (gas: 11332)
Traces:
  [11332] MyTest::testCorrect() 
    ├─ [0] VM::expectEmit(true, true, false, true) 
    │   └─ ← ()
    ├─ emit Values(a: 420)
    ├─ [1756] Emitter::plsEmit(69, 420) 
    │   ├─ emit Values()
    │   └─ ← ()
    └─ ← "Log != expected log"

The base fail message is however still incorrect. This is made more confusing by the fact that the trace seems to omit logging the parameters (a and b are missing)?

Expected Behavior

  1. The fail message clearly states that the fail was due to a mismatching event
  2. The event is displayed in full in the trace
  3. The log != expect ... message is more specific indicating why the comparison failed:
    1. No event was emitted (ideally also tells you if events were simply out-of-order)
    2. Wrong event (topic0 mismatch)
    3. The value of one of the fields is wrong
    4. Type mismatch, this can be especially hard to debug with the current state (e.g. name match but field missing, type of fields mismatch, and hardest to notice currently: everything matches except indexed on some fields)
@Philogy Philogy added the T-bug Type: bug label Oct 30, 2023
@mattsse
Copy link
Member

mattsse commented Oct 30, 2023

pls prio @Evalir

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
T-bug Type: bug
Projects
Archived in project
2 participants