foundry: `forge test` hangs on `invariant` testing

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 (97f070f 2023-03-17T09:30:30.350509Z)

What command(s) is the bug in?

No response

Operating System

macOS (Intel)

Describe the bug

When running any type of invariant testing, the terminal hangs after compiling the contracts:

image

This is the function I’m calling:

function invariant_getHello() public {
    assertTrue(true);
}

It hangs with either forge test --match-test $FUNC --fork-url $ARB --fork-block-number $BLOCK -vvv where $FUNC is invariant_getHello or with forge test --match-test invariant --fork-url $ARB --fork-block-number $BLOCK -vvv.

If I change the function name to test_getHello, the test runs and passes without issues, so the problem is in the invariant keyword:

image

About this issue

  • Original URL
  • State: open
  • Created a year ago
  • Reactions: 1
  • Comments: 49 (10 by maintainers)

Most upvoted comments

Don’t forget about me please.

I really really want to get invariant testing to work, and I’ve been struggling for like a week trying to solve it.

Or is it a bug and there’s no workaround I can do to get it to work and a patch from you guys is the only solution? @mds1 @mattsse

sorry about the inconvenience

I guess we just haven’t tested this with forking mode properly. Thanks for flagging this.

@mds1 I think both fixes would be pretty simple

Perhaps instead, if senders is empty, we change it to 5-10 default random senders to use for invariant testing?

this makes sense, this way we still have the actual accounts

Have a similar issue. Not using a forked network. Sometimes invariant tests never terminate, it may be the same thing. You can check by writing lines to a file in the test and see it keeps calling functions and testing invariant forever as if depth = infinity, or depth counter fails to increment after some point

ah there’s targetSender. i’m not familiar with targetSender works, will check

@cdgmachado0 If you want a workaround in the meantime just use targetSenders() to hardcode a list of 1 or more senders, that way random senders are not generated. You can read more in the docs: https://book.getfoundry.sh/forge/invariant-testing#invariant-test-helper-functions