If you’re familiar with Ethereum development and Solidity, you might have heard about the Foundry test framework that has been making waves in the Smart contract development industry.
Being able to write tests and deployment scripts in Solidity not only makes it super easy and quick to test and deploy contracts, but the speed of tests increases dramatically. It has been noted that Foundry is almost 5 times faster compared to Hardhat in terms of test execution.
In addition to that, Foundry offers Fuzz testing out of the box. Foundry Fuzzer generates random values for your tests to test for edge cases.
Is it Possible to use Foundry with an Existing Hardhat Project?
Yes, absolutely. Although, Foundry uses a different directory structure compared to Hardhat, however, it’s still possible to integrate Foundry with Hardhat. Many popular blockchain projects that already have their code-bases developed with hardhat have integrated with Foundry as an additional setup to better test their smart contracts.
And you can do the same. If you already have your project developed with Hardhat framework, you can still use Foundry with it. In fact, many projects prefer to deploy their contracts with Hardhat tooling but test their contracts with Foundry.
How to Install Foundry on your Computer
We are including these steps just in case you are installing Foundry for the first time on your PC. You will need Linux to run the installation command below. If you want to install it on Windows, you will need Windows Subsystem for Linux installed.
First, run the following command:
curl -L https://foundry.paradigm.xyz | bash
Then run this command to install Foundry:
How to Setup Foundry with hardhat
Here are the steps you need to follow in order to integrate Foundry with your Hardhat project:
Let’s suppose that you have a Hardhat project with dependencies such as @oppenzeppelin/contracts inside the node_modules directory.
Now, in your hardhat project, you should have the following directory structure:
- Smart contracts are in the /contracts folder
- Hardhat unit tests are located at the /test folder. We will put Foundry test files inside /test/foundry
- Hardhat inserts its cache in /cache folder. We will put the Foundry cache inside the folder /forge-cache
Create a sample Foundry project on your PC. To initiate a new project run the following command:
forge init your_project
Now copy the
lib/forge-stdfrom this sample project to your Hardhat project directory.
- Now, copy the foundry.toml configuration file to your hardhat project directory and change these variables: src, out, test, cache_path inside this file like so:
- Create a new file called remappings.txt in order to make Foundry project work well with the VS Code Solidity extension. Insert the following into this file:
- Now create a sub-directory test/foundry and write your Foundry tests in it. Just for testing purposes, copy the contract .sol file from the sample foundry project we created earlier and put it inside the /contracts folder in your main hardhat project.Also, copy the sample Counter.t.sol file from the sample project to the /test/foundry folder.Once that’s done, now you can compile the contract using command:
And finally you can run the Foundry test using this command:
That’s all you need to do to add Foundry tests into your hardhat project. Hope you enjoyed it!