UNPKG

hardhat

Version:

Hardhat is an extensible developer tool that helps smart contract developers increase productivity by reliably bringing together the tools they want.

48 lines (38 loc) 1.36 kB
// We don't have Ethereum specific assertions in Hardhat 3 yet import assert from "node:assert/strict"; import { describe, it } from "node:test"; import { network } from "hardhat"; describe("Counter", async function () { const { viem } = await network.connect(); const publicClient = await viem.getPublicClient(); it("Should emit the Increment event when calling the inc() function", async function () { const counter = await viem.deployContract("Counter"); await viem.assertions.emitWithArgs( counter.write.inc(), counter, "Increment", [1n], ); }); it("The sum of the Increment events should match the current value", async function () { const counter = await viem.deployContract("Counter"); const deploymentBlockNumber = await publicClient.getBlockNumber(); // run a series of increments for (let i = 1n; i <= 10n; i++) { await counter.write.incBy([i]); } const events = await publicClient.getContractEvents({ address: counter.address, abi: counter.abi, eventName: "Increment", fromBlock: deploymentBlockNumber, strict: true, }); // check that the aggregated events match the current value let total = 0n; for (const event of events) { total += event.args.by; } assert.equal(total, await counter.read.x()); }); });