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

misleading error message on failed expectEmit cheat code #908

Closed
2 tasks done
devtooligan opened this issue Mar 11, 2022 · 4 comments
Closed
2 tasks done

misleading error message on failed expectEmit cheat code #908

devtooligan opened this issue Mar 11, 2022 · 4 comments
Labels
A-cheatcodes Area: cheatcodes C-forge Command: forge Cmd-forge-test Command: forge test D-easy Difficulty: easy good first issue Good for newcomers P-normal Priority: normal T-bug Type: bug
Milestone

Comments

@devtooligan
Copy link

Component

Forge

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

  • Foundry
  • Foundryup

What version of Foundry are you on?

forge 0.1.0 (05eb664 2022-03-07T00:08:24.917561+00:00)

What command(s) is the bug in?

forge test

Operating System

macOS (M1)

Describe the bug

After invoking the expectEmit cheatcode, if any of the topics or data in the log does not match what was expected, it gives a misleading fail message, FAIL. Reason: Expected an emit, but no logs were emitted afterward

In my example, I had coded:

        vm.expectEmit(true, true, true, true);
        emit Liquidity(maturity, user2, user1, address(0), -INITIAL_BASE, 0, INITIAL_BASE);

but I made a mistake, the 3rd parameter shouldve been user2.

I got this:
Screen Shot 2022-03-11 at 12 17 05 PM

It would be nice to have a better message - at least something like "topics or data did not match what was expected" or even better if we could show exactly what was wrong, e.g. "Expected 0xabcbcbcbcb for topic2 but got 0x010191911991"

@devtooligan devtooligan added the T-bug Type: bug label Mar 11, 2022
@mds1
Copy link
Collaborator

mds1 commented Mar 11, 2022

Seems there may have been an expectEmit regression at some point? In #592 the failure message was Reason: Log != expected log, but I've just confirmed on forge 0.1.0 (f01d2f7 2022-03-11T00:04:44.960707+00:00) that when topics don't match I get the same no logs were emitted error

@onbjerg
Copy link
Member

onbjerg commented Mar 12, 2022

Damn the expectEmit issues are stacking up. I would love a tracking issue of some sort so I can fix them during/after the REVM port.

FWIW this is probably a regression as @mds1 pointed out. The error message is for when you call expectEmit but do not emit a log afterwards, which you seem to be doing.

@onbjerg onbjerg added Cmd-forge-test Command: forge test C-forge Command: forge A-cheatcodes Area: cheatcodes P-normal Priority: normal labels Mar 12, 2022
@onbjerg onbjerg added this to Foundry Apr 17, 2022
@onbjerg onbjerg moved this to Todo in Foundry Apr 17, 2022
@onbjerg onbjerg added good first issue Good for newcomers D-easy Difficulty: easy labels Jul 1, 2022
@onbjerg onbjerg added this to the v1.0.0 milestone Jul 1, 2022
@onbjerg
Copy link
Member

onbjerg commented Jul 3, 2022

I can't reproduce this anymore:

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

import "forge-std/Test.sol";

contract A {
    event Test(uint256 a);

    function b() external {
        emit Test(1);
    }
}

contract ContractTest is Test {
    event Test(uint256 a);

    function testExample() public {
        A a = new A();

        vm.expectEmit(false, true, false, true);
        emit Test(2);
        a.b();
    }
}
$ forge test
[⠢] Compiling...
No files changed, compilation skipped

Running 1 test for test/Contract.t.sol:ContractTest
[FAIL. Reason: Log != expected log] testExample() (gas: 70178)
Test result: FAILED. 0 passed; 1 failed; finished in 366.41µs

Failed tests:
[FAIL. Reason: Log != expected log] testExample() (gas: 70178)

Encountered a total of 1 failing tests, 0 tests succeeded

Because of this I am going to close this issue: improved error messages for expectEmit is tracked in #592. Let me know if this is still an issue for you 😄

@onbjerg onbjerg closed this as completed Jul 3, 2022
Repository owner moved this from Todo to Done in Foundry Jul 3, 2022
@kwamentw
Copy link

hi @onbjerg,
I am getting this same issue after evoking my vm.expectEmit() in my test script, When running the test you can see the values emit, but the error tells you it expected an Emit whilst there was an emit, So i do not know whether foundry can detect the emit or I am just doing something wrong. My test script and error is below ;

error:

$ forge test --mt testEmitEventTrycatch -vvvvvvv
[⠰] Compiling...
[⠃] Compiling 2 files with 0.8.20
[⠒] Solc 0.8.20 finished in 1.48s
Compiler run successful!

Running 1 test for test/TestTryCatch.t.sol:TryCatchTest
[FAIL. Reason: Expected an emit, but the call reverted instead. Ensure you're testing the happy path when using the `expectEmit` cheatcode.] testEmitEventTrycatch() (gas: 11742)
Traces:
  [273525] TryCatchTest::setUp() 
    ├─ [218863] → new TryCatchEg@0x5615dEB798BB3E4dFa0139dFa1b3D433Cc23b72f
    │   └─ ← 1093 bytes of code
    └─ ← ()

  [11742] TryCatchTest::testEmitEventTrycatch() 
    ├─ [0] VM::expectEmit() 
    │   └─ ← ()
    ├─ [3701] TryCatchEg::LetsTryCatch(21, 4234) 
    │   ├─ [411] TryCatchEg::Add(21, 4234) [staticcall]
    │   │   └─ ← 4255
    │   ├─ emit ShowResult(result: 4255)
    │   ├─ emit Log(message: Add successful)
    │   └─ ← ()
    └─ ← "Log != expected log"

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

Failing tests:
Encountered 1 failing test in test/TestTryCatch.t.sol:TryCatchTest
[FAIL. Reason: Expected an emit, but the call reverted instead. Ensure you're testing the happy path when using the `expectEmit` cheatcode.] testEmitEventTrycatch() (gas: 11742)

Encountered a total of 1 failing tests, 0 tests succeeded

my test script:

    function testEmitEventTrycatch() public {
        vm.expectEmit();
        egoftrycatch.LetsTryCatch(21, 4234);
    }

The function I am testing:

function LetsTryCatch(uint256 firstDigit, uint256 secondDigit) external {
        try this.Add(firstDigit, secondDigit) returns (uint256 result) {
            emit ShowResult(result);
            emit Log("Add successful");
        } catch Error(string memory errorMessage) {
            emit Log(errorMessage);
            // revert(errorMessage);
        } catch (bytes memory errorData) {
            emit Log(string(errorData));
            // revert(string(errorData));
        }
    }

Thanks in advance

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-cheatcodes Area: cheatcodes C-forge Command: forge Cmd-forge-test Command: forge test D-easy Difficulty: easy good first issue Good for newcomers P-normal Priority: normal T-bug Type: bug
Projects
Archived in project
Development

No branches or pull requests

4 participants