Skip to content

Forge test expectEmit fails when it should succeed #6643

@zzh1996

Description

@zzh1996

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 (df49ddd 2023-12-21T00:22:15.620581000Z)

What command(s) is the bug in?

forge test

Operating System

macOS (Intel)

Describe the bug

  1. Run forge init

  2. Modify these files accordingly:

Counter.sol

// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.13;

contract Counter {
    event TestEvent(uint n);

    constructor() {
        emit TestEvent(1);
    }

    function f() external {
        emit TestEvent(2);
    }
}

Counter.t.sol

// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.13;

import {Test, console2} from "forge-std/Test.sol";
import {Counter} from "../src/Counter.sol";

contract CounterTest is Test {
    Counter public counter;
    event TestEvent(uint n);

    function setUp() public {
        counter = new Counter();
    }

    function test_Bug() public {
        // part1
        vm.expectEmit();
        emit TestEvent(1);
        new Counter();

        // part2
        vm.expectEmit();
        emit TestEvent(2);
        counter.f();
    }
}
  1. Run forge test -vvv

Expected behavior: test passed
Actual behavior: test failed

Traces:
  [77239] CounterTest::test_Bug()
    ├─ [0] VM::expectEmit()
    │   └─ ← ()
    ├─ emit TestEvent(n: 1)
    ├─ [33338] → new Counter@0x2e234DAe75C793f67A35089C9d99245E1C58470b
    │   ├─ emit TestEvent(n: 1)
    │   └─ ← 161 bytes of code
    ├─ [0] VM::expectEmit()
    │   └─ ← ()
    ├─ emit TestEvent(n: 2)
    ├─ [1179] Counter::f()
    │   ├─ emit TestEvent(n: 2)
    │   └─ ← ()
    └─ ← log != expected log

Note that when you comment out the three lines of code in part1, or the three lines of code in part2, the test can pass normally.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    Status

    Completed

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions