In Hardhat (a popular development environment for Ethereum smart contracts), Chai and Mocha are two important JavaScript libraries used for testing your smart contracts.
Mocha is a JavaScript test framework. It provides:
before(), beforeEach(), after(), etc.describe() and it() to group and run testsChai is an assertion library. It provides human-readable syntax to check if your code behaves as expected.
expect(), assert(), and should() styles for writing assertionsHardhat uses Mocha for running the tests and Chai for writing the assertions.
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/SimpleStorage.js