Testing smart contracts is a crucial aspect of the development process to ensure that the contracts function correctly and are error-free. Hardhat is a development environment for Ethereum applications that offers various tools and libraries to facilitate smart contract testing.
Here are the benefits of testing smart contracts with Hardhat:
To help you get started quickly, we have set up everything you need, from the contract to the test scripts, in this repository.
1.1 Forking the Tutorial Repository:
1.2 Cloning the repository locally:
git clone <paste the URL you copied>
To move into the cloned repository, use this commandcd <Name of the cloned Repo>
, then move to the MultiContractTutorial folder using this commandcd MultiContractTutorial
.
The structure of a Hardhat project folder typically includes the following:
In the test folder, there are several test scripts that cover different smart contracts.
2.1 Let’s start by understanding the simple-test.js
script this is associated with SimpleStorage.sol
contract.
In the first two lines of the script, we are importing the dependencies needed for testing:
The script defines a describe block for a group of tests related to the SimpleStorage
contract:
ethers.getContractFactory("SimpleStorage")
.simpleStorageFactory.deploy()
.
This code defines an it block, which is a single test within the describe block for the SimpleStorage
contract. The test does the following:
retrieve
function and stores the return value in currentValue
.assert
function from the Chai library to assert that the currentValue.toString()
is equal to 0.In summary, this test checks the default value of favoriteNumber
when the contract is deployed to be equal to zero.
The second test does the following:
store
function on the contract instance with argument 7.retrieve
function and stores the return value in updatedValue
.assert
function from the Chai library to assert that the updatedValue.toString()
is equal to 7.In summary, this test checks the store
function of SimpleStorage
contract and verifies whether it updates the favoriteNumber
correctly by the specified number.
Similarly, in the fourth test block, we are testing the addPeople
function.
2.2 ERC20 Token Testing
Let’s understand the Token-test.js
script.
In this script, we are testing the Token.sol
contract.
The script is divided into two describe blocks:
Deployment:
Transactions:
transfer
, balanceOf
, and approve
.transfer
method transfers tokens between accounts correctly.In summary, this test is checking the successful deployment of the Token contract and its functionality to transfer tokens between accounts, check the token balance, and update the allowance.
To install the necessary packages, please execute the following command:
npm install
As per standard practices, hardhat test scripts are mostly executed locally, and this has a major drawback in that we can’t debug the failed transaction. So, we are going to use BuildBear.io. With BuildBear, all our transactions can be debugged easily using their in-built transaction tracer.
3.1 To start using BuildBear, visit the BuildBear App.
3.2. Create your Private Testnet forking from the Ethereum Mainnet, feel free to fork any Chain.
3.3. Add your Private Testnet to your MetaMask wallet by using the “Add to Metamask” button:
3.4. Update thehardhat.config.js
.
[hardhat.config.js](<https://github.com/BuildBearLabs/Tutorials/blob/main/NFTMarketplace/hardhat.config.js>)
file.To run all the tests in your project, open your terminal and type the following command:
npx hardhat test
This command will execute each test case in the project and output the results to the terminal.
If all tests pass successfully, you should see a long list of passing test results displayed in the terminal, like the example shown below:
These results indicate that all of our tests have passed successfully.
If you appreciate what we are doing, please follow us on Twitter, and LinkedIn and Join the Telegram group if you haven’t done yet.
And please give us a clap 👏 if you like our work.
Github Repo : Buildbear Tutorials
About BuildBear:
BuildBear is a platform for testing dApps at scale, for teams. It provides users with their own private Testnet to test their smart contracts and dApps, which can be forked from any EVM chain. It also provides a Faucet, Explorer, and RPC for testing purposes.
BuildBear aims to build an ecosystem of tools for testing dApps at scale for the teams.
Authors: Chandan