Skill 6.2.1
Write unit tests that assert state, return values and events
Key Concepts
In a Hardhat Mocha test, const { ethers } = await network.create(); connects to a fresh simulated network. await ethers.deployContract("Token", [args]) deploys a contract, and await ethers.getSigners() returns funded accounts. Tests are grouped in describe blocks and written as it cases.
Assertions use Chai: expect(await token.balanceOf(owner.address)).to.equal(1000n) checks state through a view function. Values returned by ethers.js are BigInt, so expected values use the n suffix. For events, await expect(token.transfer(to, 10n)).to.emit(token, "Transfer").withArgs(owner.address, to, 10n) checks that the transaction emitted the event with the expected arguments.
In a Solidity test, the test contract deploys the contract under test in setUp(), and each test_ function calls it and checks results with require or the assertion helpers of a test library such as forge-std (assertEq).