Running tests in Hardhat is a straightforward process that helps ensure your smart contracts function as expected. This guide will walk you through the steps to run tests using Hardhat, including setting up your testing environment and executing your test scripts.
1. Setting Up Your Hardhat Project
Before you can run tests, ensure you have a Hardhat project set up. If you haven't done this yet, follow these steps:
- Create a new directory for your Hardhat project:
mkdir my-hardhat-project
cd my-hardhat-project
npm init -y
npm install --save-dev hardhat
npx hardhat
Select "Create an empty hardhat.config.js" when prompted.
2. Writing Tests
Next, create a test
directory in your project root and add a test file. For example, create a file named Token.test.js
in the test
directory:
const { expect } = require("chai");
const { ethers } = require("hardhat");
describe("Token contract", function () {
let token;
let owner;
let addr1;
beforeEach(async function () {
const Token = await ethers.getContractFactory("Token");
token = await Token.deploy(1000);
[owner, addr1] = await ethers.getSigners();
});
it("Should assign the total supply of tokens to the owner", async function () {
const ownerBalance = await token.balanceOf(owner.address);
expect(await token.totalSupply()).to.equal(ownerBalance);
});
it("Should transfer tokens between accounts", async function () {
await token.transfer(addr1.address, 50);
expect(await token.balanceOf(addr1.address)).to.equal(50);
});
});
3. Running Your Tests
To run your tests, open your terminal and navigate to your Hardhat project directory. Then, execute the following command:
npx hardhat test
This command will look for all files in the test
directory that end with .js
and execute the tests within those files. You should see output similar to the following:
Token contract
✓ Should assign the total supply of tokens to the owner
✓ Should transfer tokens between accounts
2 passing (Xms)
4. Running Specific Tests
If you want to run a specific test file, you can specify the path to the test file:
npx hardhat test test/Token.test.js
5. Running Tests with a Custom Network
If you want to run your tests on a custom network (like a local Hardhat network), you can start the Hardhat network in a separate terminal window:
npx hardhat node
Then, in another terminal, run your tests against this network:
npx hardhat test --network localhost
6. Viewing Test Coverage
To check the coverage of your tests, you can use the solidity-coverage
plugin. First, install it:
npm install --save-dev solidity-coverage
Then, update your hardhat.config.js
file to include the plugin:
require("solidity-coverage");
Now you can run the coverage report with:
npx hardhat coverage
Conclusion
Running tests in Hardhat is an essential part of the development process for smart contracts. By following the steps outlined in this guide, you can ensure your contracts are functioning correctly and securely. Regular testing helps catch bugs early and improves the overall quality of your code.