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();