0
0
Blockchain / Solidityprogramming~10 mins

Hardhat testing setup in Blockchain / Solidity

Choose your learning style9 modes available
Introduction

Hardhat testing setup helps you check if your smart contracts work correctly before using them on real blockchains. It saves time and money by catching mistakes early.

When you want to make sure your smart contract functions as expected.
Before deploying contracts to a live blockchain to avoid costly errors.
When you want to automate contract checks during development.
To test how contracts behave with different inputs and scenarios.
When collaborating with others to ensure consistent contract behavior.
Syntax
Blockchain / Solidity
const { expect } = require("chai");
const { ethers } = require("hardhat");

describe("ContractName", function () {
  it("should do something", async function () {
    const Contract = await ethers.getContractFactory("ContractName");
    const contract = await Contract.deploy();
    await contract.deployed();

    expect(await contract.someFunction()).to.equal(expectedValue);
  });
});

describe groups tests together.

it defines a single test case.

Examples
This example tests storing and retrieving a number in a contract.
Blockchain / Solidity
describe("SimpleStorage", function () {
  it("should store and retrieve a value", async function () {
    const SimpleStorage = await ethers.getContractFactory("SimpleStorage");
    const storage = await SimpleStorage.deploy();
    await storage.deployed();

    await storage.store(42);
    expect(await storage.retrieve()).to.equal(42);
  });
});
This test checks if the token contract assigns the initial supply to the owner.
Blockchain / Solidity
describe("Token", function () {
  it("should assign initial supply to owner", async function () {
    const [owner] = await ethers.getSigners();
    const Token = await ethers.getContractFactory("Token");
    const token = await Token.deploy(1000);
    await token.deployed();

    expect(await token.balanceOf(owner.address)).to.equal(1000);
  });
});
Sample Program

This test deploys a Counter contract, checks the initial count is zero, then increments and checks the count is one.

Blockchain / Solidity
const { expect } = require("chai");
const { ethers } = require("hardhat");

describe("Counter", function () {
  it("should start at zero and increment", async function () {
    const Counter = await ethers.getContractFactory("Counter");
    const counter = await Counter.deploy();
    await counter.deployed();

    expect(await counter.getCount()).to.equal(0);

    await counter.increment();
    expect(await counter.getCount()).to.equal(1);
  });
});
OutputSuccess
Important Notes

Make sure your contract is compiled before running tests.

Use npx hardhat test to run tests in your project folder.

Tests run on a local blockchain provided by Hardhat automatically.

Summary

Hardhat testing setup helps you write and run tests for smart contracts easily.

Use describe and it blocks to organize tests.

Tests run on a local blockchain, so no real money is used.