RISE Logo-Light

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 test

Create a test file in the test/ directory using ESM syntax:

test/Counter.ts
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 test
yarn hardhat test
pnpm exec hardhat test
bun x hardhat test

Output:

Counter
  ✔ should start with 0
  ✔ should increment
  ✔ should set number

3 passing

Run Specific Tests

Run a single test file:

npx hardhat test test/Counter.ts
yarn hardhat test test/Counter.ts
pnpm exec hardhat test test/Counter.ts
bun x hardhat test test/Counter.ts

Run 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"

Next Steps