Your First console.log
A standout feature of Hardhat is the ability to use console.log for debugging directly within your smart contracts. To enable this, you need to import hardhat/console.sol into the contract you want to debug.
For instance, in the Lock.sol contract, you can import hardhat/console.sol and add a few console.log statements in the constructorβsuch as printing "Creating" and displaying the contractβs Ether balance. This not only confirms that the contract was deployed successfully but also allows you to log useful information, like the balance right after deployment.
// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.20;
import "hardhat/console.sol";
contract Counter {
uint256 public number;
function setNumber(uint256 newNumber) public {
number = newNumber;
console.log("Number set to", newNumber);
}
function increment() public {
number++;
console.log("Number incremented to", number);
}
}
To test the contract, create a new file named Counter.js inside the test directory and add the following content:
const {
time,
loadFixture,
} = require("@nomicfoundation/hardhat-toolbox/network-helpers");
const { anyValue } = require("@nomicfoundation/hardhat-chai-matchers/withArgs");
const { expect } = require("chai");
describe("Counter", function () {
async function deployCounterFixture() {
const [owner, otherAccount] = await ethers.getSigners();
const Counter = await ethers.getContractFactory("Counter");
const counter = await Counter.deploy();
return { counter, owner, otherAccount };
}
describe("Deployment", function () {
it("Should set the initial number to 0", async function () {
const { counter } = await loadFixture(deployCounterFixture);
expect(await counter.number()).to.equal(0);
});
});
describe("Functions", function () {
describe("setNumber", function () {
it("Should set the number to the provided value", async function () {
const { counter } = await loadFixture(deployCounterFixture);
const newNumber = 42;
await counter.setNumber(newNumber);
expect(await counter.number()).to.equal(newNumber);
});
it("Can be called by any account", async function () {
const { counter, otherAccount } = await loadFixture(deployCounterFixture);
const newNumber = 100;
await counter.connect(otherAccount).setNumber(newNumber);
expect(await counter.number()).to.equal(newNumber);
});
});
describe("increment", function () {
it("Should increment the number by 1", async function () {
const { counter } = await loadFixture(deployCounterFixture);
expect(await counter.number()).to.equal(0);
await counter.increment();
expect(await counter.number()).to.equal(1);
});
it("Should work with non-zero initial values", async function () {
const { counter } = await loadFixture(deployCounterFixture);
const initialValue = 41;
await counter.setNumber(initialValue);
await counter.increment();
expect(await counter.number()).to.equal(initialValue + 1);
});
it("Can be called by any account", async function () {
const { counter, otherAccount } = await loadFixture(deployCounterFixture);
expect(await counter.number()).to.equal(0);
await counter.connect(otherAccount).increment();
expect(await counter.number()).to.equal(1);
});
});
});
});
Then you can run:
npx hardhat test
You should see the following in the terminal:

Last updated