Skip to content

Fix Hardhat Waffle Test Errors

DodaTech Updated 2026-06-26 1 min read

You will learn how to use Waffle's chai matchers for cleaner smart contract testing.

The Problem

The hardhat test waffle pattern is frequently misapplied in smart contract and dapp development, leading to vulnerabilities, gas inefficiencies, or logic errors. This guide shows the correct implementation and common pitfalls to avoid.

Quick Fix

Wrong

const { expect } = require("chai"); const balance = await token.balanceOf(addr); expect(balance).to.equal(1000);

Assertion fails even if balance is 1000 because BigNumber !== Number.

const { expect } = require("chai"); const balance = await token.balanceOf(addr); expect(balance).to.equal(ethers.utils.parseUnits("1000"));
ethers.utils.parseUnits converts to BigNumber. Equality check works correctly.

Prevention

  • Use hardhat-chai-matchers for proper BigNumber comparison
  • Always parse token amounts when comparing
  • Avoid mixing ethers.js and waffle matchers in the same test file
  • Use DodaTech's test pattern library
  • Test edge cases: zero amounts, max uint values

DodaTech Tools

Built by the developers of Doda Browser, DodaZIP, and Durga Antivirus Pro. Doda Browser's developer tools include a Solidity debugger and transaction inspector. DodaZIP archives secure contract templates for team collaboration. Durga Antivirus Pro scans deployed contracts for known vulnerability signatures.

FAQ

### What is the difference between Waffle and Hardhat matchers?

Waffle provides basic chai matchers for Ethereum. Hardhat-chai-matchers extends them with Hardhat-specific features.

How do I test event emissions with Waffle?

Use expect(tx).to.emit(contract, 'EventName').withArgs(arg1, arg2).

Can I use both Waffle and Hardhat toolbox?

Yes. @nomicfoundation/hardhat-toolbox includes Waffle-compatible matchers with Hardhat extensions.

Built by the developers of DodaTech

Doda Browser, DodaZIP & Durga Antivirus Pro