Testing for emitted events in Hardhat
This example shows how to work with events in Hardhat in general, and it includes a test that illustrates some scenarios.
The tl;dr is that, given a contract like this one:
contract EventEmitter {
event MyEventWithData(uint, string);
function emitMyEventWithData(uint x, string calldata s) public {
emit MyEventWithData(x, s);
}
}
You can check it in a test like this (notice that this relies on hardhat-waffle):
it("Should emit MyEventWithData", async function () {
const EventEmitter = await ethers.getContractFactory("EventEmitter");
const eventEmitter = await EventEmitter.deploy();
await eventEmitter.deployed();
await expect(eventEmitter.emitMyEventWithData(42, "foo"))
.to.emit(eventEmitter, "MyEventWithData")
.withArgs(42, "foo");
});
My program tester
await expect(mycontract.connect(addr1).mint(1, {value: 1000000000000000}))
.to.emit(mycontract, "Transfer")
.withArgs("0x0000000000000000000000000000000000000000", addr1.getAddress, 9);