How can one utilize the expectEvent function from the test-helpers library in a Hardhat test?

I'm migrating from Truffle to Hardhat. I've been using an upgradable contract for my smart contract. While hardhat-truffle5 works fine for tests with normal contracts, I find myself forced to use Ethers contract initialization for the upgradable contract. How can I test the events that occur in my contracts? I'm encountering an error while using the test-helpers library.

:1234: Code to reproduce

hardhat.config.js

require("@nomiclabs/hardhat-truffle5");
require("@nomiclabs/hardhat-ganache");
require('@openzeppelin/hardhat-upgrades');
...

marketplace.test.js

...
const Marketplace = await ethers.getContractFactory("Marketplace");
this.marketplace = await upgrades.deployProxy(Marketplace, [], {
        kind: "uups",
        from: owner,
        initializer: "initialize",
      });
await this.marketplace.waitForDeployment();
...
const tx = await this.marketplace.connect(this.buyer).buyItem(this.collection.target, 0, {
        from: buyer,
        value: price,
      });
const receipt = await tx.wait();

expectEvent(receipt.logs, "ItemBought", {
        from: other,
        to: buyer,
        collection: this.collection.target,
        tokenId: new BN(0),
        amount: new BN(1),
        price: new BN(price),
      });

Result:

Error: Unknown transaction receipt object

If anyone has insights or suggestions on how to effectively test events in upgradable contracts within the Hardhat framework, your guidance would be greatly appreciated. Thank you!

This error-message implies that you need to use receipt and not receipt.logs.

Yes, I tried, but I received this error:

No 'ItemBought' events found

Using receipt.logs, I can see the event emitted.

This error-message implies that unlike before, there is no problem with the receipt that you are passing to the function.

Perhaps the function is unable to find an event with the specific parameters that you are specifying.


You can always implement your own function to search the specified event in the receipt.logs object, but if you want the hard-hat function, then you need to provide the receipt object.

The OpenZeppelin test helpers are likely to be deprecared soon since there are now better alternatives and it also doesn’t work with Hardhat since it’s expecting a Truffle receipt.

Our recommendation is to use Hardhat chai matchers.