Skip to content

Commit 6ec0e83

Browse files
authored
Merge pull request #5727 from iosh/5696
fix: hardhat-chai-matchers error message when event is overloaded
2 parents 75d4219 + 225c1c2 commit 6ec0e83

File tree

5 files changed

+74
-4
lines changed

5 files changed

+74
-4
lines changed

.changeset/gorgeous-moose-tap.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@nomicfoundation/hardhat-chai-matchers": patch
3+
---
4+
5+
Enhanced error message in `.emit` matcher for overloaded events (thanks @iosh!)

packages/hardhat-chai-matchers/src/internal/emit.ts

+6-3
Original file line numberDiff line numberDiff line change
@@ -68,9 +68,12 @@ export function supportEmit(
6868

6969
let eventFragment: EventFragment | null = null;
7070
try {
71-
eventFragment = contract.interface.getEvent(eventName);
72-
} catch (e) {
73-
// ignore error
71+
eventFragment = contract.interface.getEvent(eventName, []);
72+
} catch (e: unknown) {
73+
if (e instanceof TypeError) {
74+
const errorMessage = e.message.split(" (argument=")[0];
75+
throw new AssertionError(errorMessage);
76+
}
7477
}
7578

7679
if (eventFragment === null) {

packages/hardhat-chai-matchers/test/contracts.ts

+13
Original file line numberDiff line numberDiff line change
@@ -131,3 +131,16 @@ export type EventsContract = BaseContract & {
131131
};
132132

133133
export type AnotherContract = BaseContract & {};
134+
135+
export type OverrideEventContract = BaseContract & {
136+
emitSimpleEventWithUintArg: BaseContractMethod<
137+
[BigNumberish],
138+
void,
139+
ContractTransactionResponse
140+
>;
141+
emitSimpleEventWithoutArg: BaseContractMethod<
142+
[],
143+
void,
144+
ContractTransactionResponse
145+
>;
146+
};

packages/hardhat-chai-matchers/test/events.ts

+33
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import type {
22
AnotherContract,
33
EventsContract,
44
MatchersContract,
5+
OverrideEventContract,
56
} from "./contracts";
67

78
import { expect, AssertionError } from "chai";
@@ -14,6 +15,7 @@ import { useEnvironment, useEnvironmentWithNode } from "./helpers";
1415
describe(".to.emit (contract events)", () => {
1516
let contract: EventsContract;
1617
let otherContract: AnotherContract;
18+
let overrideEventContract: OverrideEventContract;
1719
let matchers: MatchersContract;
1820

1921
describe("with the in-process hardhat network", function () {
@@ -38,6 +40,12 @@ describe(".to.emit (contract events)", () => {
3840
)
3941
).deploy(await otherContract.getAddress());
4042

43+
overrideEventContract = await (
44+
await this.hre.ethers.getContractFactory<[], OverrideEventContract>(
45+
"OverrideEventContract"
46+
)
47+
).deploy();
48+
4149
const Matchers = await this.hre.ethers.getContractFactory<
4250
[],
4351
MatchersContract
@@ -872,5 +880,30 @@ describe(".to.emit (contract events)", () => {
872880
const tx = await contract.emitWithoutArgs();
873881
await expect(tx.hash).to.emit(contract, "WithoutArgs");
874882
});
883+
884+
describe("When event is overloaded", () => {
885+
it("Should fail when the event name is ambiguous", async function () {
886+
await expect(
887+
expect(overrideEventContract.emitSimpleEventWithUintArg(1n)).to.emit(
888+
overrideEventContract,
889+
"simpleEvent"
890+
)
891+
).to.be.eventually.rejectedWith(
892+
AssertionError,
893+
`ambiguous event description (i.e. matches "simpleEvent(uint256)", "simpleEvent()")`
894+
);
895+
});
896+
897+
it("Should pass when the event name is not ambiguous", async function () {
898+
await expect(overrideEventContract.emitSimpleEventWithUintArg(1n))
899+
.to.emit(overrideEventContract, "simpleEvent(uint256)")
900+
.withArgs(1);
901+
902+
await expect(overrideEventContract.emitSimpleEventWithoutArg()).to.emit(
903+
overrideEventContract,
904+
"simpleEvent()"
905+
);
906+
});
907+
});
875908
}
876909
});

packages/hardhat-chai-matchers/test/fixture-projects/hardhat-project/contracts/Events.sol

+17-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,10 @@ pragma solidity ^0.8.0;
44
contract Events {
55
AnotherContract anotherContract;
66

7-
struct Struct { uint u; uint v; }
7+
struct Struct {
8+
uint u;
9+
uint v;
10+
}
811

912
event WithoutArgs();
1013
event WithUintArg(uint u);
@@ -115,3 +118,16 @@ contract AnotherContract {
115118
emit WithUintArg(u);
116119
}
117120
}
121+
122+
contract OverrideEventContract {
123+
event simpleEvent(uint u);
124+
event simpleEvent();
125+
126+
function emitSimpleEventWithUintArg(uint u) public {
127+
emit simpleEvent(u);
128+
}
129+
130+
function emitSimpleEventWithoutArg() public {
131+
emit simpleEvent();
132+
}
133+
}

0 commit comments

Comments
 (0)