21
votes
Accepted
Foundry not outputting console.log
You need to increase the verbosity of forge test
That is, try forge test -vv for the log output. or forge test -vvvv for a full trace of every function call.
https://book.getfoundry.sh/reference/...
18
votes
Accepted
Hardhat vs foundry, which to use for testing?
When it comes to testing, Foundry excels compare to Hardhat, the speed difference is incredible. In my test with 1k queries and transactions on-chain the difference is from 15 min with hardhat to 1 ...
17
votes
Accepted
Failed to get EIP-1559 fees error when deploying to ZkEVM polygon using Foundry
try to add suffix --legacy in forge script
Note
The solution works for zkSync as well.
12
votes
Accepted
What's the difference between invariant and fuzz testing?
Summary
Fuzz testing, also known as fuzzing, involves providing random data as inputs during testing.
Invariant tests are tests that focus on verifying the conditions that must always hold in a system....
11
votes
Accepted
How to send array of structs using Foundry cast
Figured it out!
cast send CONTRACT_ADDRESS "updatePrices((address,uint256,uint256)[])" "[(ADDR,100,200),(ADDR,300,400)]" --rpc-url RPC_URL --private-key=P_KEY
11
votes
Accepted
Forge-std/Test.sol imported and working but VSCode marks an error
remappings.txt is used by VSCode to remap the imports (Forge might use the remappings declared in foundry.toml, remappings.txt or lookup in ./lib/ without the need to manually declare them, which is ...
8
votes
How to spoof accounts using Foundry?
There is even a simpler solution than suggested by adijo.
address someUser = vm.addr(somePrivateKey);
To get the private key either use an integer > 0, or do it more properly like this
string ...
8
votes
Accepted
How do I access an event in a foundry script?
You can use vm.recordLogs() and then just get the logs and access them. Also please note that vm.getRecordedLogs() function will consume the recorded logs when called.
vm.recordLogs();
emit LogTopic1(...
7
votes
Accepted
Catching custom error
SOLUTION 1:
Best solution is using expectRevert cheat code from forge-std:
vm.expectRevert(ExampleBridgeContract.InvalidCaller.selector);
exampleBridge.convert(empty, empty, empty, empty, 0, 0, 0, ...
7
votes
Accepted
Foundry skip compilation on test
We recently added forge build --skip <...>. So you can do forge build --skip test link and that will only compile src/.
We also recently trimmed down forge-std for v1.2 so compilation times with ...
7
votes
Foundry: skip specifics tests
In addition to the @Olivier Demeaux answer.
If I needed to skip some tests I would do
Make tests internal/private. Forge will run only public or external tests
Remove the "test" word to ...
7
votes
Hardhat vs foundry, which to use for testing?
I agree with other answers about foundry's speed, it is incredible, as is its reandomization testing and its debugging options. So, if you need fast tests or are stuck on an error, quickly (and easily)...
7
votes
Accepted
Foundry pranking different in test contract vs contract
Your understanding is correct. The prank cheatcode only applies to calls made from your tests; you cannot use the prank cheatcodes to change the msg.sender of the test functions themselves.
To quote ...
7
votes
Accepted
How to configure multiple solidity compiler versions in foundry?
So it doesn't seem like Foundry allows you to manually configure multiple solc versions however Foundry does automatically detect and configure the required compilers for your project, you'll need to ...
6
votes
Accepted
How to fund an address in foundry test
For most such cases you need to find a vm function that does what you want. I recommend you check out this link. In this case it's vm.deal where you pass in the address as well as the amount of ether ...
6
votes
Hardhat vs foundry, which to use for testing?
Here is my experience with Hardhat - for us, Hardhat works very well. We use it as the Test Node, I think in the Foundry ecosystem this is equivalent to the Anvil. So here are the points why Hardhat ...
6
votes
How to use anvil accounts in the Foundry Test
When you run the anvil command, you can use the exact same mnemonic (secret phrase) to generate the keys & addresses.
anvil -m 'test test test test test test test test test test test junk'
When ...
5
votes
Accepted
Transaction gas cost in Foundry `forge` unit tests
It is not currently possible to create custom spans for gas reporting in Forge - or rather, Forge does not have a built-in mechanism for this.
The only way you can achieve something similar, is to ...
5
votes
vm.prank is not chaging the msg.sender
The prank() cheat code in Foundry pranks the sender of the next call (and only the next call), all your subsequent calls (ownerOf, approve) are back to the original context, including msg.sender.
You ...
5
votes
Accepted
Foundry expectRevert on Custom Error with Multiple Errors
I had to remove the space between uint256 and uint256 on this line: bytes4 selector = bytes4(keccak256("MaximumContributionExceeded(uint256,uint256)"));
5
votes
Accepted
How to configure custom ethereum node in foundry project?
Yes, Foundry allows users to configure custom Ethereum nodes.
RPC-Endpoints settings
Custom RPC endpoints can be defined in the foundry.toml under [rpc_endpoints] as follows:
# foundry.toml
[...
5
votes
Accepted
How to deploy a contract in Foundry?
There are multiple different ways to deploy contracts in Foundry.
The most basic ones use forge create and require you to pass some OPTIONS in-line when you run the command.
Here is an example of ...
5
votes
Accepted
What are the differences of Foundry vm.prank vs vm.broadcast
prank sets msg.sender to the address provided to it.
broadcast actually creates transactions that can then be signed and sent on chain.
broadcast can be used to deploy contracts inside of scripts, for ...
4
votes
Importing openzeppelin contracts in foundry
You need to add remapping to a separate remappings.txt file instead of foundry.toml. (Seems like the instructions in video is a bit outdated).
Try to follow the instructions in https://w.mirror.xyz/...
4
votes
Accepted
To run vm.expectEmit() in my foundry test, do I need to put the events in the testing contract or can they be inherited?
It is the way it's been done in the docs.
See: https://book.getfoundry.sh/cheatcodes/expect-emit
You will have to declare locally in your tests the events and do
event Transfer(address indexed from, ...
4
votes
To run vm.expectEmit() in my foundry test, do I need to put the events in the testing contract or can they be inherited?
I documented this in an article here if you're interested.
An approach I've been taking to have it be a bit cleaner is to inherit an interface of events only. The downside to this approach is that you ...
4
votes
Foundry: skip specifics tests
Yes you can!
You just have to add "no_match_test" to your foundry.toml file, then the name of the test.
I ran my tests, then added the line
no_match_test = "testClaimTreasure"
to ...
4
votes
Foundry: skip specifics tests
For any forge test, we support filtering via matches.
➜ ~ forge t --help
Run the project's tests.
Usage: forge test [OPTIONS]
Options:
-h, --help
Print help information (use `-h` for a ...
4
votes
Accepted
Exclusive tests in forge
Yes you can!
You can use forge test --match-contract $YourContractName
or even forge test --match where, for exemple, if you use --match mint, forge will execute all tests that have 'mint' in their ...
4
votes
Hardhat vs foundry, which to use for testing?
I personally use Foundry. You can test whatever you want in either, so won’t (and shouldn’t) have to use both in a given project. The biggest difference is that Foundry lets your write tests in ...
Only top scored, non community-wiki answers of a minimum length are eligible
Related Tags
foundry × 360solidity × 208
forge × 95
testing × 59
hardhat × 16
test × 16
fuzz-test × 16
solidity-0.8.x × 15
blockchain × 14
contract-deployment × 14
unittesting × 13
contract-development × 12
chainlink × 12
cast × 12
evm × 10
anvil × 9
web3js × 8
ether × 8
contract-debugging × 8
struct × 7
ethers.js × 7
invariant-test × 7
remix × 6
error × 6
openzeppelin × 6