In Hardhat (a popular development environment for Ethereum smart contracts), Chai and Mocha are two important JavaScript libraries used for testing your smart contracts.


๐Ÿ”น What is Mocha?

Mocha is a JavaScript test framework. It provides:


๐Ÿ”น What is Chai?

Chai is an assertion library. It provides human-readable syntax to check if your code behaves as expected.

Hardhat uses Mocha for running the tests and Chai for writing the assertions.


๐Ÿงช Example: Hardhat Test Using Mocha & Chai

Suppose you have a smart contract SimpleStorage.sol:

// contracts/SimpleStorage.sol
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

contract SimpleStorage {
    uint256 private data;

    function set(uint256 _data) public {
        data = _data;
    }

    function get() public view returns (uint256) {
        return data;
    }
}


๐Ÿ“„ Test File: test/SimpleStorage.js