Error using expectEvent.inTransaction

Hi all,
I’m trying to pass a test when a specific event is emitted using expectEvent.inTransaction() method provided by OZ test helpers.

  • I call function1 with a JS Promise.
  • function1 calls function2 which emits an event.

Though I get an error as shown below. What is the correct way to pass a test when a specific event is emitted ?

:computer: Environment
solc 0.6
OZ contract v3.4.0
OZ test-helpers v0.5.11
Hardhat

:memo:Details

I assume I must use expectEvent.inTransaction() instead of expectEvent() given the event is emitted as a result of a secondary function call within the contract.
Parts of code are emitted for simplicity:

:1234: Code to reproduce

Smart Contract

contract MyContract
{
string message;
constructor(){
_message = "This event has happened."
}

event THIS_EVENT(string message);

function1() {
function2();
}

function2() {
emit THIS_EVENT("_message")
 }

}

test.js

const { expectEvent } = require('@openzeppelin/test-helpers');
const { ethers }      = require("hardhat");

describe("test", function() => {
let mycontract;
before( async() => {
    const Mycontract = await ethers.getContractFactory("MyContract");
    mycontract = await Mycontract.deploy();
})

it("expected event is emitted.", async() => {
    const{ hash } = await mycontract.function1();
    await expectEvent.inTransaction(hash, mycontract, "THIS_EVENT");
 });
});

However this is the error I get in the console:

     Error: Unknown contract object

Hi, welcome! :wave:

I think you used a wrong way to deploy the contract, and it should be:

const Mycontract = await ethers.getContractFactory("MyContract");
mycontract = await Mycontract.deploy();
await mycontract.deployed();

Unfortunately test-helpers does not support Ethers.js contracts. You need to use Web3 or Truffle-style contracts. When using Hardhat, you can use the hardhat-web3 or hardhat-truffle5 plugins.

Oh, surprised. I thought all “professionals” use now hardhat & ethers… and everything from OpenZeppelin of course anyway :wink:

I was trying as well to get expectRevert and time (for moving block.timestamp) to work in hardhat + TypeScript … and I am struggling a bit to get it to work.

error TS7016: Could not find a declaration file for module '@openzeppelin/test-helpers'. '/home/sum/DEV/ETH/Polkastarter/staking-hardhat/node_modules/@openzeppelin/test-helpers/index.js' implicitly has an 'any' type.
  Try `npm i --save-dev @types/openzeppelin__test-helpers` if it exists or add a new declaration (.d.ts) file containing `declare module '@openzeppelin/test-helpers';`

8 import { expectRevert, time } from "@openzeppelin/test-helpers";

and because of this, it does not recognize the expectRevert

OpenZeppelin Test Helpers don’t have type declarations yet… You can add // @ts-ignore in the line before the import, and everything will be imported with type any.