diff --git a/.changeset/gorgeous-moose-tap.md b/.changeset/gorgeous-moose-tap.md new file mode 100644 index 0000000000..300f7c0d02 --- /dev/null +++ b/.changeset/gorgeous-moose-tap.md @@ -0,0 +1,5 @@ +--- +"@nomicfoundation/hardhat-chai-matchers": patch +--- + +Fix hardhat-chai-matchers error message when event is overloaded diff --git a/packages/hardhat-chai-matchers/src/internal/emit.ts b/packages/hardhat-chai-matchers/src/internal/emit.ts index 5718861999..23838cfbd6 100644 --- a/packages/hardhat-chai-matchers/src/internal/emit.ts +++ b/packages/hardhat-chai-matchers/src/internal/emit.ts @@ -68,9 +68,12 @@ export function supportEmit( let eventFragment: EventFragment | null = null; try { - eventFragment = contract.interface.getEvent(eventName); - } catch (e) { - // ignore error + eventFragment = contract.interface.getEvent(eventName, []); + } catch (e: unknown) { + if (e instanceof TypeError) { + const errorMessage = e.message.split(" (argument=")[0]; + throw new AssertionError(errorMessage); + } } if (eventFragment === null) { diff --git a/packages/hardhat-chai-matchers/test/contracts.ts b/packages/hardhat-chai-matchers/test/contracts.ts index 451bf646c4..85e5f6e519 100644 --- a/packages/hardhat-chai-matchers/test/contracts.ts +++ b/packages/hardhat-chai-matchers/test/contracts.ts @@ -131,3 +131,16 @@ export type EventsContract = BaseContract & { }; export type AnotherContract = BaseContract & {}; + +export type OverrideEventContract = BaseContract & { + emitSimpleEventWithUintArg: BaseContractMethod< + [BigNumberish], + void, + ContractTransactionResponse + >; + emitSimpleEventWithoutArg: BaseContractMethod< + [], + void, + ContractTransactionResponse + >; +}; diff --git a/packages/hardhat-chai-matchers/test/events.ts b/packages/hardhat-chai-matchers/test/events.ts index e9f9d00a18..ba5069c505 100644 --- a/packages/hardhat-chai-matchers/test/events.ts +++ b/packages/hardhat-chai-matchers/test/events.ts @@ -2,6 +2,7 @@ import type { AnotherContract, EventsContract, MatchersContract, + OverrideEventContract, } from "./contracts"; import { expect, AssertionError } from "chai"; @@ -14,6 +15,7 @@ import { useEnvironment, useEnvironmentWithNode } from "./helpers"; describe(".to.emit (contract events)", () => { let contract: EventsContract; let otherContract: AnotherContract; + let overrideEventContract: OverrideEventContract; let matchers: MatchersContract; describe("with the in-process hardhat network", function () { @@ -38,6 +40,12 @@ describe(".to.emit (contract events)", () => { ) ).deploy(await otherContract.getAddress()); + overrideEventContract = await ( + await this.hre.ethers.getContractFactory<[], OverrideEventContract>( + "OverrideEventContract" + ) + ).deploy(); + const Matchers = await this.hre.ethers.getContractFactory< [], MatchersContract @@ -872,5 +880,30 @@ describe(".to.emit (contract events)", () => { const tx = await contract.emitWithoutArgs(); await expect(tx.hash).to.emit(contract, "WithoutArgs"); }); + + describe("When event is overloaded", () => { + it("Should fail when the event name is ambiguous", async function () { + await expect( + expect(overrideEventContract.emitSimpleEventWithUintArg(1n)).to.emit( + overrideEventContract, + "simpleEvent" + ) + ).to.be.eventually.rejectedWith( + AssertionError, + `ambiguous event description (i.e. matches "simpleEvent(uint256)", "simpleEvent()")` + ); + }); + + it("Should pass when the event name is not ambiguous", async function () { + await expect(overrideEventContract.emitSimpleEventWithUintArg(1n)) + .to.emit(overrideEventContract, "simpleEvent(uint256)") + .withArgs(1); + + await expect(overrideEventContract.emitSimpleEventWithoutArg()).to.emit( + overrideEventContract, + "simpleEvent()" + ); + }); + }); } }); diff --git a/packages/hardhat-chai-matchers/test/fixture-projects/hardhat-project/contracts/Events.sol b/packages/hardhat-chai-matchers/test/fixture-projects/hardhat-project/contracts/Events.sol index 2e3131db88..cb82198dc4 100644 --- a/packages/hardhat-chai-matchers/test/fixture-projects/hardhat-project/contracts/Events.sol +++ b/packages/hardhat-chai-matchers/test/fixture-projects/hardhat-project/contracts/Events.sol @@ -4,7 +4,10 @@ pragma solidity ^0.8.0; contract Events { AnotherContract anotherContract; - struct Struct { uint u; uint v; } + struct Struct { + uint u; + uint v; + } event WithoutArgs(); event WithUintArg(uint u); @@ -115,3 +118,16 @@ contract AnotherContract { emit WithUintArg(u); } } + +contract OverrideEventContract { + event simpleEvent(uint u); + event simpleEvent(); + + function emitSimpleEventWithUintArg(uint u) public { + emit simpleEvent(u); + } + + function emitSimpleEventWithoutArg() public { + emit simpleEvent(); + } +}