Testing Contracts
Test smart contracts with Hardhat for RISE
Hardhat includes a built-in testing framework using Mocha and Chai. Write tests to ensure your contracts work correctly before deploying to RISE.
Write Tests
Create Test File
Make sure you're in your project's root directory and create a new directory called test.
mkdir testCreate a test file in the test/ directory using ESM syntax:
import hre from "hardhat";
import { expect } from "chai";
const { ethers } = await hre.network.connect();
describe("Counter", function () {
it("should start with 0", async function () {
const counter = await ethers.deployContract("Counter");
expect(await counter.number()).to.equal(0n);
});
it("should increment", async function () {
const counter = await ethers.deployContract("Counter");
await counter.increment();
expect(await counter.number()).to.equal(1n);
});
it("should set number", async function () {
const counter = await ethers.deployContract("Counter");
await counter.setNumber(42n);
expect(await counter.number()).to.equal(42n);
});
});Note: In Hardhat 3, you explicitly create network connections with hre.network.connect().
Run Tests
Execute your tests:
npx hardhat testyarn hardhat testpnpm exec hardhat testbun x hardhat testOutput:
Counter
✔ should start with 0
✔ should increment
✔ should set number
3 passingRun Specific Tests
Run a single test file:
npx hardhat test test/Counter.tsyarn hardhat test test/Counter.tspnpm exec hardhat test test/Counter.tsbun x hardhat test test/Counter.tsRun tests matching a pattern:
npx hardhat test --grep "increment"yarn hardhat test --grep "increment"pnpm exec hardhat test --grep "increment"bun x hardhat test --grep "increment"