Mocha and Chai are popular testing frameworks that integrate seamlessly with Hardhat, allowing developers to write structured and expressive tests for their Ethereum smart contracts. This guide will explain how to set up and use Mocha and Chai in a Hardhat project, complete with sample code.

1. Setting Up Your Hardhat Project

Before you can use Mocha and Chai, you need to set up a Hardhat project. If you haven't done this yet, follow these steps:

mkdir my-hardhat-project
cd my-hardhat-project
npm init -y
npm install --save-dev hardhat

Next, initialize Hardhat:

npx hardhat

When prompted, choose to create an empty Hardhat configuration file.

2. Installing Mocha and Chai

Mocha and Chai come pre-installed with Hardhat, but if you want to ensure you have the latest versions, you can install them manually:

npm install --save-dev mocha chai

3. Writing Tests with Mocha and Chai

Create a test directory in your project root and add a test file, for example, Token.test.js:

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); // Deploy the contract with an initial supply of 1000
[owner, addr1] = await ethers.getSigners(); // Get the test accounts
});

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); // Check that the owner's balance matches the total supply
});

it("Should transfer tokens between accounts", async function () {
await token.transfer(addr1.address, 50); // Transfer 50 tokens to addr1
expect(await token.balanceOf(addr1.address)).to.equal(50); // Check addr1's balance
});
});

4. Running Your Tests

To run your tests, open your terminal, navigate to your Hardhat project directory, and execute the following command:

npx hardhat test

This command will:

  • Compile your smart contracts.
  • Deploy them to a local Ethereum network created by Hardhat.
  • Execute the tests defined in your test files.

Sample Output

After running the tests, you will 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)

5. Using Different Assertion Styles with Chai

Chai provides different assertion styles, including expect, should, and npx hardhat0. You can use any of these styles based on your preference. Here's an example using the should style:

npx hardhat2

Conclusion

Using Mocha and Chai with Hardhat allows developers to write clear, structured tests for their Ethereum smart contracts. With the built-in support for these frameworks, you can easily set up your testing environment and ensure your contracts behave as expected. Regular testing is crucial for maintaining the reliability and security of your smart contracts.