Fix Hardhat Waffle Test Errors
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.
Right
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
Built by the developers of DodaTech
Doda Browser, DodaZIP & Durga Antivirus Pro