Solidity Testing – Is Waffle an Obsolete Library?

hardhatsoliditytesting

I was always using web3 for anything but now it looks like ethers is more suitable for my needs so I'm playing with it.

I'm using hardhat for testing as probably all of us, and I found out that in spite of hardhat docs claiming that I need ethers + waffle I see that ethers alone works just fine. For example lets check tutorial:

const { expect } = require("chai");
const { ethers } = require("hardhat");

describe("Greeter", function () {
  it("Should return the new greeting once it's changed", async function () {
    const Greeter = await ethers.getContractFactory("Greeter");
    const greeter = await Greeter.deploy("Hello, world!");
    await greeter.deployed();

    expect(await greeter.greet()).to.equal("Hello, world!");

    const setGreetingTx = await greeter.setGreeting("Hola, mundo!");

    // wait until the transaction is mined
    await setGreetingTx.wait();

    expect(await greeter.greet()).to.equal("Hola, mundo!");
  });
});

Zero occurrences for "Waffles". I'm rewriting my tests and I didn't find one that doesn't work with regular chai and hardhat.

So the question is: why hardhat is claiming I need it? What am I missing?

Best Answer

Waffle is very useful for mocking functions of smart contracts, without the need to write a new fake contract. Other things could probably be done with normal etherjs and chai.

Mock contract example:

const { waffle, ethers } = require('hardhat');
const { deployMockContract, provider } = waffle;

...code

  const mockERC20 = await deployMockContract(<params>);
  await mockERC20.mock.transfer.reverts();
  --> test something
  await mockERC20.mock.transferFrom.returns(true);
  --> test something
Related Topic